Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: 생성자 주입 수정 #171

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/java/com/readyvery/readyverydemo/config/OauthConfig.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
package com.readyvery.readyverydemo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
@Getter
public class OauthConfig {

@Value("${app.apple.url}")
private String appleUrl;

@Value("${app.apple.private-key}")
private String privateKeyString;
@Value("${app.apple.client-id}")
private String appleClientId;

@Value("${app.apple.team-id}")
private String appleTeamId;

@Value("${app.apple.key-id}")
private String appleKeyId;
public static final String KAKAO_NAME = "kakao";
public static final String APPLE_NAME = "apple";
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
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.convert.converter.Converter;
import org.springframework.http.RequestEntity;
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequest;
import org.springframework.security.oauth2.client.endpoint.OAuth2AuthorizationCodeGrantRequestEntityConverter;
import org.springframework.util.MultiValueMap;

import com.readyvery.readyverydemo.config.OauthConfig;

import io.jsonwebtoken.Jwts;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -27,26 +28,14 @@
@Getter
public class CustomRequestEntityConverter implements Converter<OAuth2AuthorizationCodeGrantRequest, RequestEntity<?>> {

private OAuth2AuthorizationCodeGrantRequestEntityConverter defaultConverter;
private final OAuth2AuthorizationCodeGrantRequestEntityConverter defaultConverter;
private final OauthConfig oauthConfig;

public CustomRequestEntityConverter() {
defaultConverter = new OAuth2AuthorizationCodeGrantRequestEntityConverter();
oauthConfig = new OauthConfig();
}

@Value("${app.apple.url}")
private String appleUrl;

@Value("${app.apple.private-key}")
private String privateKeyString;
@Value("${app.apple.client-id}")
private String appleClientId;

@Value("${app.apple.team-id}")
private String appleTeamId;

@Value("${app.apple.key-id}")
private String appleKeyId;

@Override
public RequestEntity<?> convert(OAuth2AuthorizationCodeGrantRequest req) {
RequestEntity<?> entity = defaultConverter.convert(req);
Expand All @@ -64,7 +53,7 @@ public RequestEntity<?> convert(OAuth2AuthorizationCodeGrantRequest req) {
}

public PrivateKey getPrivateKey() throws IOException {
PEMParser pemParser = new PEMParser(new StringReader(privateKeyString));
PEMParser pemParser = new PEMParser(new StringReader(oauthConfig.getPrivateKeyString()));
PrivateKeyInfo object = (PrivateKeyInfo)pemParser.readObject();
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
return converter.getPrivateKey(object);
Expand All @@ -73,16 +62,16 @@ public PrivateKey getPrivateKey() throws IOException {
public String createClientSecret() throws IOException {
Date expirationDate = Date.from(LocalDateTime.now().plusDays(30).atZone(ZoneId.systemDefault()).toInstant());
Map<String, Object> jwtHeader = new HashMap<>();
jwtHeader.put("kid", appleKeyId);
jwtHeader.put("kid", oauthConfig.getAppleKeyId());
jwtHeader.put("alg", "ES256");

return Jwts.builder()
.setHeaderParams(jwtHeader)
.setIssuer(appleTeamId)
.setIssuer(oauthConfig.getAppleTeamId())
.setIssuedAt(new Date(System.currentTimeMillis())) // 발행 시간 - UNIX 시간
.setExpiration(expirationDate) // 만료 시간
.setAudience(appleUrl)
.setSubject(appleClientId)
.setAudience(oauthConfig.getAppleUrl())
.setSubject(oauthConfig.getAppleClientId())
.signWith(getPrivateKey())
.compact();
}
Expand Down
Loading