-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
229 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 13 additions & 5 deletions
18
src/main/java/umc/th/juinjang/model/dto/auth/apple/AppleClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,26 @@ | ||
package umc.th.juinjang.model.dto.auth.apple; | ||
|
||
|
||
import org.springframework.cloud.openfeign.EnableFeignClients; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import umc.th.juinjang.JuinjangApplication; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE; | ||
|
||
@FeignClient(name = "appleClient", url = "https://appleid.apple.com/auth") // configuration 속성 제거함 | ||
|
||
public interface AppleClient { | ||
@GetMapping(value = "/keys") | ||
ApplePublicKeyResponse getAppleAuthPublicKey(); | ||
|
||
@PostMapping(value = "/token", consumes = APPLICATION_FORM_URLENCODED_VALUE) | ||
AppleTokenResponse getAppleTokens(@RequestBody AppleTokenRequest request); | ||
|
||
|
||
@PostMapping(value = "/revoke", consumes = APPLICATION_FORM_URLENCODED_VALUE) | ||
void revoke(@RequestPart(value = "token") String token, | ||
@RequestPart(value = "client_id") String client_id, | ||
@RequestPart(value = "client_secret") String client_secret, | ||
@RequestPart(value = "token_type_hint") String token_type_hint); | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/umc/th/juinjang/model/dto/auth/apple/AppleClientSecretGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package umc.th.juinjang.model.dto.auth.apple; | ||
|
||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.util.Date; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class AppleClientSecretGenerator { | ||
|
||
private static final String AUDIENCE = "https://appleid.apple.com"; | ||
private final ApplePrivateKeyGenerator applePrivateKeyGenerator; | ||
|
||
@Value("${apple.key.id}") | ||
private String keyId; | ||
@Value("${apple.team-id}") | ||
private String teamId; | ||
@Value("${apple.aud}") | ||
private String clientId; | ||
|
||
public String generateClientSecret() throws IOException { | ||
Date expirationDate = Date.from(LocalDateTime.now().plusDays(5) | ||
.atZone(ZoneId.systemDefault()).toInstant()); | ||
return Jwts.builder() | ||
.setHeaderParam("alg", SignatureAlgorithm.ES256) | ||
.setHeaderParam("kid", keyId) | ||
.setIssuer(teamId) | ||
.setIssuedAt(new Date(System.currentTimeMillis())) | ||
.setExpiration(expirationDate) | ||
.setAudience(AUDIENCE) | ||
.setSubject(clientId) | ||
.signWith(applePrivateKeyGenerator.getPrivateKey(), SignatureAlgorithm.ES256) | ||
.compact(); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
src/main/java/umc/th/juinjang/model/dto/auth/apple/AppleOAuthProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package umc.th.juinjang.model.dto.auth.apple; | ||
|
||
|
||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
import umc.th.juinjang.apiPayload.exception.handler.MemberHandler; | ||
|
||
import static umc.th.juinjang.apiPayload.code.status.ErrorStatus.FAILED_TO_LOAD_PRIVATE_KEY; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class AppleOAuthProvider { | ||
|
||
@Value("${apple.aud}") | ||
private String clientId; | ||
private final String GRANTTYPE = "authorization_code"; | ||
|
||
private final AppleClient appleClient; | ||
|
||
public String getAppleRefreshToken(final String code, final String clientSecret) { | ||
try { | ||
AppleTokenRequest appleTokenRequest = AppleTokenRequest.builder() | ||
.client_id(clientId) | ||
.client_secret(clientSecret) | ||
.grant_type(GRANTTYPE) | ||
.code(code).build(); | ||
AppleTokenResponse appleTokenResponse = appleClient.getAppleTokens(appleTokenRequest); | ||
log.info("Apple token response: {}", appleTokenResponse); | ||
return appleTokenResponse.refreshToken(); | ||
} catch (Exception e) { | ||
log.error("Failed to get apple refresh token."); | ||
throw new MemberHandler(FAILED_TO_LOAD_PRIVATE_KEY); | ||
} | ||
} | ||
|
||
public void requestRevoke(final String refreshToken, final String clientSecret) { | ||
appleClient.revoke(clientSecret,refreshToken,clientId, "refresh_token"); | ||
log.error("Failed to revoke apple refresh token."); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/umc/th/juinjang/model/dto/auth/apple/ApplePrivateKeyGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package umc.th.juinjang.model.dto.auth.apple; | ||
|
||
import org.apache.commons.codec.binary.Base64; | ||
import org.springframework.core.io.ClassPathResource; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.security.KeyFactory; | ||
import java.security.PrivateKey; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
|
||
@Component | ||
public class ApplePrivateKeyGenerator { | ||
|
||
public PrivateKey getPrivateKey() throws IOException { | ||
// .p8 파일의 경로를 가져옴. | ||
ClassPathResource resource = new ClassPathResource("AUTHKEY_JUINJAG.p8"); | ||
|
||
// 파일의 내용을 String으로 읽어옴. | ||
String privateKeyContent = new String(Files.readAllBytes(Paths.get(resource.getURI()))); | ||
|
||
// PEM 파일의 헤더와 푸터를 제거하고 Base64 인코딩된 문자열을 추출. | ||
String privateKeyPEM = privateKeyContent | ||
.replace("-----BEGIN PRIVATE KEY-----", "") | ||
.replace("-----END PRIVATE KEY-----", "") | ||
.replaceAll("\\s+", ""); | ||
|
||
// Base64로 인코딩된 문자열을 디코딩. | ||
byte[] encoded = Base64.decodeBase64(privateKeyPEM); | ||
|
||
// PKCS8EncodedKeySpec을 생성. | ||
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded); | ||
|
||
try { | ||
// KeyFactory를 사용하여 PrivateKey 객체를 생성 | ||
KeyFactory keyFactory = KeyFactory.getInstance("EC"); // 키 타입에 따라 "RSA" 또는 "EC"를 사용. | ||
return keyFactory.generatePrivate(keySpec); | ||
} catch (Exception e) { | ||
throw new IOException("Failed to convert private key.", e); | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/umc/th/juinjang/model/dto/auth/apple/AppleTokenRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package umc.th.juinjang.model.dto.auth.apple; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
@Getter | ||
@Builder | ||
@ToString | ||
public class AppleTokenRequest { | ||
private String client_id; | ||
private String client_secret; | ||
private String code; //authorization code | ||
private String grant_type; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/umc/th/juinjang/model/dto/auth/apple/AppleTokenResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package umc.th.juinjang.model.dto.auth.apple; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public record AppleTokenResponse( @JsonProperty(value = "access_token") | ||
@Schema(description = "애플 access_token") String accessToken, | ||
|
||
@JsonProperty(value = "expires_in") | ||
@Schema(description = "애플 토큰 만료 기한 expires_in") String expiresIn, | ||
|
||
@JsonProperty(value = "id_token") | ||
@Schema(description = "애플 id_token") String idToken, | ||
|
||
@JsonProperty(value = "refresh_token") | ||
@Schema(description = "애플 token_tyoe") String refreshToken, | ||
|
||
@Schema(description = "error") String error) { | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters