Skip to content

Commit

Permalink
Task 61 : Add missing Java Doc for auth service
Browse files Browse the repository at this point in the history
  • Loading branch information
Rapter1990 committed Jul 21, 2024
1 parent 19c2f69 commit b2b1617
Show file tree
Hide file tree
Showing 24 changed files with 269 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,20 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
* The entry point for the API Gateway 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
@EnableFeignClients
public class AuthserviceApplication {

/**
* 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(AuthserviceApplication.class, args);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,55 @@
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

/**
* Feign client interface named {@link UserServiceClient} for interacting with the User Service.
* This client is used to perform various operations related to user management,
* such as registration, token validation, login, token refresh, and logout.
*/
@FeignClient(name = "userservice", path = "/api/v1/users")
public interface UserServiceClient {

/**
* Registers a new user with the provided registration request.
*
* @param request the registration request containing user details
* @return the registered user
*/
@PostMapping("/register")
ResponseEntity<User> register(@RequestBody @Valid final RegisterRequest request);

/**
* Validates the given token by making a POST request to the User Service.
*
* @param token the token to be validated
*/
@PostMapping("/validate-token")
void validateToken(@RequestParam String token);

/**
* Logs in a user with the provided login request.
*
* @param loginRequest the login request containing user credentials
* @return the token response containing access and refresh tokens
*/
@PostMapping("/login")
CustomResponse<TokenResponse> loginUser(@RequestBody @Valid final LoginRequest loginRequest);

/**
* Refreshes the access token using the provided token refresh request.
*
* @param tokenRefreshRequest the token refresh request containing the refresh token
* @return the token response containing new access and refresh tokens
*/
@PostMapping("/refresh-token")
CustomResponse<TokenResponse> refreshToken(@RequestBody @Valid final TokenRefreshRequest tokenRefreshRequest);

/**
* Logs out a user by invalidating the provided token.
*
* @param tokenInvalidateRequest the token invalidate request containing the token to be invalidated
* @return a response indicating the result of the logout operation
*/
@PostMapping("/logout")
CustomResponse<Void> logout(@RequestBody @Valid final TokenInvalidateRequest tokenInvalidateRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,35 @@

import java.util.List;

/**
* Configuration class named {@link SecurityConfig} for Spring Security.
* It sets up security filters, session management, CORS configuration, and password encoding.
*/
@Configuration
@EnableWebSecurity
@RequiredArgsConstructor
@EnableMethodSecurity
public class SecurityConfig {

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

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


/**
* Configures the CORS settings.
*
* @return the CORS configuration source
*/
private CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
configuration.setAllowedOrigins(List.of("*"));
Expand All @@ -67,8 +89,14 @@ private CorsConfigurationSource corsConfigurationSource() {
return source;
}

/**
* Configures the password encoder.
*
* @return the password encoder
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* Controller named {@link AuthController} for handling authentication-related operations.
* This includes user registration, login, token refresh, and logout.
*/
@RestController
@RequestMapping("/api/v1/authentication/users")
@RequiredArgsConstructor
Expand All @@ -30,23 +34,46 @@ public class AuthController {

private final LogoutService logoutService;


/**
* Registers a new user with the provided registration request.
*
* @param registerRequest the registration request containing user details
* @return a response indicating success
*/
@PostMapping("/register")
public CustomResponse<Void> registerAdmin(@RequestBody @Valid final RegisterRequest registerRequest) {
registerService.registerUser(registerRequest);
return CustomResponse.SUCCESS;
}

/**
* Logs in a user with the provided login request.
*
* @param loginRequest the login request containing user credentials
* @return the token response containing access and refresh tokens
*/
@PostMapping("/login")
public CustomResponse<TokenResponse> loginUser(@RequestBody @Valid final LoginRequest loginRequest) {
return userLoginService.login(loginRequest);
}

/**
* Refreshes the access token using the provided token refresh request.
*
* @param tokenRefreshRequest the token refresh request containing the refresh token
* @return the token response containing new access and refresh tokens
*/
@PostMapping("/refresh-token")
public CustomResponse<TokenResponse> refreshToken(@RequestBody @Valid final TokenRefreshRequest tokenRefreshRequest) {
return refreshTokenService.refreshToken(tokenRefreshRequest);
}

/**
* Logs out a user by invalidating the provided token.
*
* @param tokenInvalidateRequest the token invalidate request containing the token to be invalidated
* @return a response indicating success
*/
@PostMapping("/logout")
public CustomResponse<Void> logout(@RequestBody @Valid final TokenInvalidateRequest tokenInvalidateRequest) {
logoutService.logout(tokenInvalidateRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import java.time.LocalDateTime;
import java.util.List;

/**
* Represents a custom error response named {@link CustomError} for the API.
* It contains details about the error such as time, HTTP status, header, message, and sub-errors.
*/
@Getter
@Builder
public class CustomError {
Expand All @@ -29,6 +33,9 @@ public class CustomError {
@JsonInclude(JsonInclude.Include.NON_NULL)
private List<CustomSubError> subErrors;

/**
* Represents a sub-error, providing additional details about the error.
*/
@Getter
@Builder
public static class CustomSubError {
Expand All @@ -45,6 +52,9 @@ public static class CustomSubError {

}

/**
* Enum representing the headers for different types of errors.
*/
@Getter
@RequiredArgsConstructor
public enum Header {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,26 @@

import java.io.IOException;

/**
* Filter named {@link CustomBearerTokenAuthenticationFilter} for authenticating requests using a Bearer token.
* This filter validates the Bearer token by calling the user service.
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class CustomBearerTokenAuthenticationFilter extends OncePerRequestFilter {

private final UserServiceClient userServiceClient;

/**
* Filters requests to authenticate using a Bearer token.
*
* @param httpServletRequest the HTTP request
* @param httpServletResponse the HTTP response
* @param filterChain the filter chain
* @throws ServletException if an error occurs during filtering
* @throws IOException if an I/O error occurs during filtering
*/
@Override
protected void doFilterInternal(@NonNull final HttpServletRequest httpServletRequest,
@NonNull final HttpServletResponse httpServletResponse,
Expand All @@ -37,5 +50,7 @@ protected void doFilterInternal(@NonNull final HttpServletRequest httpServletReq
}

filterChain.doFilter(httpServletRequest, httpServletResponse);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import lombok.Getter;
import org.springframework.util.StringUtils;

/**
* Represents an authentication token named {@link Token} used for securing API requests.
* This class contains the access token, its expiration time, and the refresh token.
*/
@Getter
@Builder
public class Token {
Expand All @@ -14,11 +18,24 @@ public class Token {

private static final String TOKEN_PREFIX = "Bearer ";

/**
* Checks if the given authorization header contains a Bearer token.
*
* @param authorizationHeader the Authorization header value
* @return {@code true} if the header starts with "Bearer "; {@code false} otherwise
*/
public static boolean isBearerToken(final String authorizationHeader) {
return StringUtils.hasText(authorizationHeader) &&
authorizationHeader.startsWith(TOKEN_PREFIX);
}

/**
* Extracts the JWT from the given Authorization header.
* The JWT is expected to follow the Bearer token prefix.
*
* @param authorizationHeader the Authorization header value
* @return the JWT extracted from the header, without the "Bearer " prefix
*/
public static String getJwt(final String authorizationHeader) {
return authorizationHeader.replace(TOKEN_PREFIX, "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
import lombok.Setter;
import lombok.experimental.SuperBuilder;

/**
* Represents a user named {@link User} in the system.
* This class contains information about the user's identity, contact details, status, and type.
*/
@Getter
@Setter
@SuperBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import jakarta.validation.constraints.NotBlank;
import lombok.*;

/**
* Represents a login request named {@link LoginRequest} containing the user's email and password.
*/
@Getter
@Setter
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import jakarta.validation.constraints.Size;
import lombok.*;

/**
* Represents a request named {@link RegisterRequest} for user registration.
* This class contains the necessary details required to register a new user.
*/
@Getter
@Setter
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import jakarta.validation.constraints.NotBlank;
import lombok.*;

/**
* Represents a request named {@link TokenInvalidateRequest} to invalidate tokens.
* This class contains the access and refresh tokens that need to be invalidated.
*/
@Getter
@Setter
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import jakarta.validation.constraints.NotBlank;
import lombok.*;

/**
* Represents a request named {@link TokenRefreshRequest} to refresh an access token using a refresh token.
* This class contains the refresh token required for obtaining a new access token.
*/
@Getter
@Setter
@Builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import lombok.*;

/**
* Represents a response named {@link TokenResponse} containing tokens for authentication.
* This class includes the access token, its expiration time, and the refresh token.
*/
@Getter
@Setter
@Builder
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.springbootmicroservices.authservice.model.auth.enums;

/**
* Represents the status of a user.
* This enum defines the different states a user can have in the system.
*/
public enum UserStatus {
ACTIVE,
PASSIVE,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.springbootmicroservices.authservice.model.auth.enums;

/**
* Represents the type of a user in the system.
* This enum defines the different roles a user can have.
*/
public enum UserType {
USER,
ADMIN
Expand Down
Loading

0 comments on commit b2b1617

Please sign in to comment.