-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
160 additions
and
373 deletions.
There are no files selected for viewing
28 changes: 14 additions & 14 deletions
28
src/main/java/com/zerozero/auth/application/AuthorizeOAuthUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
package com.zerozero.auth.application; | ||
|
||
import com.zerozero.auth.exception.AuthenticationErrorCode; | ||
import com.zerozero.auth.infrastructure.oauth.OAuthRestClient; | ||
import com.zerozero.auth.infrastructure.oauth.OAuthRestClientFactory; | ||
import com.zerozero.auth.infrastructure.oauth.common.Provider; | ||
import java.net.URI; | ||
import com.zerozero.auth.exception.AuthErrorType; | ||
import com.zerozero.auth.exception.AuthException; | ||
import com.zerozero.auth.infrastructure.oauth.core.Provider; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.net.URI; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Log4j2 | ||
public class AuthorizeOAuthUseCase { | ||
|
||
private final OAuthRestClientFactory oAuthRestClientFactory; | ||
private final OAuthRestClientFactory oAuthRestClientFactory; | ||
|
||
public URI getAuthorizeUrl(String providerName) { | ||
if (providerName == null || providerName.isEmpty()) { | ||
log.error("[AuthorizeOAuthService] Invalid provider name"); | ||
throw AuthenticationErrorCode.NOT_EXIST_PROVIDER.toException(); | ||
public URI getAuthorizeUrl(String providerName) { | ||
if (providerName == null || providerName.isEmpty()) { | ||
log.error("[AuthorizeOAuthService] Invalid provider name"); | ||
throw new AuthException(AuthErrorType.NOT_EXIST_PROVIDER); | ||
} | ||
Provider provider = Provider.valueOf(providerName.toUpperCase()); | ||
OAuthRestClient oAuthRestClient = oAuthRestClientFactory.getOAuthRestClient(provider); | ||
return oAuthRestClient.getAuthUrl(); | ||
} | ||
Provider provider = Provider.valueOf(providerName.toUpperCase()); | ||
OAuthRestClient oAuthRestClient = oAuthRestClientFactory.getOAuthRestClient(provider); | ||
return oAuthRestClient.getAuthUrl(); | ||
} | ||
} |
203 changes: 58 additions & 145 deletions
203
src/main/java/com/zerozero/auth/application/HandleOAuthLoginUseCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,162 +1,75 @@ | ||
package com.zerozero.auth.application; | ||
|
||
import com.zerozero.auth.application.HandleOAuthLoginUseCase.HandleOAuthLoginRequest; | ||
import com.zerozero.auth.application.HandleOAuthLoginUseCase.HandleOAuthLoginResponse; | ||
import com.zerozero.auth.application.HandleOAuthLoginUseCase.HandleOAuthLoginResponse.Token; | ||
import com.zerozero.auth.exception.AuthenticationErrorCode; | ||
import com.zerozero.auth.infrastructure.oauth.OAuthRestClient; | ||
import com.zerozero.auth.infrastructure.oauth.OAuthRestClientFactory; | ||
import com.zerozero.auth.infrastructure.oauth.common.OAuthAccessTokenResponse; | ||
import com.zerozero.auth.infrastructure.oauth.common.OAuthResourceResponse; | ||
import com.zerozero.auth.infrastructure.oauth.common.Provider; | ||
import com.zerozero.core.application.BaseRequest; | ||
import com.zerozero.core.application.BaseResponse; | ||
import com.zerozero.core.application.BaseUseCase; | ||
import com.zerozero.core.domain.entity.Status; | ||
import com.zerozero.core.domain.entity.User; | ||
import com.zerozero.core.domain.infra.repository.RefreshTokenRepository; | ||
import com.zerozero.core.domain.infra.repository.UserJPARepository; | ||
import com.zerozero.core.domain.vo.AccessToken; | ||
import com.zerozero.core.domain.vo.RefreshToken; | ||
import com.zerozero.core.exception.DomainException; | ||
import com.zerozero.core.exception.error.BaseErrorCode; | ||
import com.zerozero.auth.domain.repository.RefreshTokenRepository; | ||
import com.zerozero.auth.exception.AuthErrorType; | ||
import com.zerozero.auth.exception.AuthException; | ||
import com.zerozero.auth.infrastructure.oauth.core.OAuthAccessTokenResponse; | ||
import com.zerozero.auth.infrastructure.oauth.core.OAuthResourceResponse; | ||
import com.zerozero.auth.infrastructure.oauth.core.Provider; | ||
import com.zerozero.auth.presentation.response.LoginResponse; | ||
import com.zerozero.auth.presentation.response.TokenResponse; | ||
import com.zerozero.core.util.JwtUtil; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import com.zerozero.user.domain.model.User; | ||
import com.zerozero.user.domain.model.UserStatus; | ||
import com.zerozero.user.domain.repository.UserRepository; | ||
import com.zerozero.user.domain.response.UserResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.Setter; | ||
import lombok.ToString; | ||
import lombok.experimental.SuperBuilder; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
@Log4j2 | ||
public class HandleOAuthLoginUseCase implements BaseUseCase<HandleOAuthLoginRequest, HandleOAuthLoginResponse> { | ||
|
||
private final OAuthRestClientFactory oAuthRestClientFactory; | ||
|
||
private final UserJPARepository userJPARepository; | ||
|
||
private final JwtUtil jwtUtil; | ||
|
||
private final RefreshTokenRepository refreshTokenRepository; | ||
|
||
@Override | ||
public HandleOAuthLoginResponse execute(HandleOAuthLoginRequest request) { | ||
if (request == null || !request.isValid()) { | ||
log.error("[HandleOAuthLoginUseCase] Invalid request"); | ||
return HandleOAuthLoginResponse.builder() | ||
.success(false) | ||
.errorCode(HandleOAuthLoginErrorCode.NOT_EXIST_LOGIN_CONDITION) | ||
.build(); | ||
} | ||
Provider provider = Provider.valueOf(request.providerName.toUpperCase()); | ||
OAuthRestClient oAuthRestClient = oAuthRestClientFactory.getOAuthRestClient(provider); | ||
|
||
OAuthAccessTokenResponse oAuthAccessTokenResponse = oAuthRestClient.getAccessToken(request.code); | ||
if (oAuthAccessTokenResponse == null) { | ||
log.error("[HandleOAuthLoginUseCase] OAuth access token is null"); | ||
throw AuthenticationErrorCode.ACCESS_TOKEN_NOT_ISSUED.toException(); | ||
} | ||
OAuthResourceResponse oAuthResourceResponse = oAuthRestClient.getResource(oAuthAccessTokenResponse.accessToken()); | ||
if (oAuthResourceResponse == null) { | ||
log.error("[HandleOAuthLoginUseCase] OAuth resource is null"); | ||
throw AuthenticationErrorCode.NOT_EXIST_RESOURCE_RESPONSE.toException(); | ||
} | ||
|
||
String userEmail = oAuthResourceResponse.email(); | ||
User user = userJPARepository.findByEmail(userEmail); | ||
if (user == null) { | ||
User pendingUser = User.builder() | ||
.email(userEmail) | ||
.status(Status.PENDING) | ||
.build(); | ||
userJPARepository.save(pendingUser); | ||
return generateAndBuildResponse(pendingUser); | ||
} else { | ||
return generateAndBuildResponse(user); | ||
} | ||
} | ||
|
||
private HandleOAuthLoginResponse generateAndBuildResponse(User user) { | ||
AccessToken accessToken = jwtUtil.generateAccessToken(user); | ||
RefreshToken refreshToken = jwtUtil.generateRefreshToken(user); | ||
refreshTokenRepository.save(refreshToken.toEntity(user.getId())); | ||
|
||
return HandleOAuthLoginResponse.builder() | ||
.user(com.zerozero.core.domain.vo.User.of(user)) | ||
.token(Token.builder() | ||
.accessToken(accessToken) | ||
.refreshToken(refreshToken) | ||
.build()) | ||
.build(); | ||
} | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public enum HandleOAuthLoginErrorCode implements BaseErrorCode<DomainException> { | ||
NOT_EXIST_LOGIN_CONDITION(HttpStatus.BAD_REQUEST, "로그인 요청 조건이 존재하지 않습니다."), | ||
; | ||
|
||
private final HttpStatus httpStatus; | ||
|
||
private final String message; | ||
|
||
@Override | ||
public DomainException toException() { | ||
return new DomainException(httpStatus, this); | ||
public class HandleOAuthLoginUseCase { | ||
|
||
private final OAuthRestClientFactory oAuthRestClientFactory; | ||
|
||
private final UserRepository userRepository; | ||
|
||
private final JwtUtil jwtUtil; | ||
|
||
private final RefreshTokenRepository refreshTokenRepository; | ||
|
||
public LoginResponse execute(String code, String providerName) { | ||
Provider provider = Provider.valueOf(providerName.toUpperCase()); | ||
OAuthRestClient oAuthRestClient = oAuthRestClientFactory.getOAuthRestClient(provider); | ||
|
||
OAuthAccessTokenResponse oAuthAccessTokenResponse = oAuthRestClient.getAccessToken(code); | ||
if (oAuthAccessTokenResponse == null) { | ||
log.error("[HandleOAuthLoginUseCase] OAuth access token is null"); | ||
throw new AuthException(AuthErrorType.ACCESS_TOKEN_NOT_ISSUED); | ||
} | ||
|
||
OAuthResourceResponse oAuthResourceResponse = oAuthRestClient.getResource(oAuthAccessTokenResponse.accessToken()); | ||
if (oAuthResourceResponse == null) { | ||
log.error("[HandleOAuthLoginUseCase] OAuth resource is null"); | ||
throw new AuthException(AuthErrorType.NOT_EXIST_RESOURCE_RESPONSE); | ||
} | ||
|
||
String userEmail = oAuthResourceResponse.email(); | ||
User user = userRepository.findByEmail(userEmail).orElse(null); | ||
if (user == null) { | ||
User pendingUser = User.builder() | ||
.email(userEmail) | ||
.userStatus(UserStatus.PENDING) | ||
.build(); | ||
userRepository.save(pendingUser); | ||
return generateAndBuildResponse(pendingUser); | ||
} else { | ||
return generateAndBuildResponse(user); | ||
} | ||
} | ||
} | ||
|
||
@ToString | ||
@Getter | ||
@Setter | ||
@SuperBuilder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
public static class HandleOAuthLoginResponse extends BaseResponse<HandleOAuthLoginErrorCode> { | ||
|
||
private com.zerozero.core.domain.vo.User user; | ||
|
||
private Token token; | ||
|
||
@ToString | ||
@Getter | ||
@Setter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
public static class Token { | ||
|
||
private AccessToken accessToken; | ||
|
||
private RefreshToken refreshToken; | ||
private LoginResponse generateAndBuildResponse(User user) { | ||
String accessToken = jwtUtil.generateAccessToken(user); | ||
String refreshToken = jwtUtil.generateRefreshToken(user); | ||
refreshTokenRepository.save(com.zerozero.auth.domain.model.RefreshToken.builder() | ||
.userId(user.getId()) | ||
.refreshToken(refreshToken) | ||
.build()); | ||
return LoginResponse.of(UserResponse.from(user), TokenResponse.of(accessToken, refreshToken)); | ||
} | ||
} | ||
|
||
@ToString | ||
@Getter | ||
@Setter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor(access = AccessLevel.PROTECTED) | ||
public static class HandleOAuthLoginRequest implements BaseRequest { | ||
|
||
private String providerName; | ||
|
||
private String code; | ||
|
||
@Override | ||
public boolean isValid() { | ||
return providerName != null && !providerName.isEmpty() && code != null && !code.isEmpty(); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/zerozero/auth/application/OAuthRestClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.zerozero.auth.application; | ||
|
||
import com.zerozero.auth.infrastructure.oauth.core.OAuthAccessTokenResponse; | ||
import com.zerozero.auth.infrastructure.oauth.core.OAuthResourceResponse; | ||
|
||
import java.net.URI; | ||
|
||
public interface OAuthRestClient { | ||
|
||
URI getAuthUrl(); | ||
|
||
OAuthAccessTokenResponse getAccessToken(String authCode); | ||
|
||
OAuthResourceResponse getResource(String accessToken); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/zerozero/auth/application/OAuthRestClientFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.zerozero.auth.application; | ||
|
||
import com.zerozero.auth.exception.AuthErrorType; | ||
import com.zerozero.auth.exception.AuthException; | ||
import com.zerozero.auth.infrastructure.oauth.core.Provider; | ||
import com.zerozero.auth.infrastructure.oauth.kakao.KakaoOAuthRestClient; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class OAuthRestClientFactory { | ||
|
||
private final KakaoOAuthRestClient kakaoOAuthRestClient; | ||
|
||
public OAuthRestClient getOAuthRestClient(Provider provider) { | ||
switch (provider) { | ||
case KAKAO -> { | ||
return kakaoOAuthRestClient; | ||
} | ||
default -> throw new AuthException(AuthErrorType.INVALID_PROVIDER); | ||
} | ||
} | ||
} |
Oops, something went wrong.