Skip to content

Commit

Permalink
Task 63 : Add missing Java Doc for user service
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapter1990 committed Jul 21, 2024
1 parent b72db61 commit 41f956b
Show file tree
Hide file tree
Showing 44 changed files with 591 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,19 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* The entry point for the Product service Spring Boot application.
* This application is a Eureka client that registers itself with a Eureka server.
* The application is configured with the {@link SpringBootApplication} annotation.
*/
@SpringBootApplication
public class UserserviceApplication {

/**
* Main method to run the Spring Boot application.
*
* @param args Command-line arguments passed during the application startup.
*/
public static void main(String[] args) {
SpringApplication.run(UserserviceApplication.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,35 @@

import java.util.List;

/**
* Security configuration class named {@link SecurityConfig} for setting up security filters and policies.
* Configures HTTP security settings, CORS, CSRF, and session management.
*/
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
@EnableMethodSecurity
public class SecurityConfig {

/**
* Configures the session authentication strategy.
*
* @return a {@link SessionAuthenticationStrategy} instance
*/
@Bean
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}

/**
* Configures the security filter chain for HTTP requests.
*
* @param httpSecurity the {@link HttpSecurity} to configure
* @param customBearerTokenAuthenticationFilter the custom filter for token authentication
* @param customAuthenticationEntryPoint the custom entry point for authentication errors
* @return a configured {@link SecurityFilterChain} instance
* @throws Exception if an error occurs while configuring security
*/
@Bean
public SecurityFilterChain filterChain(
final HttpSecurity httpSecurity,
Expand All @@ -56,7 +74,11 @@ public SecurityFilterChain filterChain(
return httpSecurity.build();
}


/**
* Provides CORS configuration for the application.
*
* @return a {@link CorsConfigurationSource} instance
*/
private CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(List.of("*"));
Expand All @@ -67,6 +89,11 @@ private CorsConfigurationSource corsConfigurationSource() {
return source;
}

/**
* Provides a {@link PasswordEncoder} bean for encoding passwords.
*
* @return a {@link PasswordEncoder} instance
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import java.security.PrivateKey;
import java.security.PublicKey;

/**
* Configuration class named {@link TokenConfigurationParameter} for token parameters.
* Provides access to token expiration settings and cryptographic keys.
*/
@Getter
@Configuration
public class TokenConfigurationParameter {
Expand All @@ -19,8 +23,6 @@ public class TokenConfigurationParameter {

public TokenConfigurationParameter() {

//this.issuer = ConfigurationParameter.ISSUER.getDefaultValue();

this.accessTokenExpireMinute = Integer.parseInt(
ConfigurationParameter.AUTH_ACCESS_TOKEN_EXPIRE_MINUTE.getDefaultValue()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
* REST controller named {@link UserController} for managing user-related operations.
* Provides endpoints for user registration, token validation, login, token refresh, logout, and authentication retrieval.
*/
@RestController
@RequestMapping("/api/v1/users")
@RequiredArgsConstructor
Expand All @@ -35,20 +39,38 @@ public class UserController {

private final TokenToTokenResponseMapper tokenToTokenResponseMapper = TokenToTokenResponseMapper.initialize();

/**
* Registers a new user.
*
* @param registerRequest the user registration request containing user details
* @return a {@link CustomResponse} indicating the success of the registration
*/
@PostMapping("/register")
public CustomResponse<Void> registerUser(@RequestBody @Validated final RegisterRequest registerRequest) {
log.info("UserController | registerUser");
registerService.registerUser(registerRequest);
return CustomResponse.SUCCESS;
}

/**
* Validates the provided token.
*
* @param token the token to be validated
* @return a {@link ResponseEntity} with an HTTP status code indicating the validation result
*/
@PostMapping("/validate-token")
public ResponseEntity<Void> validateToken(@RequestParam String token) {
log.info("UserController | validateToken");
tokenService.verifyAndValidate(token);
return ResponseEntity.ok().build();
}

/**
* Logs in a user and generates a new token.
*
* @param loginRequest the login request containing user credentials
* @return a {@link CustomResponse} containing the generated {@link TokenResponse}
*/
@PostMapping("/login")
public CustomResponse<TokenResponse> loginUser(@RequestBody @Valid final LoginRequest loginRequest) {
log.info("UserController | validateToken");
Expand All @@ -57,6 +79,12 @@ public CustomResponse<TokenResponse> loginUser(@RequestBody @Valid final LoginRe
return CustomResponse.successOf(tokenResponse);
}

/**
* Refreshes the token based on the provided refresh request.
*
* @param tokenRefreshRequest the token refresh request containing the refresh token
* @return a {@link CustomResponse} containing the new {@link TokenResponse}
*/
@PostMapping("/refresh-token")
public CustomResponse<TokenResponse> refreshToken(@RequestBody @Valid final TokenRefreshRequest tokenRefreshRequest) {
log.info("UserController | refreshToken");
Expand All @@ -65,13 +93,25 @@ public CustomResponse<TokenResponse> refreshToken(@RequestBody @Valid final Toke
return CustomResponse.successOf(tokenResponse);
}

/**
* Logs out a user by invalidating the provided token.
*
* @param tokenInvalidateRequest the request containing the token to be invalidated
* @return a {@link CustomResponse} indicating the success of the logout operation
*/
@PostMapping("/logout")
public CustomResponse<Void> logout(@RequestBody @Valid final TokenInvalidateRequest tokenInvalidateRequest) {
log.info("UserController | logout");
logoutService.logout(tokenInvalidateRequest);
return CustomResponse.SUCCESS;
}

/**
* Retrieves the authentication details for the provided token.
*
* @param token the token for which to retrieve authentication details
* @return a {@link ResponseEntity} containing the {@link UsernamePasswordAuthenticationToken} for the token
*/
@GetMapping("/authenticate")
public ResponseEntity<UsernamePasswordAuthenticationToken> getAuthentication(@RequestParam String token) {
UsernamePasswordAuthenticationToken authentication = tokenService.getAuthentication(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.io.Serial;

/**
* Exception named {@link PasswordNotValidException} thrown when a password does not meet the required criteria.
*/
public class PasswordNotValidException extends RuntimeException {

@Serial
Expand All @@ -11,10 +14,18 @@ public class PasswordNotValidException extends RuntimeException {
Password is not valid!
""";

/**
* Constructs a {@code PasswordNotValidException} with the default message.
*/
public PasswordNotValidException() {
super(DEFAULT_MESSAGE);
}

/**
* Constructs a {@code PasswordNotValidException} with a custom message.
*
* @param message the detail message
*/
public PasswordNotValidException(final String message) {
super(DEFAULT_MESSAGE + " " + message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.io.Serial;

/**
* Exception named {@link TokenAlreadyInvalidatedException} thrown when a token has already been invalidated.
*/
public class TokenAlreadyInvalidatedException extends RuntimeException {

@Serial
Expand All @@ -11,10 +14,18 @@ public class TokenAlreadyInvalidatedException extends RuntimeException {
Token is already invalidated!
""";

/**
* Constructs a {@code TokenAlreadyInvalidatedException} with the default message.
*/
public TokenAlreadyInvalidatedException() {
super(DEFAULT_MESSAGE);
}

/**
* Constructs a {@code TokenAlreadyInvalidatedException} with a custom message including the token ID.
*
* @param tokenId the ID of the invalidated token
*/
public TokenAlreadyInvalidatedException(final String tokenId) {
super(DEFAULT_MESSAGE + " TokenID = " + tokenId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.io.Serial;

/**
* Exception named {@link UserAlreadyExistException} thrown when an attempt is made to register a user who already exists.
*/
public class UserAlreadyExistException extends RuntimeException {

@Serial
Expand All @@ -11,10 +14,18 @@ public class UserAlreadyExistException extends RuntimeException {
User already exist!
""";

/**
* Constructs a {@code UserAlreadyExistException} with the default message.
*/
public UserAlreadyExistException() {
super(DEFAULT_MESSAGE);
}

/**
* Constructs a {@code UserAlreadyExistException} with a custom message.
*
* @param message the detail message
*/
public UserAlreadyExistException(final String message) {
super(DEFAULT_MESSAGE + " " + message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.io.Serial;

/**
* Exception named {@link UserNotFoundException} thrown when a requested user cannot be found.
*/
public class UserNotFoundException extends RuntimeException {

@Serial
Expand All @@ -11,10 +14,18 @@ public class UserNotFoundException extends RuntimeException {
User not found!
""";

/**
* Constructs a {@code UserNotFoundException} with the default message.
*/
public UserNotFoundException() {
super(DEFAULT_MESSAGE);
}

/**
* Constructs a {@code UserNotFoundException} with a custom message.
*
* @param message the detail message
*/
public UserNotFoundException(final String message) {
super(DEFAULT_MESSAGE + " " + message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import java.io.Serial;

/**
* Exception named {@link UserStatusNotValidException} thrown when a user's status is not valid for the current operation.
*/
public class UserStatusNotValidException extends RuntimeException {

@Serial
Expand All @@ -11,10 +14,18 @@ public class UserStatusNotValidException extends RuntimeException {
User status is not valid!
""";

/**
* Constructs a {@code UserStatusNotValidException} with the default message.
*/
public UserStatusNotValidException() {
super(DEFAULT_MESSAGE);
}

/**
* Constructs a {@code UserStatusNotValidException} with a custom message.
*
* @param message the detail message
*/
public UserStatusNotValidException(final String message) {
super(DEFAULT_MESSAGE + " " + message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@

import java.io.IOException;

/**
* Custom filter named {@link CustomBearerTokenAuthenticationFilter} for handling Bearer token authentication in HTTP requests.
* This filter extracts the Bearer token from the Authorization header,
* validates it, and sets the authentication context if the token is valid.
*/
@Slf4j
@Component
@RequiredArgsConstructor
Expand All @@ -27,6 +32,20 @@ public class CustomBearerTokenAuthenticationFilter extends OncePerRequestFilter
private final TokenService tokenService;
private final InvalidTokenService invalidTokenService;

/**
* Processes the HTTP request and checks for Bearer token authentication.
* <p>
* This method extracts the Bearer token from the request, verifies its validity,
* and checks whether the token has been invalidated. If the token is valid, it sets
* the authentication in the SecurityContext.
* </p>
*
* @param httpServletRequest the HTTP request
* @param httpServletResponse the HTTP response
* @param filterChain the filter chain to pass the request and response
* @throws ServletException if an error occurs during filtering
* @throws IOException if an I/O error occurs
*/
@Override
protected void doFilterInternal(@NonNull final HttpServletRequest httpServletRequest,
@NonNull final HttpServletResponse httpServletResponse,
Expand Down
Loading

0 comments on commit 41f956b

Please sign in to comment.