diff --git a/src/main/java/sopt/makers/authentication/application/auth/api/AuthApi.java b/src/main/java/sopt/makers/authentication/application/auth/api/AuthApi.java index d228501..5bfa264 100644 --- a/src/main/java/sopt/makers/authentication/application/auth/api/AuthApi.java +++ b/src/main/java/sopt/makers/authentication/application/auth/api/AuthApi.java @@ -4,6 +4,7 @@ import sopt.makers.authentication.support.common.api.BaseResponse; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RequestHeader; public interface AuthApi { @@ -19,5 +20,12 @@ ResponseEntity> authenticateSocialAuthInfoFromWeb( ResponseEntity> authenticateSocialAuthInfoFromApp( AuthRequest.AuthenticateSocialAuthInfo socialAuthInfo); + ResponseEntity> refreshTokenFromApp( + AuthRequest.AuthenticationTokenInfo authenticationTokenInfo); + + ResponseEntity> refreshTokenFromWeb( + @RequestHeader("accessToken") String accessToken, + @RequestHeader("refreshToken") String refreshToken); + ResponseEntity> signUp(AuthRequest.SignUpInfo signUp); } diff --git a/src/main/java/sopt/makers/authentication/application/auth/api/AuthApiController.java b/src/main/java/sopt/makers/authentication/application/auth/api/AuthApiController.java index d397e88..ed22a50 100644 --- a/src/main/java/sopt/makers/authentication/application/auth/api/AuthApiController.java +++ b/src/main/java/sopt/makers/authentication/application/auth/api/AuthApiController.java @@ -16,6 +16,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @@ -77,10 +78,42 @@ public ResponseEntity> authenticateSocialAuthInfoFromApp( tokenInfo.accessToken(), tokenInfo.refreshToken())); } - @Override @PostMapping("/signup") public ResponseEntity> signUp(AuthRequest.SignUpInfo signUpInfo) { signUpUsecase.signUp(signUpInfo.toCommand()); return ResponseUtil.success(AuthSuccess.CREATE_SIGN_UP_USER); } + + @Override + @PostMapping("/refresh/app") + public ResponseEntity> refreshTokenFromApp( + AuthRequest.AuthenticationTokenInfo authenticationTokenInfo) { + + AuthenticateTokenInfo tokenInfo = + authenticateSocialAccountUsecase.refresh(authenticationTokenInfo.toCommand()); + + return ResponseUtil.success( + AuthSuccess.AUTHENTICATE_SOCIAL_ACCOUNT, + AuthResponse.AuthenticateSocialAuthInfoForApp.of( + tokenInfo.accessToken(), tokenInfo.refreshToken())); + } + + @Override + @PostMapping("/refresh/web") + public ResponseEntity> refreshTokenFromWeb( + @RequestHeader("accessToken") String accessToken, + @RequestHeader("refreshToken") String refreshToken) { + + AuthRequest.AuthenticationTokenInfo authenticationTokenInfo = + new AuthRequest.AuthenticationTokenInfo(accessToken, refreshToken); + + AuthenticateTokenInfo tokenInfo = + authenticateSocialAccountUsecase.refresh(authenticationTokenInfo.toCommand()); + HttpHeaders headers = cookieUtil.setRefreshToken(tokenInfo.refreshToken()); + + return ResponseUtil.success( + AuthSuccess.AUTHENTICATE_SOCIAL_ACCOUNT, + headers, + AuthResponse.AuthenticateSocialAuthInfoForWeb.of(tokenInfo.accessToken())); + } } diff --git a/src/main/java/sopt/makers/authentication/application/auth/dto/request/AuthRequest.java b/src/main/java/sopt/makers/authentication/application/auth/dto/request/AuthRequest.java index c132b34..300b21c 100644 --- a/src/main/java/sopt/makers/authentication/application/auth/dto/request/AuthRequest.java +++ b/src/main/java/sopt/makers/authentication/application/auth/dto/request/AuthRequest.java @@ -5,6 +5,7 @@ import sopt.makers.authentication.domain.auth.AuthPlatform; import sopt.makers.authentication.domain.auth.PhoneVerificationType; import sopt.makers.authentication.usecase.auth.port.in.AuthenticateSocialAccountUsecase.AuthenticateSocialAccountCommand; +import sopt.makers.authentication.usecase.auth.port.in.AuthenticateSocialAccountUsecase.AuthenticateTokenInfo; import sopt.makers.authentication.usecase.auth.port.in.CreatePhoneVerificationUsecase.CreateVerificationCommand; import sopt.makers.authentication.usecase.auth.port.in.SignUpUsecase.SignUpCommand; import sopt.makers.authentication.usecase.auth.port.in.VerifyPhoneVerificationUsecase.VerifyVerificationCommand; @@ -57,4 +58,10 @@ public SignUpCommand toCommand() { this.name, this.phone, this.token, AuthPlatform.find(this.authPlatform)); } } + + public record AuthenticationTokenInfo(String accessToken, String refreshToken) { + public AuthenticateTokenInfo toCommand() { + return AuthenticateTokenInfo.of(accessToken, refreshToken); + } + } } diff --git a/src/main/java/sopt/makers/authentication/support/jwt/provider/JwtAuthAccessTokenProvider.java b/src/main/java/sopt/makers/authentication/support/jwt/provider/JwtAuthAccessTokenProvider.java index e0880fd..bef0968 100644 --- a/src/main/java/sopt/makers/authentication/support/jwt/provider/JwtAuthAccessTokenProvider.java +++ b/src/main/java/sopt/makers/authentication/support/jwt/provider/JwtAuthAccessTokenProvider.java @@ -8,7 +8,6 @@ import sopt.makers.authentication.support.security.authentication.CustomAuthentication; import sopt.makers.authentication.support.value.JwtProperty; -import java.io.IOException; import java.time.Instant; import java.util.List; import java.util.stream.Collectors; @@ -53,7 +52,7 @@ public String generate(CustomAuthentication authentication) { } @Override - public CustomAuthentication parse(String requestToken) throws IOException { + public CustomAuthentication parse(String requestToken) { String token = extract(requestToken); Jwt accessToken = jwtDecoder.decode(token); JwtAccessToken jwtAccessToken = JwtAccessToken.createJwtAccessToken(accessToken); diff --git a/src/main/java/sopt/makers/authentication/usecase/auth/port/in/AuthenticateSocialAccountUsecase.java b/src/main/java/sopt/makers/authentication/usecase/auth/port/in/AuthenticateSocialAccountUsecase.java index 23ba671..68c2b9a 100644 --- a/src/main/java/sopt/makers/authentication/usecase/auth/port/in/AuthenticateSocialAccountUsecase.java +++ b/src/main/java/sopt/makers/authentication/usecase/auth/port/in/AuthenticateSocialAccountUsecase.java @@ -5,6 +5,8 @@ public interface AuthenticateSocialAccountUsecase { AuthenticateTokenInfo authenticate(AuthenticateSocialAccountCommand command); + AuthenticateTokenInfo refresh(AuthenticateTokenInfo command); + record AuthenticateTokenInfo(String accessToken, String refreshToken) { public static AuthenticateTokenInfo of(String accessToken, String refreshToken) { return new AuthenticateTokenInfo(accessToken, refreshToken); diff --git a/src/main/java/sopt/makers/authentication/usecase/auth/service/AuthenticateSocialAccountService.java b/src/main/java/sopt/makers/authentication/usecase/auth/service/AuthenticateSocialAccountService.java index 68f7a5c..26d4bd1 100644 --- a/src/main/java/sopt/makers/authentication/usecase/auth/service/AuthenticateSocialAccountService.java +++ b/src/main/java/sopt/makers/authentication/usecase/auth/service/AuthenticateSocialAccountService.java @@ -39,4 +39,17 @@ public AuthenticateTokenInfo authenticate(AuthenticateSocialAccountCommand comma return AuthenticateTokenInfo.of(accessToken, refreshToken); } + + @Override + public AuthenticateTokenInfo refresh(AuthenticateTokenInfo command) { + String refreshToken = command.refreshToken(); + + jwtAuthRefreshTokenProvider.parse(refreshToken); + CustomAuthentication customAuthentication = + jwtAuthAccessTokenProvider.parse(command.accessToken()); + + String renewedAccessToken = jwtAuthAccessTokenProvider.generate(customAuthentication); + String renewedRefreshToken = jwtAuthRefreshTokenProvider.generate(renewedAccessToken); + return AuthenticateTokenInfo.of(renewedAccessToken, renewedRefreshToken); + } }