-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from COS301-SE-2023/development
Development!
- Loading branch information
Showing
143 changed files
with
7,534 additions
and
1,797 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,3 +106,5 @@ out/ | |
### VS Code ### | ||
.vscode/ | ||
angular.json | ||
package-lock.json | ||
package.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +0,0 @@ | ||
{ | ||
"recommendations": [ | ||
"ionic.ionic" | ||
] | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -237,4 +237,4 @@ eval "set -- $( | |
tr '\n' ' ' | ||
)" '"$@"' | ||
|
||
exec "$JAVACMD" "$@" | ||
exec "$JAVACMD" "$@" |
Empty file.
49 changes: 49 additions & 0 deletions
49
backend/src/main/java/fellowship/mealmaestro/config/ApplicationConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package fellowship.mealmaestro.config; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider; | ||
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
|
||
import fellowship.mealmaestro.services.UserService; | ||
|
||
@Configuration | ||
public class ApplicationConfig { | ||
|
||
private final UserService userService; | ||
|
||
public ApplicationConfig(UserService userService){ | ||
this.userService = userService; | ||
} | ||
|
||
@Bean | ||
public UserDetailsService userDetailsService(){ | ||
return username -> userService.findByEmail(username) | ||
.orElseThrow(() -> new UsernameNotFoundException("User '" + username + "' not found")); | ||
} | ||
|
||
@Bean | ||
public AuthenticationProvider authenticationProvider(){ | ||
DaoAuthenticationProvider provider = new DaoAuthenticationProvider(); | ||
provider.setUserDetailsService(userDetailsService()); | ||
provider.setPasswordEncoder(passwordEncoder()); | ||
return provider; | ||
} | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
//TODO | ||
} | ||
|
||
@Bean | ||
public AuthenticationManager authenticationManager(AuthenticationConfiguration config) throws Exception { | ||
return config.getAuthenticationManager(); | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
backend/src/main/java/fellowship/mealmaestro/config/CORSConfig.java
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/fellowship/mealmaestro/config/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package fellowship.mealmaestro.config; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
|
||
@ExceptionHandler(RuntimeException.class) | ||
public ResponseEntity<String> handleUserNotFoundException(RuntimeException e){ | ||
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
backend/src/main/java/fellowship/mealmaestro/config/JwtAuthenticationFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package fellowship.mealmaestro.config; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.web.authentication.WebAuthenticationDetailsSource; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import fellowship.mealmaestro.models.UserModel; | ||
import fellowship.mealmaestro.services.auth.JwtService; | ||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
|
||
@Component | ||
public class JwtAuthenticationFilter extends OncePerRequestFilter { | ||
|
||
private final JwtService jwtService; | ||
|
||
private final UserDetailsService userDetailsService; | ||
|
||
public JwtAuthenticationFilter(JwtService jwtService, UserDetailsService userDetailsService){ | ||
this.jwtService = jwtService; | ||
this.userDetailsService = userDetailsService; | ||
} | ||
|
||
@Override | ||
protected void doFilterInternal( | ||
HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) | ||
throws ServletException, IOException { | ||
|
||
final String authHeader = request.getHeader("Authorization"); | ||
final String jwtToken; | ||
final String userEmail; | ||
|
||
if (authHeader == null || !authHeader.startsWith("Bearer ")) { | ||
filterChain.doFilter(request, response); | ||
return; | ||
} | ||
|
||
jwtToken = authHeader.substring(7); | ||
userEmail = jwtService.extractUserEmail(jwtToken); | ||
|
||
if (userEmail != null && SecurityContextHolder.getContext().getAuthentication() == null) { | ||
UserModel userDetails = (UserModel) this.userDetailsService.loadUserByUsername(userEmail); | ||
if (jwtService.isTokenValid(jwtToken, userDetails)){ | ||
UsernamePasswordAuthenticationToken authToken = new UsernamePasswordAuthenticationToken( | ||
userDetails, | ||
null, | ||
userDetails.getAuthorities() | ||
); | ||
|
||
authToken.setDetails( | ||
new WebAuthenticationDetailsSource().buildDetails(request) | ||
); | ||
|
||
SecurityContextHolder.getContext().setAuthentication(authToken); | ||
} | ||
} | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
backend/src/main/java/fellowship/mealmaestro/config/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package fellowship.mealmaestro.config; | ||
|
||
import java.util.Arrays; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.CorsConfigurationSource; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
public class SecurityConfig { | ||
|
||
private final JwtAuthenticationFilter jwtAuthFilter; | ||
|
||
private final AuthenticationProvider authenticationProvider; | ||
|
||
public SecurityConfig(JwtAuthenticationFilter jwtAuthFilter, AuthenticationProvider authenticationProvider){ | ||
this.jwtAuthFilter = jwtAuthFilter; | ||
this.authenticationProvider = authenticationProvider; | ||
} | ||
|
||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{ | ||
http | ||
.cors(cors -> cors.configurationSource(corsConfigurationSource())) | ||
.csrf(csrf -> csrf.disable()) | ||
.authorizeHttpRequests(authReq -> authReq | ||
.requestMatchers("/register", "/authenticate") | ||
.permitAll() | ||
.anyRequest() | ||
.authenticated() | ||
) | ||
.sessionManagement(session -> session | ||
.sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
) | ||
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class); | ||
|
||
|
||
http.authenticationProvider(authenticationProvider); | ||
return http.build(); | ||
} | ||
|
||
@Bean | ||
CorsConfigurationSource corsConfigurationSource(){ | ||
CorsConfiguration corsConfig = new CorsConfiguration(); | ||
corsConfig.setAllowedOrigins(Arrays.asList("http://localhost:4200", "http://localhost:8100")); | ||
corsConfig.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE", "OPTIONS")); | ||
corsConfig.setAllowedHeaders(Arrays.asList("*")); | ||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
source.registerCorsConfiguration("/**", corsConfig); | ||
return source; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
backend/src/main/java/fellowship/mealmaestro/controllers/BrowseController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package fellowship.mealmaestro.controllers; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestHeader; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import fellowship.mealmaestro.models.MealModel; | ||
import fellowship.mealmaestro.services.BrowseService; | ||
//import fellowship.mealmaestro.services.PantryService; | ||
import jakarta.validation.Valid; | ||
|
||
@RestController | ||
public class BrowseController { | ||
|
||
@Autowired | ||
private BrowseService browseService; | ||
|
||
@GetMapping("/getPopularMeals") | ||
public ResponseEntity<List<MealModel>> getPopularMeals(@RequestHeader("Authorization") String token){ | ||
if (token == null || token.isEmpty()) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
String authToken = token.substring(7); | ||
return ResponseEntity.ok(browseService.getPopularMeals(authToken)); | ||
} | ||
|
||
@GetMapping("/getSearchedMeals") | ||
public ResponseEntity<List<MealModel>> getSearcedhMeals(@RequestParam("query") String mealName, @RequestHeader("Authorization") String token){ | ||
if (token == null || token.isEmpty()) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
String authToken = token.substring(7); | ||
return ResponseEntity.ok(browseService.getSearchedMeals(mealName,authToken)); | ||
} | ||
|
||
} |
Oops, something went wrong.