Skip to content

Commit 7b0b6bc

Browse files
committed
feat: 예외 로직 분리
1 parent d66e77a commit 7b0b6bc

2 files changed

Lines changed: 32 additions & 14 deletions

File tree

src/main/java/ssu/eatssu/domain/auth/entity/SystemAppleAuthenticator.java

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import io.jsonwebtoken.ExpiredJwtException;
88
import io.jsonwebtoken.Jwts;
99
import lombok.RequiredArgsConstructor;
10+
import lombok.extern.slf4j.Slf4j;
1011
import org.springframework.http.ResponseEntity;
1112
import org.springframework.stereotype.Component;
1213
import org.springframework.web.client.RestTemplate;
1314
import org.springframework.web.util.UriComponentsBuilder;
1415
import ssu.eatssu.domain.auth.dto.AppleKeys;
1516
import ssu.eatssu.domain.auth.dto.OAuthInfo;
17+
import ssu.eatssu.domain.user.repository.UserRepository;
1618
import ssu.eatssu.global.handler.response.BaseException;
1719

1820
import java.math.BigInteger;
@@ -29,9 +31,11 @@
2931

3032
@Component
3133
@RequiredArgsConstructor
34+
@Slf4j
3235
public class SystemAppleAuthenticator implements AppleAuthenticator {
3336

3437
private final RestTemplate restTemplate;
38+
private final UserRepository userRepository;
3539

3640
public OAuthInfo getOAuthInfoByIdentityToken(String identityToken) {
3741
PublicKey publicKey = generatePublicKey(identityToken);
@@ -42,30 +46,43 @@ public OAuthInfo getOAuthInfoByIdentityToken(String identityToken) {
4246
* 애플 로그인 - PublicKey 를 통해 유저 정보(providerId, email) 조회
4347
*/
4448
private OAuthInfo getOAuthInfoByPublicKey(String identityToken, PublicKey publicKey) {
45-
// identityToken 에서 publicKey 서명을 통해 Claims 를 추출한다.
46-
Claims claims = Jwts.parserBuilder()
47-
.setSigningKey(publicKey)
48-
.build()
49-
.parseClaimsJws(identityToken)
50-
.getBody();
49+
Claims claims;
50+
try {
51+
claims = Jwts.parserBuilder()
52+
.setSigningKey(publicKey)
53+
.build()
54+
.parseClaimsJws(identityToken)
55+
.getBody();
56+
} catch (ExpiredJwtException exception) {
57+
throw new BaseException(INVALID_IDENTITY_TOKEN);
58+
}
5159

5260
Object emailObj = claims.get("email");
5361
Object providerIdObj = claims.get("sub");
5462

5563
if (providerIdObj == null) {
5664
throw new BaseException(NOT_FOUND_PROVIDER_ID);
5765
}
66+
67+
String providerId = providerIdObj.toString();
68+
69+
// email 없는 경우 → Apple 재로그인 케이스 검증 (Apple 스펙상 최초 로그인 시에만 email 포함)
5870
if (emailObj == null) {
59-
throw new BaseException(NOT_FOUND_EMAIL);
71+
boolean existsUser = userRepository.findByProviderId(providerId).isPresent();
72+
73+
if (existsUser) {
74+
// 가설 맞음: 기존 유저 재로그인 케이스
75+
log.info("[Apple Login] email claim 없음 & DB 유저 있음. 재로그인 케이스로 확인. providerId={}", providerId);
76+
throw new BaseException(NOT_FOUND_EMAIL);
77+
} else {
78+
// 다른 원인: 신규 유저인데 email 없음 → 슬랙 알럿용 별도 에러코드
79+
log.warn("[Apple Login] email claim 없음 & DB 유저 없음. 원인 불명. providerId={}", providerId);
80+
throw new BaseException(NOT_FOUND_APPLE_EMAIL_NEW_USER);
81+
}
6082
}
6183

62-
try {
63-
String email = emailObj.toString();
64-
String providerId = providerIdObj.toString();
65-
return new OAuthInfo(email, providerId);
66-
} catch (ExpiredJwtException exception) {
67-
throw new BaseException(INVALID_IDENTITY_TOKEN);
68-
}
84+
String email = emailObj.toString();
85+
return new OAuthInfo(email, providerId);
6986
}
7087

7188
private PublicKey generatePublicKey(String identityToken) {

src/main/java/ssu/eatssu/global/handler/response/BaseResponseStatus.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public enum BaseResponseStatus {
7777
INVALID_NICKNAME(false, HttpStatus.NOT_FOUND, 40412, "잘못된 닉네임입니다."),
7878
NOT_FOUND_PROVIDER_ID(false, HttpStatus.NOT_FOUND, 40413, "Claims에서 ProviderId(sub)를 찾을 수 없습니다."),
7979
NOT_FOUND_EMAIL(false, HttpStatus.NOT_FOUND, 40414, "Claims에서 이메일을 찾을 수 없습니다."),
80+
NOT_FOUND_APPLE_EMAIL_NEW_USER(false, HttpStatus.NOT_FOUND, 40415, "신규 Apple 유저인데 email claim이 없습니다."),
8081

8182
/**
8283
* 405 METHOD_NOT_ALLOWED 지원하지 않은 method 호출

0 commit comments

Comments
 (0)