-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSystemAppleAuthenticator.java
More file actions
144 lines (120 loc) ยท 5.57 KB
/
SystemAppleAuthenticator.java
File metadata and controls
144 lines (120 loc) ยท 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package ssu.eatssu.domain.auth.entity;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import ssu.eatssu.domain.auth.dto.AppleKeys;
import ssu.eatssu.domain.auth.dto.OAuthInfo;
import ssu.eatssu.global.handler.response.BaseException;
import java.math.BigInteger;
import java.net.URI;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPublicKeySpec;
import java.util.Base64;
import java.util.Map;
import static ssu.eatssu.global.handler.response.BaseResponseStatus.*;
@Component
@RequiredArgsConstructor
public class SystemAppleAuthenticator implements AppleAuthenticator {
private final RestTemplate restTemplate;
public OAuthInfo getOAuthInfoByIdentityToken(String identityToken) {
PublicKey publicKey = generatePublicKey(identityToken);
return getOAuthInfoByPublicKey(identityToken, publicKey);
}
/**
* ์ ํ ๋ก๊ทธ์ธ - PublicKey ๋ฅผ ํตํด ์ ์ ์ ๋ณด(providerId, email) ์กฐํ
*/
private OAuthInfo getOAuthInfoByPublicKey(String identityToken, PublicKey publicKey) {
// identityToken ์์ publicKey ์๋ช
์ ํตํด Claims ๋ฅผ ์ถ์ถํ๋ค.
Claims claims = Jwts.parserBuilder()
.setSigningKey(publicKey)
.build()
.parseClaimsJws(identityToken)
.getBody();
Object emailObj = claims.get("email");
Object providerIdObj = claims.get("sub");
if (providerIdObj == null) {
throw new BaseException(NOT_FOUND_PROVIDER_ID);
}
if (emailObj == null) {
throw new BaseException(NOT_FOUND_EMAIL);
}
try {
String email = emailObj.toString();
String providerId = providerIdObj.toString();
return new OAuthInfo(email, providerId);
} catch (ExpiredJwtException exception) {
throw new BaseException(INVALID_IDENTITY_TOKEN);
}
}
private PublicKey generatePublicKey(String identityToken) {
//PublicKey ๋ฅผ ๋ง๋ค๊ธฐ ์ํ ์ฌ๋ฃ๊ฐ ๋๋ ํ๋ณด Key ๋ชฉ๋ก์ ๊ฐ์ ธ์จ๋ค.
AppleKeys keys = getAppleKeys();
//ํ๋ณด Key ์์ ์ ๋ต Key ๋ฅผ ์ฐพ๋๋ค.
AppleKeys.Key matchedKey = selectMatchedKey(identityToken, keys);
//์ ๋ต Key ๋ฅผ ํตํด PublicKey ๋ฅผ ์์ฑํ๋ค.
return generatePublicKeyWithApplePublicKey(matchedKey);
}
/**
* ์ ํ ๋ก๊ทธ์ธ - ํค๋์์ ๋ฝ์ ์ ๋ณด๋ฅผ ํตํด ํ๋ณด Key ์์ ์ ๋ต Key ๋ฅผ ์ฐพ์์ ๋ฐํํ๋ค.
*/
private AppleKeys.Key selectMatchedKey(String identityToken, AppleKeys candidateKeys) {
//identity token ์์ header ๋ฅผ ๋ฝ์์ decode ํ๋ค.
String header = identityToken.split("\\.")[0];
String decodedHeader = new String(Base64.getDecoder().decode(header));
//decode ๋ header ์ ๋ณด๋ฅผ ํตํด ์ ๋ตํค์ key id, algorithm ์ ๋ณด๋ฅผ ๊ฐ์ ธ์จ๋ค.
Map<String, String> headerMap;
try {
headerMap = new ObjectMapper().readValue(decodedHeader,
new TypeReference<Map<String, String>>() {
});
} catch (JsonProcessingException e) {
throw new BaseException(INVALID_IDENTITY_TOKEN);
}
//ํ๋ณดํค ์ค์์ ์ ๋ตํค๋ฅผ ์ฐพ์์ ๋ฐํํ๋ค.
return candidateKeys.findKeyBy(headerMap.get("kid"), headerMap.get("alg"))
.orElseThrow(() -> new BaseException(INVALID_IDENTITY_TOKEN));
}
/**
* ์ ํ ๋ก๊ทธ์ธ - ์ ๋ต Key ๋ฅผ ํตํด PublicKey ๋ฅผ ์์ฑํ๋ค.
*/
private PublicKey generatePublicKeyWithApplePublicKey(AppleKeys.Key matchedKey) {
//์ ๋ต ํค์์ PublicKey ์ ์ฌ๋ฃ๊ฐ ๋ n, e ๊ฐ์ ๊ฐ์ ธ์จ๋ค.
byte[] nBytes = Base64.getUrlDecoder().decode(matchedKey.getN());
byte[] eBytes = Base64.getUrlDecoder().decode(matchedKey.getE());
BigInteger n = new BigInteger(1, nBytes);
BigInteger e = new BigInteger(1, eBytes);
// n, e ๊ฐ์ ํตํด PublicKeySpec ์ ์ธํ
ํ๋ค.
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(n, e);
//PublicKeySpec ์ ํตํด PublicKey ๋ฅผ ์์ฑํ๋ค.
try {
KeyFactory keyFactory = KeyFactory.getInstance(matchedKey.getKty());
return keyFactory.generatePublic(publicKeySpec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
throw new BaseException(INVALID_IDENTITY_TOKEN);
}
}
/**
* ์ ํ ๋ก๊ทธ์ธ - Apple api ํธ์ถ์ ํตํด apple ํ๋ณด key ๋ฆฌ์คํธ๋ฅผ ๋ฐ์์จ๋ค.
*/
private AppleKeys getAppleKeys() {
URI uri = UriComponentsBuilder
.fromUriString("https://appleid.apple.com")
.path("/auth/keys")
.encode()
.build()
.toUri();
ResponseEntity<AppleKeys> response = restTemplate.getForEntity(uri, AppleKeys.class);
return response.getBody();
}
}