Skip to content

Commit

Permalink
Merge pull request #79 from Modagbul/fix/auth
Browse files Browse the repository at this point in the history
refactor: 애플 키 가져오는 함수 confiuration으로 관리
  • Loading branch information
minsu20 authored Nov 18, 2023
2 parents e3cf2a3 + 5074a9b commit 32a7839
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ data.sql
*.html

firebase-key.json
apple-key.p8
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,14 @@
import com.moing.backend.domain.auth.application.service.WithdrawProvider;
import com.moing.backend.domain.auth.application.service.apple.utils.AppleClient;
import com.moing.backend.domain.auth.application.service.apple.utils.AppleToken;
import com.moing.backend.global.config.sns.AppleConfig;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.RequiredArgsConstructor;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.security.Key;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
Expand All @@ -37,10 +30,8 @@ public class AppleWithdrawUserCase implements WithdrawProvider {
@Value("${oauth2.apple.clientId}")
private String clientId;

@Value("${oauth2.apple.keyPath}")
private String keyPath;

private final AppleClient appleClient;
private final AppleConfig appleConfig;

public void withdraw(String token) throws IOException {
AppleToken.Response response = generateAuthToken(token);
Expand Down Expand Up @@ -78,18 +69,7 @@ public String createClientSecret() throws IOException {
.setExpiration(expirationDate)
.setAudience("https://appleid.apple.com")
.setSubject(clientId)
.signWith(getPrivateKey(), SignatureAlgorithm.ES256)
.signWith(appleConfig.applePrivateKey(), SignatureAlgorithm.ES256)
.compact();
}

private Key getPrivateKey() throws IOException {
ClassPathResource resource = new ClassPathResource(keyPath);
String privateKey = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));

Reader pemReader = new StringReader(privateKey);
PEMParser pemParser = new PEMParser(pemReader);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PrivateKeyInfo object = (PrivateKeyInfo) pemParser.readObject();
return converter.getPrivateKey(object);
}
}
42 changes: 42 additions & 0 deletions src/main/java/com/moing/backend/global/config/sns/AppleConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.moing.backend.global.config.sns;

import com.moing.backend.global.config.fcm.exception.InitializeException;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.FileCopyUtils;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.security.PrivateKey;

@Configuration
public class AppleConfig {


@Value("${oauth2.apple.keyPath}")
private String keyPath;

@Bean
public PrivateKey applePrivateKey(){
try{
ClassPathResource resource = new ClassPathResource(keyPath);
String privateKeyString = new String(FileCopyUtils.copyToByteArray(resource.getInputStream()));
Reader reader = new StringReader(privateKeyString);
PEMParser pemParser = new PEMParser(reader);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PrivateKeyInfo object = (PrivateKeyInfo) pemParser.readObject();
return converter.getPrivateKey(object);
} catch (FileNotFoundException e) {
throw new IllegalStateException("파일을 찾을 수 없습니다." + e.getMessage());
} catch (IOException e) {
throw new InitializeException();
}
}
}

0 comments on commit 32a7839

Please sign in to comment.