Skip to content

Commit

Permalink
feat: get private key through bouncycastle
Browse files Browse the repository at this point in the history
  • Loading branch information
Kang1221 committed Sep 12, 2024
1 parent b3e0ef3 commit 615751c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion DDANZI_Server_yml
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ dependencies {
// Google Firebase Admin
implementation 'com.google.firebase:firebase-admin:9.2.0'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation group: 'org.bouncycastle', name: 'bcpkix-jdk15to18', version: '1.78.1' // 최신 버전 확인 후 사용

}

tasks.named('test') {
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/co/orange/ddanzi/service/auth/OAuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
Expand All @@ -28,9 +30,8 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.Security;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
Expand Down Expand Up @@ -180,13 +181,14 @@ private String generateClientSecret() {
}

private PrivateKey getPrivateKey() {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC");

try {
byte[] privateKeyBytes = Base64.getDecoder().decode(appleProperties.getPrivateKey());

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("EC"); // Elliptic Curve 키 타입 (ES256에 맞게)

return keyFactory.generatePrivate(keySpec);
PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(privateKeyBytes);
return converter.getPrivateKey(privateKeyInfo);
} catch (Exception e) {
throw new RuntimeException("Error converting private key from String", e);
}
Expand Down

0 comments on commit 615751c

Please sign in to comment.