feat: add cors config and refresh token api

issue #46
This commit is contained in:
Ricardo Campos
2024-09-19 10:48:49 -03:00
parent 6020f8f6d5
commit 24f858e6a0
6 changed files with 114 additions and 4 deletions
@@ -0,0 +1,34 @@
package br.com.tasknoteapp.java_api.config;
import java.util.Arrays;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/** This class contains configurations for CORS management. */
@Slf4j
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Value("${cors.allowed-origins}")
private String[] allowedOrigins;
/**
* Add CORS mappings configuration.
*
* @param registry CorsRegistry instance.
*/
public void addCorsMappings(@NonNull CorsRegistry registry) {
if (allowedOrigins != null && allowedOrigins.length > 0) {
log.info("CORS policy allowed origins: {}", Arrays.asList(allowedOrigins));
registry
.addMapping("/**")
.allowedOrigins(allowedOrigins)
.allowedMethods("GET", "PUT", "POST", "DELETE", "OPTIONS", "HEAD", "PATCH");
}
}
}
@@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/** This class contains resources for handling users admin requests. */
@RestController
@RequestMapping("/rest/users")
@AllArgsConstructor
@@ -0,0 +1,50 @@
package br.com.tasknoteapp.java_api.controller;
import br.com.tasknoteapp.java_api.exception.UserNotFoundException;
import br.com.tasknoteapp.java_api.response.JwtAuthenticationResponse;
import br.com.tasknoteapp.java_api.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/** This class contains resources for handling user sessions. */
@RestController
@RequestMapping("/rest/user-sessions")
@AllArgsConstructor
@Tag(name = "User Sessions", description = "Resources to handle user sessions.")
public class UserSessionController {
private final AuthService authService;
/**
* Refresh an existing user session, generating a new token.
*
* @return JwtAuthenticationResponse with token created.
* @throws UserNotFoundException if user not found
*/
@GetMapping(path = "/refresh")
@Operation(
summary = "Refresh an existing user session",
description = "Refresh an existing user session, generating a new token",
responses = {
@ApiResponse(responseCode = "200", description = "Session successfully refreshed"),
@ApiResponse(
responseCode = "403",
description = "Forbidden. Access Denied",
content = @Content(schema = @Schema(implementation = Void.class))),
@ApiResponse(
responseCode = "404",
description = "User not found",
content = @Content(schema = @Schema(implementation = Void.class)))
})
public JwtAuthenticationResponse refresh() {
String token = authService.refreshCurrentUserToken();
return new JwtAuthenticationResponse(token);
}
}
@@ -49,4 +49,11 @@ public interface AuthService {
* @return List of UserEntity.
*/
public List<UserResponse> getAllUsers();
/**
* Refresh the current user token.
*
* @return Token
*/
public String refreshCurrentUserToken();
}
@@ -124,13 +124,9 @@ class AuthServiceImpl implements AuthService {
*/
@Override
public List<UserResponse> getAllUsers() {
log.info("1");
Optional<String> currentUserEmail = authUtil.getCurrentUserEmail();
log.info("2");
String email = currentUserEmail.orElseThrow();
log.info("3");
UserEntity currentUser = findByEmail(email).orElseThrow();
log.info("4");
log.info("Getting all users to user {}", currentUser.getId());
@@ -156,4 +152,23 @@ class AuthServiceImpl implements AuthService {
return usersResponse;
}
/**
* Refresh the current user token.
*
* @return Token
*/
@Override
public String refreshCurrentUserToken() {
Optional<String> currentUserEmail = authUtil.getCurrentUserEmail();
String email = currentUserEmail.orElseThrow();
UserEntity currentUser = findByEmail(email).orElseThrow();
log.info("Refreshing current session to user {}", currentUser.getId());
String token = jwtService.generateToken(email);
log.info("User refreshed! Token {}", token);
return token;
}
}
@@ -22,3 +22,6 @@ springdoc.enable-native-support=true
# Version (build)
br.com.tasknote.java-api.version=${BUILD:local}
# CORS
cors.allowed-origins=${CORS_ALLOWED_ORIGINS}