-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added everything * major changes. * BE: Chore Backend User cleanups * BE: Working Version of user registration * Spotless code fixes * Fix Swagger Security Configs * Fix Spotless Pipeline issues --------- Co-authored-by: Mgrdich <[email protected]>
- Loading branch information
Showing
33 changed files
with
900 additions
and
3 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
33 changes: 33 additions & 0 deletions
33
backend/src/main/java/com/llm_service/llm_service/config/CustomLogoutHandler.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,33 @@ | ||
package com.llm_service.llm_service.config; | ||
|
||
import com.llm_service.llm_service.persistance.entities.TokenEntity; | ||
import com.llm_service.llm_service.persistance.repositories.token.TokenRepository; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.web.authentication.logout.LogoutHandler; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class CustomLogoutHandler implements LogoutHandler { | ||
private final TokenRepository tokenRepository; | ||
|
||
@Override | ||
public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) { | ||
String authHeader = request.getHeader("Authorization"); | ||
|
||
if (authHeader == null || !authHeader.startsWith("Bearer ")) { | ||
return; | ||
} | ||
|
||
String token = authHeader.substring(7); | ||
TokenEntity storedToken = tokenRepository.findByToken(token).orElse(null); | ||
|
||
if (storedToken != null) { | ||
storedToken.setLoggedOut(true); | ||
tokenRepository.save(storedToken); | ||
} | ||
} | ||
} |
52 changes: 49 additions & 3 deletions
52
backend/src/main/java/com/llm_service/llm_service/config/SecurityConfiguration.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 |
---|---|---|
@@ -1,21 +1,67 @@ | ||
package com.llm_service.llm_service.config; | ||
|
||
import com.llm_service.llm_service.persistance.entities.Role; | ||
import com.llm_service.llm_service.service.jwt.filter.JwtAuthenticationFilter; | ||
import com.llm_service.llm_service.service.user.UserDetailsServiceImp; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.Customizer; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.security.authentication.AuthenticationManager; | ||
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.HttpStatusEntryPoint; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
@RequiredArgsConstructor | ||
public class SecurityConfiguration { | ||
private final UserDetailsServiceImp userDetailsServiceImp; | ||
|
||
private final JwtAuthenticationFilter jwtAuthenticationFilter; | ||
|
||
private final CustomLogoutHandler logoutHandler; | ||
|
||
@Bean | ||
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { | ||
|
||
// TODO fix the swagger security config | ||
return http.csrf(AbstractHttpConfigurer::disable) | ||
.authorizeHttpRequests(req -> req.anyRequest().permitAll()) | ||
.httpBasic(Customizer.withDefaults()) | ||
.authorizeHttpRequests(req -> req.requestMatchers( | ||
"/login/**", "/register/**", "/forget-password/**", "/swagger-ui/**", "/v3/api-docs/**") | ||
.permitAll() | ||
.requestMatchers("/paid/**") | ||
.hasAuthority(Role.PAID.name()) | ||
.anyRequest() | ||
.authenticated()) | ||
.userDetailsService(userDetailsServiceImp) | ||
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS)) | ||
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class) | ||
.exceptionHandling(e -> e.accessDeniedHandler((request, response, accessDeniedException) -> | ||
response.setStatus(HttpStatus.FORBIDDEN.value())) | ||
.authenticationEntryPoint(new HttpStatusEntryPoint(HttpStatus.UNAUTHORIZED))) | ||
.logout(l -> l.logoutUrl("/logout") | ||
.addLogoutHandler(logoutHandler) | ||
.logoutSuccessHandler( | ||
(request, response, authentication) -> SecurityContextHolder.clearContext())) | ||
.build(); | ||
} | ||
|
||
@Bean | ||
public PasswordEncoder passwordEncoder() { | ||
return new BCryptPasswordEncoder(); | ||
} | ||
|
||
@Bean | ||
public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception { | ||
return configuration.getAuthenticationManager(); | ||
} | ||
} |
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
2 changes: 2 additions & 0 deletions
2
...rc/main/java/com/llm_service/llm_service/controller/conversation/ConversationRequest.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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
package com.llm_service.llm_service.controller.conversation; | ||
|
||
import lombok.Builder; | ||
import lombok.NonNull; | ||
import lombok.Value; | ||
import lombok.extern.jackson.Jacksonized; | ||
|
||
@Value | ||
@Builder | ||
@Jacksonized | ||
public class ConversationRequest { | ||
@NonNull | ||
String text; | ||
} |
2 changes: 2 additions & 0 deletions
2
...in/java/com/llm_service/llm_service/controller/conversation/ConversationTitleRequest.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 |
---|---|---|
@@ -1,12 +1,14 @@ | ||
package com.llm_service.llm_service.controller.conversation; | ||
|
||
import lombok.Builder; | ||
import lombok.NonNull; | ||
import lombok.Value; | ||
import lombok.extern.jackson.Jacksonized; | ||
|
||
@Value | ||
@Builder | ||
@Jacksonized | ||
public class ConversationTitleRequest { | ||
@NonNull | ||
String title; | ||
} |
17 changes: 17 additions & 0 deletions
17
backend/src/main/java/com/llm_service/llm_service/controller/user/LoginRequest.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,17 @@ | ||
package com.llm_service.llm_service.controller.user; | ||
|
||
import lombok.Builder; | ||
import lombok.NonNull; | ||
import lombok.Value; | ||
import lombok.extern.jackson.Jacksonized; | ||
|
||
@Value | ||
@Builder | ||
@Jacksonized | ||
public class LoginRequest { | ||
@NonNull | ||
String username; | ||
|
||
@NonNull | ||
String password; | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/com/llm_service/llm_service/controller/user/LoginResponse.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,12 @@ | ||
package com.llm_service.llm_service.controller.user; | ||
|
||
import lombok.Builder; | ||
import lombok.Value; | ||
import lombok.extern.jackson.Jacksonized; | ||
|
||
@Value | ||
@Builder | ||
@Jacksonized | ||
public class LoginResponse { | ||
String token; | ||
} |
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/com/llm_service/llm_service/controller/user/UserApiMapper.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,14 @@ | ||
package com.llm_service.llm_service.controller.user; | ||
|
||
import com.llm_service.llm_service.dto.User; | ||
import com.llm_service.llm_service.service.jwt.AuthenticationResponse; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
|
||
@Mapper(componentModel = "spring") | ||
public interface UserApiMapper { | ||
LoginResponse map(AuthenticationResponse response); | ||
|
||
@Mapping(target = "name", source = "username") | ||
UserResponse map(User user); | ||
} |
80 changes: 80 additions & 0 deletions
80
backend/src/main/java/com/llm_service/llm_service/controller/user/UserController.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,80 @@ | ||
package com.llm_service.llm_service.controller.user; | ||
|
||
import com.llm_service.llm_service.dto.User; | ||
import com.llm_service.llm_service.exception.user.UserAlreadyExistsException; | ||
import com.llm_service.llm_service.exception.user.UserNotFoundException; | ||
import com.llm_service.llm_service.exception.user.UsernameAlreadyExistsException; | ||
import com.llm_service.llm_service.service.jwt.AuthenticationResponse; | ||
import com.llm_service.llm_service.service.jwt.AuthenticationService; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponses; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@CrossOrigin("http://localhost:4040") | ||
@RestController | ||
public class UserController { | ||
private final AuthenticationService authenticationService; | ||
private final UserApiMapper userApiMapper; | ||
|
||
public UserController(AuthenticationService authenticationService, UserApiMapper userApiMapper) { | ||
this.authenticationService = authenticationService; | ||
this.userApiMapper = userApiMapper; | ||
} | ||
|
||
@ApiResponses( | ||
value = { | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "Creates a new user", | ||
content = {@Content(mediaType = "application/json")}) | ||
}) | ||
@Operation(summary = "register the user") | ||
@PostMapping("/register") | ||
public ResponseEntity<UserResponse> register(@RequestBody UserRequest userRequest) | ||
throws UsernameAlreadyExistsException { | ||
User user = authenticationService.register(userRequest); | ||
return ResponseEntity.status(HttpStatus.OK).body(userApiMapper.map(user)); | ||
} | ||
|
||
@ApiResponses( | ||
value = { | ||
@ApiResponse( | ||
responseCode = "200", | ||
description = "Logs the user into the system", | ||
content = {@Content(mediaType = "application/json")}) | ||
}) | ||
@Operation(summary = "login phase of the user") | ||
@PostMapping("/login") | ||
public ResponseEntity<LoginResponse> login(@RequestBody LoginRequest loginRequest) | ||
throws UserNotFoundException, AuthenticationException { | ||
AuthenticationResponse authenticationResponse = authenticationService.authenticate(loginRequest); | ||
return ResponseEntity.ok(userApiMapper.map(authenticationResponse)); | ||
} | ||
|
||
@ExceptionHandler(UserAlreadyExistsException.class) | ||
ResponseEntity<String> handleUsernameAlreadyExistsExceptions( | ||
UserAlreadyExistsException usernameAlreadyExistsException) { | ||
return ResponseEntity.status(HttpStatus.CONFLICT).body(usernameAlreadyExistsException.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(UsernameAlreadyExistsException.class) | ||
ResponseEntity<String> handleUsernameAlreadyExistsExceptions( | ||
UsernameAlreadyExistsException usernameAlreadyExistsException) { | ||
return ResponseEntity.status(HttpStatus.CONFLICT).body(usernameAlreadyExistsException.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(UserNotFoundException.class) | ||
ResponseEntity<String> handleUsernameNotFoundExceptions(UserNotFoundException usernameNotFoundException) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(usernameNotFoundException.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(AuthenticationException.class) | ||
ResponseEntity<String> handleAuthenticationException(AuthenticationException authenticationException) { | ||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(authenticationException.getMessage()); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
backend/src/main/java/com/llm_service/llm_service/controller/user/UserRequest.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,25 @@ | ||
package com.llm_service.llm_service.controller.user; | ||
|
||
import com.llm_service.llm_service.persistance.entities.Role; | ||
import lombok.*; | ||
import lombok.extern.jackson.Jacksonized; | ||
|
||
@Value | ||
@Builder | ||
@Jacksonized | ||
public class UserRequest { | ||
@NonNull | ||
String username; | ||
|
||
@NonNull | ||
String password; | ||
|
||
@NonNull | ||
String firstName; | ||
|
||
@NonNull | ||
String lastName; | ||
|
||
@NonNull | ||
Role role; | ||
} |
12 changes: 12 additions & 0 deletions
12
backend/src/main/java/com/llm_service/llm_service/controller/user/UserResponse.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,12 @@ | ||
package com.llm_service.llm_service.controller.user; | ||
|
||
import lombok.Value; | ||
import lombok.experimental.SuperBuilder; | ||
import lombok.extern.jackson.Jacksonized; | ||
|
||
@Value | ||
@Jacksonized | ||
@SuperBuilder | ||
public class UserResponse { | ||
String name; | ||
} |
17 changes: 17 additions & 0 deletions
17
backend/src/main/java/com/llm_service/llm_service/dto/User.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,17 @@ | ||
package com.llm_service.llm_service.dto; | ||
|
||
import com.llm_service.llm_service.persistance.entities.Role; | ||
import java.util.UUID; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
|
||
@Value | ||
@Builder(toBuilder = true) | ||
public class User { | ||
UUID id; | ||
String firstName; | ||
String lastName; | ||
String username; | ||
String password; | ||
Role role; | ||
} |
Oops, something went wrong.