Skip to content

Commit

Permalink
[MERGE] feat/#31 -> dev
Browse files Browse the repository at this point in the history
[FEAT/#31] 토큰 Refresh API 구현
  • Loading branch information
hyunw9 authored Jan 10, 2025
2 parents 42ad8ed + e68e168 commit 2746db0
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -19,5 +20,12 @@ ResponseEntity<BaseResponse<?>> authenticateSocialAuthInfoFromWeb(
ResponseEntity<BaseResponse<?>> authenticateSocialAuthInfoFromApp(
AuthRequest.AuthenticateSocialAuthInfo socialAuthInfo);

ResponseEntity<BaseResponse<?>> refreshTokenFromApp(
AuthRequest.AuthenticationTokenInfo authenticationTokenInfo);

ResponseEntity<BaseResponse<?>> refreshTokenFromWeb(
@RequestHeader("accessToken") String accessToken,
@RequestHeader("refreshToken") String refreshToken);

ResponseEntity<BaseResponse<?>> signUp(AuthRequest.SignUpInfo signUp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -77,10 +78,42 @@ public ResponseEntity<BaseResponse<?>> authenticateSocialAuthInfoFromApp(
tokenInfo.accessToken(), tokenInfo.refreshToken()));
}

@Override
@PostMapping("/signup")
public ResponseEntity<BaseResponse<?>> signUp(AuthRequest.SignUpInfo signUpInfo) {
signUpUsecase.signUp(signUpInfo.toCommand());
return ResponseUtil.success(AuthSuccess.CREATE_SIGN_UP_USER);
}

@Override
@PostMapping("/refresh/app")
public ResponseEntity<BaseResponse<?>> 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<BaseResponse<?>> 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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 2746db0

Please sign in to comment.