-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 값이 올바르지 않습니다."); | ||
} | ||
} | ||
} | ||
|