-
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
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/trothly/trothcam/feign/PublicKeyGenerator.java
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,48 @@ | ||
package trothly.trothcam.feign; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.Base64Utils; | ||
import trothly.trothcam.dto.auth.apple.ApplePublicKey; | ||
import trothly.trothcam.dto.auth.apple.ApplePublicKeys; | ||
|
||
import java.math.BigInteger; | ||
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.Map; | ||
|
||
@Component | ||
public class PublicKeyGenerator { | ||
|
||
private static final String SIGN_ALGORITHM_HEADER_KEY = "alg"; | ||
private static final String KEY_ID_HEADER_KEY = "kid"; | ||
private static final int POSITIVE_SIGN_NUMBER = 1; | ||
|
||
// 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)); | ||
|
||
return generatePublicKeyWithApplePublicKey(applePublicKey); | ||
} | ||
|
||
// Public Key 속 정보로 Public Key 생성 | ||
private PublicKey generatePublicKeyWithApplePublicKey(ApplePublicKey publicKey) { | ||
byte[] nBytes = Base64Utils.decodeFromUrlSafeString(publicKey.getN()); | ||
byte[] eBytes = Base64Utils.decodeFromUrlSafeString(publicKey.getE()); | ||
|
||
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()); | ||
return keyFactory.generatePublic(publicKeySpec); | ||
} catch (NoSuchAlgorithmException | InvalidKeySpecException exception) { | ||
throw new IllegalStateException("Apple OAuth 로그인 중 public key 생성에 문제가 발생했습니다."); | ||
} | ||
} | ||
} |