-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEAT/#54] 애플 로그인 구현
- Loading branch information
Showing
9 changed files
with
148 additions
and
1 deletion.
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
7 changes: 7 additions & 0 deletions
7
src/main/java/com/acon/server/global/config/OpenFeignConfig.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,12 +1,19 @@ | ||
package com.acon.server.global.config; | ||
|
||
import com.acon.server.ServerApplication; | ||
import feign.Retryer; | ||
import feign.Retryer.Default; | ||
import org.springframework.cloud.openfeign.EnableFeignClients; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@EnableFeignClients(basePackageClasses = ServerApplication.class) | ||
public class OpenFeignConfig { | ||
|
||
@Bean | ||
public Retryer retryer() { | ||
return new Default(1000, 1500, 2); | ||
} | ||
// TODO: 타임아웃 설정 추가, 서킷 브레이커 적용하기 | ||
} |
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
24 changes: 24 additions & 0 deletions
24
src/main/java/com/acon/server/member/infra/external/ios/AppleAuthAdapter.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,24 @@ | ||
package com.acon.server.member.infra.external.ios; | ||
|
||
import com.acon.server.global.auth.jwt.JwtTokenProvider; | ||
import java.security.PublicKey; | ||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AppleAuthAdapter { | ||
|
||
private final AppleAuthClient appleAuthClient; | ||
private final ApplePublicKeyGenerator applePublicKeyGenerator; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
public String getAppleAccountId(String identityToken) { | ||
Map<String, String> headers = jwtTokenProvider.parseHeaders(identityToken); | ||
PublicKey publicKey = | ||
applePublicKeyGenerator.generatePublicKey(headers, appleAuthClient.getAppleAuthPublicKey()); | ||
|
||
return jwtTokenProvider.getTokenClaims(identityToken, publicKey).getSubject(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/acon/server/member/infra/external/ios/AppleAuthClient.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,11 @@ | ||
package com.acon.server.member.infra.external.ios; | ||
|
||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
@FeignClient(name = "appleAuthClient", url = "${apple.auth.public-key-url}") | ||
public interface AppleAuthClient { | ||
|
||
@GetMapping | ||
ApplePublicKeyResponse getAppleAuthPublicKey(); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/acon/server/member/infra/external/ios/ApplePublicKey.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,11 @@ | ||
package com.acon.server.member.infra.external.ios; | ||
|
||
public record ApplePublicKey( | ||
String kty, | ||
String kid, | ||
String alg, | ||
String n, | ||
String e | ||
) { | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/acon/server/member/infra/external/ios/ApplePublicKeyGenerator.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,39 @@ | ||
package com.acon.server.member.infra.external.ios; | ||
|
||
import static org.apache.commons.codec.binary.Base64.decodeBase64; | ||
|
||
import com.acon.server.global.exception.BusinessException; | ||
import com.acon.server.global.exception.ErrorType; | ||
import java.math.BigInteger; | ||
import java.security.KeyFactory; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PublicKey; | ||
import java.security.spec.InvalidKeySpecException; | ||
import java.security.spec.RSAPublicKeySpec; | ||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ApplePublicKeyGenerator { | ||
|
||
public PublicKey generatePublicKey(Map<String, String> tokenHeaders, ApplePublicKeyResponse applePublicKeys) { | ||
ApplePublicKey publicKey = applePublicKeys.getMatchedKey(tokenHeaders.get("kid"), tokenHeaders.get("alg")); | ||
|
||
return getPublicKey(publicKey); | ||
} | ||
|
||
private PublicKey getPublicKey(ApplePublicKey publicKey) { | ||
byte[] nBytes = decodeBase64(publicKey.n()); | ||
byte[] eBytes = decodeBase64(publicKey.e()); | ||
|
||
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(new BigInteger(1, nBytes), new BigInteger(1, eBytes)); | ||
|
||
try { | ||
return KeyFactory.getInstance(publicKey.kty()).generatePublic(publicKeySpec); | ||
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) { | ||
throw new BusinessException(ErrorType.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/acon/server/member/infra/external/ios/ApplePublicKeyResponse.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,17 @@ | ||
package com.acon.server.member.infra.external.ios; | ||
|
||
import com.acon.server.global.exception.BusinessException; | ||
import com.acon.server.global.exception.ErrorType; | ||
import java.util.List; | ||
|
||
public record ApplePublicKeyResponse( | ||
List<ApplePublicKey> keys | ||
) { | ||
|
||
public ApplePublicKey getMatchedKey(String kid, String alg) { | ||
return keys.stream() | ||
.filter(key -> key.kid().equals(kid) && key.alg().equals(alg)) | ||
.findAny() | ||
.orElseThrow(() -> new BusinessException(ErrorType.INTERNAL_SERVER_ERROR)); | ||
} | ||
} |