Skip to content

Commit

Permalink
feat(#5) : PublicKey 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
aeeazip committed Jul 2, 2023
1 parent c2c3f7b commit 5de7d8a
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/main/java/trothly/trothcam/feign/PublicKeyGenerator.java
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 생성에 문제가 발생했습니다.");
}
}
}

0 comments on commit 5de7d8a

Please sign in to comment.