Implementation of refresh token.
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package org.sportshub.exposition.configuration;
|
||||
|
||||
import static org.springframework.http.HttpStatus.BAD_REQUEST;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
import static org.springframework.http.HttpStatus.UNAUTHORIZED;
|
||||
import org.sportshub.domain.exception.LoginFailureException;
|
||||
import org.sportshub.domain.exception.RefreshTokenDoesNotExistException;
|
||||
import org.sportshub.domain.exception.RefreshTokenExpiredException;
|
||||
import org.sportshub.domain.exception.UserDoesNotExistException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
@@ -14,4 +19,22 @@ public class GlobalControllerExceptionHandler {
|
||||
public void handleLoginFailureException() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
@ResponseStatus(NOT_FOUND)
|
||||
@ExceptionHandler(UserDoesNotExistException.class)
|
||||
public void handleUserDoesNotExistException() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
@ResponseStatus(NOT_FOUND)
|
||||
@ExceptionHandler(RefreshTokenDoesNotExistException.class)
|
||||
public void handleRefreshTokenDoesNotExistException() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
||||
@ResponseStatus(UNAUTHORIZED)
|
||||
@ExceptionHandler(RefreshTokenExpiredException.class)
|
||||
public void handleRefreshTokenExpiredException() {
|
||||
// Do nothing.
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +48,8 @@ public class SecurityConfiguration {
|
||||
).permitAll()
|
||||
.requestMatchers(
|
||||
POST,
|
||||
"/api/users/login"
|
||||
"/api/users/login",
|
||||
"/api/users/refresh-token"
|
||||
).permitAll()
|
||||
.requestMatchers(OPTIONS).permitAll()
|
||||
.anyRequest().authenticated()
|
||||
|
||||
@@ -4,7 +4,10 @@ import java.util.List;
|
||||
|
||||
import org.sportshub.application.user.UserUseCases;
|
||||
import org.sportshub.domain.user.model.User;
|
||||
import org.sportshub.domain.user.model.UserAuthenticationData;
|
||||
import org.sportshub.exposition.user.model.LoginRequest;
|
||||
import org.sportshub.exposition.user.model.LoginResponse;
|
||||
import org.sportshub.exposition.user.model.RefreshTokenRequest;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
@@ -21,12 +24,19 @@ public class UserController {
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public String login(@RequestBody LoginRequest request) {
|
||||
return userUseCases.authenticate(request.id(), request.password());
|
||||
public LoginResponse login(@RequestBody LoginRequest request) {
|
||||
UserAuthenticationData userAuthenticationData = userUseCases.authenticate(request.id(), request.password());
|
||||
return new LoginResponse(userAuthenticationData);
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<User> findAll() {
|
||||
return userUseCases.findAll();
|
||||
}
|
||||
|
||||
@PostMapping("/refresh-token")
|
||||
public LoginResponse refreshToken(@RequestBody RefreshTokenRequest request) {
|
||||
UserAuthenticationData userAuthenticationData = userUseCases.authenticate(request.refreshTokenValue());
|
||||
return new LoginResponse(userAuthenticationData);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.sportshub.exposition.user.model;
|
||||
|
||||
import org.sportshub.domain.user.model.UserAuthenticationData;
|
||||
|
||||
public record LoginResponse(
|
||||
String tokenType,
|
||||
String accessToken,
|
||||
String refreshToken
|
||||
) {
|
||||
public LoginResponse(UserAuthenticationData userAuthenticationData) {
|
||||
this(
|
||||
userAuthenticationData.tokenType(),
|
||||
userAuthenticationData.accessToken(),
|
||||
userAuthenticationData.refreshToken().value().toString()
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.sportshub.exposition.user.model;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record RefreshTokenRequest(
|
||||
UUID refreshTokenValue
|
||||
) {
|
||||
}
|
||||
Reference in New Issue
Block a user