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

[BE] feat: AppleOpenId 로직 리팩터링(#975) #976

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -4,13 +4,10 @@
import com.festago.auth.domain.OpenIdNonceValidator;
import com.festago.auth.domain.SocialType;
import com.festago.auth.domain.UserInfo;
import com.festago.common.exception.ErrorCode;
import com.festago.common.exception.UnauthorizedException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import java.time.Clock;
import java.util.Date;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand All @@ -34,6 +31,7 @@ public AppleOpenIdClient(
this.openIdNonceValidator = openIdNonceValidator;
this.idTokenParser = new OpenIdIdTokenParser(Jwts.parser()
.keyLocator(appleOpenIdPublicKeyLocator)
.requireAudience(clientId)
.requireIssuer(ISSUER)
.clock(() -> Date.from(clock.instant()))
.build());
Expand All @@ -43,23 +41,12 @@ public AppleOpenIdClient(
public UserInfo getUserInfo(String idToken) {
Claims payload = idTokenParser.parse(idToken);
openIdNonceValidator.validate(payload.get("nonce", String.class), payload.getExpiration());
validateAudience(payload.getAudience());
return UserInfo.builder()
.socialType(SocialType.APPLE)
.socialId(payload.getSubject())
.build();
}

private void validateAudience(Set<String> audiences) {
for (String audience : audiences) {
if (clientId.equals(audience)) {
return;
}
}
log.info("허용되지 않는 id 토큰의 audience 값이 요청되었습니다. audiences={}", audiences);
throw new UnauthorizedException(ErrorCode.OPEN_ID_INVALID_TOKEN);
}

@Override
public SocialType getSocialType() {
return SocialType.APPLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class AppleOpenIdPublicKeyLocator implements Locator<Key> {

private final AppleOpenIdJwksClient appleOpenIdJwksClient;
private final CachedAppleOpenIdKeyProvider cachedOpenIdKeyProvider;
private final CachedOpenIdKeyProvider cachedOpenIdKeyProvider;

@Override
public Key locate(Header header) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Supplier;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@Scope("prototype")
public class CachedOpenIdKeyProvider {

private final Map<String, Key> cache = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,4 @@ void setUp() {
// then
assertThat(expect.socialId()).isEqualTo(socialId);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,4 @@ class AppleOpenIdJwksClientTest {
.map(Identifiable::getId)
.containsExactlyInAnyOrder("pyaRQpAbnY", "lVHdOx8ltR", "Bh6H7rHVmb");
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.festago.auth.infrastructure.openid;

import static org.assertj.core.api.Assertions.assertThat;

import com.festago.support.ApplicationIntegrationTest;
import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator.ReplaceUnderscores;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

@DisplayNameGeneration(ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class AppleOpenIdPublicKeyLocatorTest extends ApplicationIntegrationTest {

@Autowired
AppleOpenIdPublicKeyLocator appleOpenIdPublicKeyLocator;

@Autowired
KakaoOpenIdPublicKeyLocator kakaoOpenIdPublicKeyLocator;

@Test
void 소셜별_Locator_들은_캐싱을_공유하지_않는다() {

// given & when & then
assertThat(appleOpenIdPublicKeyLocator)
.usingRecursiveComparison()
.comparingOnlyFields("cachedOpenIdKeyProvider")
.isNotEqualTo(kakaoOpenIdPublicKeyLocator);
}
}
Copy link
Collaborator

@seokjin8678 seokjin8678 May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

비즈니스 로직의 검증이 아닌, 스프링 프레임워크의 동작에 대한 검증 테스트 같네요!
해당 테스트가 필요하다고 생각하신 이유가 있을까요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사실 필요없지만 prototype을 직접 사용한건 처음이라 한번 검증차에서 구현해본 것을 남겼습니다!!
검증 됬으니 지우겠습니다!

Loading