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]: 액세스 토큰 발급 요청 수정 #65

Merged
merged 3 commits into from
Feb 8, 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Map;

@FeignClient(name = "apple-auth-client",url = "https://appleid.apple.com/auth")
public interface AppleAuthClient{
@GetMapping("/keys")
JwkResponse.JwkSet getKeys();

@GetMapping(value = "/token", consumes = "application/x-www-form-urlencoded")
TokenResponse getToken(MultiValueMap requestBody);
@PostMapping(value = "/token", consumes = "application/x-www-form-urlencoded")
TokenResponse getToken(@RequestBody Map<String, ?> requestBody);

//회원 탈퇴 메서드
// @GetMapping("/revoke")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@


import com.onnoff.onnoff.auth.feignClient.CustomErrorDecoder;
import feign.codec.Encoder;
import feign.codec.ErrorDecoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class FeignConfig {
@Bean
public ErrorDecoder errorDecoder(){
return new CustomErrorDecoder();
}
@Bean
public Encoder multipartFormEncoder() {
return new SpringFormEncoder(new SpringEncoder(() -> new HttpMessageConverters(new RestTemplate().getMessageConverters())));
}
}
25 changes: 13 additions & 12 deletions src/main/java/com/onnoff/onnoff/auth/service/AppleLoginService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
import com.onnoff.onnoff.domain.user.User;
import com.onnoff.onnoff.domain.user.enums.SocialType;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.MultiValueMap;

Expand Down Expand Up @@ -52,6 +50,7 @@ public class AppleLoginService implements LoginService{
public TokenResponse getAccessTokenByCode(String code) {
// client secret 만들기
String clientSecret = createClientSecret();
log.info("clientSecret = {}", clientSecret);
// 요청
MultiValueMap<String, String> urlEncoded = TokenRequest.builder()
.clientId(clientId)
Expand All @@ -63,21 +62,23 @@ public TokenResponse getAccessTokenByCode(String code) {
}
private String createClientSecret() {
Date expirationDate = Date.from(LocalDateTime.now().plusDays(30).atZone(ZoneId.systemDefault()).toInstant());
Map<String, Object> jwtHeader = new HashMap<>();
jwtHeader.put("kid", kid);
jwtHeader.put("alg", "ES256");

try {
log.info("teamId = {}", teamId);
log.info("kid = {}", kid);
return Jwts.builder()
.setHeaderParams(jwtHeader)
.setIssuer(teamId) // 토큰 발행자 = 우리 팀
.setIssuedAt(new Date(System.currentTimeMillis())) // 발행 시간 - UNIX 시간
.setExpiration(expirationDate) // 만료 시간
.setAudience(iss) // 애플이 수신자
.setSubject(clientId) // 토큰의 주체 = 우리 앱
.signWith(SignatureAlgorithm.ES256, getPrivateKey())
.header()
.keyId(kid)
.add("alg", "ES256")
.and()
.subject(clientId) // 토큰의 주체 = 우리 앱
.issuer(teamId)
.issuedAt(new Date(System.currentTimeMillis()))
.expiration(expirationDate) // 만료 시간
.audience()
.add(iss)
.and()
.signWith(getPrivateKey(), Jwts.SIG.ES256)
.compact();
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Loading