Skip to content

Commit 9e8cb85

Browse files
authored
Merge pull request #347 from EAT-SSU/fix/#346-apple-login-error
feat: ์˜ˆ์™ธ ๋กœ์ง ๋ถ„๋ฆฌ
2 parents d66e77a + e2e1ad3 commit 9e8cb85

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.warn("[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)