Skip to content

Commit

Permalink
feat(#5) : identity token 파싱
Browse files Browse the repository at this point in the history
  • Loading branch information
aeeazip committed Jul 2, 2023
1 parent 5de7d8a commit 175417b
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/main/java/trothly/trothcam/feign/AppleJwtParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package trothly.trothcam.feign;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.jsonwebtoken.*;
import org.springframework.stereotype.Component;
import org.springframework.util.Base64Utils;
import trothly.trothcam.exception.custom.InvalidTokenException;
import trothly.trothcam.exception.custom.TokenExpiredException;

import java.security.PublicKey;
import java.util.Map;

@Component
public class AppleJwtParser {

private static final String IDENTITY_TOKEN_VALUE_DELIMITER = "\\.";
private static final int HEADER_INDEX = 0;

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

public Map<String, String> parseHeaders(String identityToken) {
try {
String encodedHeader = identityToken.split(IDENTITY_TOKEN_VALUE_DELIMITER)[HEADER_INDEX];
String decodedHeader = new String(Base64Utils.decodeFromUrlSafeString(encodedHeader));
return OBJECT_MAPPER.readValue(decodedHeader, Map.class);
} catch (JsonProcessingException | ArrayIndexOutOfBoundsException e) {
throw new InvalidTokenException("Apple OAuth Identity Token 형식이 올바르지 않습니다.");
}
}

public Claims parsePublicKeyAndGetClaims(String idToken, PublicKey publicKey) {
try {
return Jwts.parser()
.setSigningKey(publicKey)
.parseClaimsJws(idToken)
.getBody();
} catch (ExpiredJwtException e) {
throw new TokenExpiredException("Apple OAuth 로그인 중 Identity Token 유효기간이 만료됐습니다.");
} catch (UnsupportedJwtException | MalformedJwtException | SignatureException | IllegalArgumentException e) {
throw new InvalidTokenException("Apple OAuth Identity Token 값이 올바르지 않습니다.");
}
}
}

0 comments on commit 175417b

Please sign in to comment.