-
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.
* test: apple auth * chore: appleTest RequestBody => RequestParam으로 변경 * feat: apple 토큰 검증 코드 작성 * feat: apple 로그인 구현 * chore: application-prod.yml, dev.yml 수정 * chore: 불필요한 로그 삭제 * chore: LayerApplication 불필요한 주석 삭제 * chore: 필요없는 빈 주입 제거 * chore: AppleService 빈 주입 제거 --------- Co-authored-by: Raymond <[email protected]>
- Loading branch information
1 parent
41c1ee3
commit 06c1421
Showing
17 changed files
with
307 additions
and
7 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
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
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
5 changes: 4 additions & 1 deletion
5
layer-domain/src/main/java/org/layer/domain/member/entity/SocialType.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,5 +1,8 @@ | ||
package org.layer.domain.member.entity; | ||
|
||
public enum SocialType { | ||
KAKAO, GOOGLE, NONE | ||
KAKAO, | ||
GOOGLE, | ||
APPLE, | ||
NONE | ||
} |
16 changes: 16 additions & 0 deletions
16
layer-external/src/main/java/org/layer/oauth/config/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,16 @@ | ||
package org.layer.oauth.config; | ||
|
||
|
||
import org.layer.oauth.dto.service.apple.ApplePublicKeys; | ||
import org.springframework.cloud.openfeign.FeignClient; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
|
||
@FeignClient(name = "apple-public-key", url = "https://appleid.apple.com") | ||
@Component | ||
public interface AppleAuthClient { | ||
|
||
@GetMapping("/auth/keys") | ||
ApplePublicKeys getApplePublicKeys(); | ||
} |
43 changes: 43 additions & 0 deletions
43
layer-external/src/main/java/org/layer/oauth/dto/service/apple/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,43 @@ | ||
package org.layer.oauth.dto.service.apple; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class ApplePublicKey { | ||
private final String kty; | ||
|
||
private final String kid; | ||
|
||
private final String use; | ||
|
||
private final String alg; | ||
|
||
private final String n; | ||
|
||
private final String e; | ||
|
||
public boolean isSameAlg(final String alg) { | ||
return this.alg.equals(alg); | ||
} | ||
|
||
public boolean isSameKid(final String kid) { | ||
return this.kid.equals(kid); | ||
} | ||
|
||
@JsonCreator | ||
public ApplePublicKey(@JsonProperty("kty") final String kty, | ||
@JsonProperty("kid") final String kid, | ||
@JsonProperty("use") final String use, | ||
@JsonProperty("alg") final String alg, | ||
@JsonProperty("n") final String n, | ||
@JsonProperty("e") final String e) { | ||
this.kty = kty; | ||
this.kid = kid; | ||
this.use = use; | ||
this.alg = alg; | ||
this.n = n; | ||
this.e = e; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
layer-external/src/main/java/org/layer/oauth/dto/service/apple/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,44 @@ | ||
package org.layer.oauth.dto.service.apple; | ||
|
||
import org.springframework.stereotype.Component; | ||
|
||
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.Base64; | ||
import java.util.Map; | ||
|
||
@Component | ||
public class ApplePublicKeyGenerator { | ||
|
||
private static final String SIGN_ALGORITHM_HEADER = "alg"; | ||
private static final String KEY_ID_HEADER = "kid"; | ||
private static final int POSITIVE_SIGN_NUMBER = 1; | ||
|
||
public PublicKey generate(final Map<String, String> headers, final ApplePublicKeys publicKeys) { | ||
final ApplePublicKey applePublicKey = publicKeys.getMatchingKey( | ||
headers.get(SIGN_ALGORITHM_HEADER), | ||
headers.get(KEY_ID_HEADER) | ||
); | ||
return generatePublicKey(applePublicKey); | ||
} | ||
|
||
private PublicKey generatePublicKey(final ApplePublicKey applePublicKey) { | ||
final byte[] nBytes = Base64.getUrlDecoder().decode(applePublicKey.getN()); | ||
final byte[] eBytes = Base64.getUrlDecoder().decode(applePublicKey.getE()); | ||
|
||
final BigInteger n = new BigInteger(POSITIVE_SIGN_NUMBER, nBytes); | ||
final BigInteger e = new BigInteger(POSITIVE_SIGN_NUMBER, eBytes); | ||
final RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(n, e); | ||
|
||
try { | ||
final KeyFactory keyFactory = KeyFactory.getInstance(applePublicKey.getKty()); | ||
return keyFactory.generatePublic(rsaPublicKeySpec); | ||
} catch (NoSuchAlgorithmException | InvalidKeySpecException exception) { | ||
throw new RuntimeException("잘못된 애플 키"); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
layer-external/src/main/java/org/layer/oauth/dto/service/apple/ApplePublicKeys.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 org.layer.oauth.dto.service.apple; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class ApplePublicKeys { | ||
|
||
private List<ApplePublicKey> keys; | ||
|
||
public ApplePublicKeys(List<ApplePublicKey> keys) { | ||
this.keys = List.copyOf(keys); | ||
} | ||
|
||
public ApplePublicKey getMatchingKey(final String alg, final String kid) { | ||
return keys.stream() | ||
.filter(key -> key.isSameAlg(alg) && key.isSameKid(kid)) | ||
.findFirst() | ||
.orElseThrow(() -> new RuntimeException("잘못된 토큰 형태입니다.")); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
layer-external/src/main/java/org/layer/oauth/dto/service/apple/AppleTokenParser.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,54 @@ | ||
package org.layer.oauth.dto.service.apple; | ||
|
||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonMappingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.JwtException; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.UnsupportedJwtException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.security.PublicKey; | ||
import java.util.Base64; | ||
import java.util.Map; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class AppleTokenParser { | ||
|
||
private static final String IDENTITY_TOKEN_VALUE_DELIMITER = "\\."; | ||
private static final int HEADER_INDEX = 0; | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
public Map parseHeader(final String appleToken) { | ||
try { | ||
final String encodedHeader = appleToken.split(IDENTITY_TOKEN_VALUE_DELIMITER)[HEADER_INDEX]; | ||
final String decodedHeader = new String(Base64.getDecoder().decode(encodedHeader)); | ||
return objectMapper.readValue(decodedHeader, Map.class); | ||
} catch (JsonMappingException e) { | ||
throw new RuntimeException("appleToken 값이 jwt 형식인지, 값이 정상적인지 확인해주세요."); | ||
} catch (JsonProcessingException e) { | ||
throw new RuntimeException("디코드된 헤더를 Map 형태로 분류할 수 없습니다. 헤더를 확인해주세요."); | ||
} | ||
} | ||
|
||
public Claims extractClaims(final String appleToken, final PublicKey publicKey) { | ||
try { | ||
return Jwts.parser() | ||
.verifyWith(publicKey) | ||
.build() | ||
.parseSignedClaims(appleToken) | ||
.getPayload(); | ||
} catch (UnsupportedJwtException e) { | ||
throw new UnsupportedJwtException("지원되지 않는 jwt 타입"); | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalArgumentException("비어있는 jwt"); | ||
} catch (JwtException e) { | ||
throw new JwtException("jwt 검증 or 분석 오류"); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
layer-external/src/main/java/org/layer/oauth/exception/OAuthException.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,10 @@ | ||
package org.layer.oauth.exception; | ||
|
||
import org.layer.common.exception.BaseCustomException; | ||
import org.layer.common.exception.ExceptionType; | ||
|
||
public class OAuthException extends BaseCustomException { | ||
public OAuthException(ExceptionType exceptionType) { | ||
super(exceptionType); | ||
} | ||
} |
Oops, something went wrong.