Skip to content

Commit

Permalink
🚑 hotfix: Apple Login parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jun02160 committed Apr 15, 2024
1 parent f51a4ed commit 6aa9a9c
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.context.annotation.Configuration;

import feign.Logger;
import feign.Retryer;
import sopt.org.motivoo.external.MotivooExternalRoot;

@Configuration
Expand All @@ -14,4 +15,9 @@ public class FeignClientConfig {
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}

@Bean
public Retryer retryer() {
return new Retryer.Default(1000, 1500, 1);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package sopt.org.motivoo.external.client.auth.apple;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
public record ApplePublicKey(
String kty,
String kid,
String alg,
String n,
String e
) {

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Getter
public class ApplePublicKey {
private String kty;
private String kid;
private String use;
private String alg;
private String n;
private String e;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class ApplePublicKeys {

public ApplePublicKey getMatchesKey(String alg, String kid) {
return this.keys
.stream()
.filter(k -> k.getAlg().equals(alg) && k.getKid().equals(kid))
.findFirst()
.orElseThrow(() -> new BusinessException(INVALID_APPLE_PUBLIC_KEY));
.stream()
.filter(k -> k.alg().equals(alg) && k.kid().equals(kid))
.findFirst()
.orElseThrow(() -> new BusinessException(INVALID_APPLE_PUBLIC_KEY));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ public Map<String, String> parseHeaders(String identityToken) {
public Claims parsePublicKeyAndGetClaims(String idToken, PublicKey publicKey) {
try {
return Jwts.parserBuilder()
.setSigningKey(publicKey)
.build()
.parseClaimsJws(idToken)
.getBody();
.setSigningKey(publicKey)
.build()
.parseClaimsJws(idToken)
.getBody();

} catch (ExpiredJwtException e) {
throw new BusinessException(EXPIRED_APPLE_IDENTITY_TOKEN);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public OAuthPlatformMemberResult getApplePlatformMember(String identityToken) {

Claims claims = appleJwtParser.parsePublicKeyAndGetClaims(identityToken, publicKey);
validateClaims(claims);
log.info("sub: {}", claims.getSubject());
log.info(claims.toString());
return new OAuthPlatformMemberResult(claims.getSubject(), claims.get("email", String.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ public class PublicKeyGenerator {
//Apple Public Key를 바탕으로 Public Key를 생성
public PublicKey generatePublicKey(Map<String, String> headers, ApplePublicKeys applePublicKeys) {
ApplePublicKey applePublicKey =
applePublicKeys.getMatchesKey(headers.get(SIGN_ALGORITHM_HEADER_KEY), headers.get(KEY_ID_HEADER_KEY));
applePublicKeys.getMatchesKey(headers.get(SIGN_ALGORITHM_HEADER_KEY), headers.get(KEY_ID_HEADER_KEY));

return generatePublicKeyWithApplePublicKey(applePublicKey);
}

private PublicKey generatePublicKeyWithApplePublicKey(ApplePublicKey publicKey) {
byte[] nBytes = Base64.getUrlDecoder().decode(publicKey.getN());
byte[] eBytes = Base64.getUrlDecoder().decode(publicKey.getE());
byte[] nBytes = Base64.getUrlDecoder().decode(publicKey.n());
byte[] eBytes = Base64.getUrlDecoder().decode(publicKey.e());

BigInteger n = new BigInteger(POSITIVE_SIGN_NUMBER, nBytes);
BigInteger e = new BigInteger(POSITIVE_SIGN_NUMBER, eBytes);

RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(n, e);

try {
KeyFactory keyFactory = KeyFactory.getInstance(publicKey.getKty());
KeyFactory keyFactory = KeyFactory.getInstance(publicKey.kty());
return keyFactory.generatePublic(publicKeySpec);
} catch (NoSuchAlgorithmException | InvalidKeySpecException exception) {
throw new BusinessException(CREATE_PUBLIC_KEY_EXCEPTION);
Expand Down

0 comments on commit 6aa9a9c

Please sign in to comment.