Skip to content

Commit

Permalink
FIX: (#145) 인증도메인 인프라스트럭처 계층을 재정의한다
Browse files Browse the repository at this point in the history
  • Loading branch information
anxi01 committed Feb 20, 2025
1 parent 0f357c5 commit 2ba4dfe
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 169 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.zerozero.auth.infrastructure.oauth.core;

import com.fasterxml.jackson.annotation.JsonProperty;

public record OAuthAccessTokenResponse(
@JsonProperty("access_token")
String accessToken,

@JsonProperty("expires_in")
long expiresIn,

@JsonProperty("refresh_token")
String refreshToken,

@JsonProperty("token_type")
String tokenType
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.zerozero.auth.infrastructure.oauth.core;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;

@Builder
public record OAuthResourceResponse(
String id,

String email,

@JsonProperty("verified_email")
boolean verifiedEmail
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.zerozero.auth.infrastructure.oauth.core;

public enum Provider {
KAKAO
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.zerozero.auth.infrastructure.oauth.kakao;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@Getter
@Setter
@ConfigurationProperties("oauth.kakao")
public class KakaoOAuthProperty {

protected String clientId;

protected String clientSecret;

protected String redirectUri;

protected Set<String> scope;

protected String tokenUri;

protected String resourceUri;

protected String authUri;
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package com.zerozero.auth.infrastructure.oauth.kakao;

import com.zerozero.auth.exception.AuthenticationErrorCode;
import com.zerozero.auth.infrastructure.oauth.OAuthRestClient;
import com.zerozero.auth.infrastructure.oauth.common.OAuthAccessTokenResponse;
import com.zerozero.auth.infrastructure.oauth.common.OAuthResourceResponse;
import com.zerozero.auth.exception.AuthErrorType;
import com.zerozero.auth.exception.AuthException;
import com.zerozero.auth.application.OAuthRestClient;
import com.zerozero.auth.infrastructure.oauth.core.OAuthAccessTokenResponse;
import com.zerozero.auth.infrastructure.oauth.core.OAuthResourceResponse;
import com.zerozero.auth.infrastructure.oauth.kakao.dto.KakaoResourceResponse;
import com.zerozero.configuration.property.oauth.KakaoOAuthProperty;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.MediaType;
Expand All @@ -19,91 +15,96 @@
import org.springframework.web.client.RestClient;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Optional;

@Component
@RequiredArgsConstructor
@Log4j2
public class KakaoOAuthRestClient implements OAuthRestClient {

private static final String GRANT_TYPE = "authorization_code";

private final KakaoOAuthProperty kakaoOAuthProperty;
private static final String GRANT_TYPE = "authorization_code";

@Override
public URI getAuthUrl() {
return UriComponentsBuilder.fromUriString(kakaoOAuthProperty.getAuthUri())
.queryParam("client_id", kakaoOAuthProperty.getClientId())
.queryParam("redirect_uri", kakaoOAuthProperty.getRedirectUri())
.queryParam("response_type", "code")
.queryParam("scope", kakaoOAuthProperty.getScope())
.build()
.toUri();
}
private final KakaoOAuthProperty kakaoOAuthProperty;

@Override
public OAuthAccessTokenResponse getAccessToken(String authCode) {
if (authCode == null || authCode.isEmpty()) {
log.error("[KakaoOAuthRestClient] authCode is null");
throw AuthenticationErrorCode.NOT_EXIST_AUTH_CODE.toException();
@Override
public URI getAuthUrl() {
return UriComponentsBuilder.fromUriString(kakaoOAuthProperty.getAuthUri())
.queryParam("client_id", kakaoOAuthProperty.getClientId())
.queryParam("redirect_uri", kakaoOAuthProperty.getRedirectUri())
.queryParam("response_type", "code")
.queryParam("scope", kakaoOAuthProperty.getScope())
.build()
.toUri();
}
try {
return RestClient.create()
.post()
.uri(kakaoOAuthProperty.getTokenUri())
.headers(header -> {
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
header.setAcceptCharset(Collections.singletonList(StandardCharsets.UTF_8));
})
.body(createAccessTokenRequestBody(authCode))
.retrieve()
.body(OAuthAccessTokenResponse.class);
} catch (Exception e) {
log.error("[KakaoOAuthRestClient] error", e);
throw AuthenticationErrorCode.ACCESS_TOKEN_NOT_ISSUED.toException();
}
}

@Override
public OAuthResourceResponse getResource(String accessToken) {
if (accessToken == null || accessToken.isEmpty()) {
log.error("[KakaoOAuthRestClient] accessToken is null");
throw AuthenticationErrorCode.ACCESS_TOKEN_NOT_ISSUED.toException();
@Override
public OAuthAccessTokenResponse getAccessToken(String authCode) {
if (authCode == null || authCode.isEmpty()) {
log.error("[KakaoOAuthRestClient] authCode is null");
throw new AuthException(AuthErrorType.NOT_EXIST_AUTH_CODE);
}
try {
return RestClient.create()
.post()
.uri(kakaoOAuthProperty.getTokenUri())
.headers(header -> {
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
header.setAcceptCharset(Collections.singletonList(StandardCharsets.UTF_8));
})
.body(createAccessTokenRequestBody(authCode))
.retrieve()
.body(OAuthAccessTokenResponse.class);
} catch (Exception e) {
log.error("[KakaoOAuthRestClient] error", e);
throw new AuthException(AuthErrorType.ACCESS_TOKEN_NOT_ISSUED);
}
}
try {
KakaoResourceResponse kakaoResourceResponse = RestClient.create()
.get()
.uri(kakaoOAuthProperty.getResourceUri())
.headers(header -> {
header.setBearerAuth(accessToken);
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
header.setAcceptCharset(Collections.singletonList(StandardCharsets.UTF_8));
})
.retrieve()
.body(KakaoResourceResponse.class);
long id = Optional.ofNullable(kakaoResourceResponse)
.map(KakaoResourceResponse::id)
.orElseThrow(AuthenticationErrorCode.NOT_EXIST_RESOURCE_RESPONSE::toException);
KakaoResourceResponse.Response kakaoAccount = Optional.ofNullable(
kakaoResourceResponse.kakaoAccount())
.orElseThrow(AuthenticationErrorCode.NOT_EXIST_RESOURCE_RESPONSE::toException);
String email = Optional.ofNullable(kakaoAccount.email())
.orElseThrow(AuthenticationErrorCode.NOT_EXIST_RESOURCE_RESPONSE::toException);
return OAuthResourceResponse.builder()
.id(String.valueOf(id))
.email(email)
.build();
} catch (Exception e) {
log.error("[KakaoOAuthRestClient] error", e);
throw AuthenticationErrorCode.RESOURCE_SERVER_UNAVAILABLE.toException();

@Override
public OAuthResourceResponse getResource(String accessToken) {
if (accessToken == null || accessToken.isEmpty()) {
log.error("[KakaoOAuthRestClient] accessToken is null");
throw new AuthException(AuthErrorType.ACCESS_TOKEN_NOT_ISSUED);
}
try {
KakaoResourceResponse kakaoResourceResponse = RestClient.create()
.get()
.uri(kakaoOAuthProperty.getResourceUri())
.headers(header -> {
header.setBearerAuth(accessToken);
header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
header.setAcceptCharset(Collections.singletonList(StandardCharsets.UTF_8));
})
.retrieve()
.body(KakaoResourceResponse.class);
long id = Optional.ofNullable(kakaoResourceResponse)
.map(KakaoResourceResponse::id)
.orElseThrow(() -> new AuthException(AuthErrorType.NOT_EXIST_RESOURCE_RESPONSE));
KakaoResourceResponse.Response kakaoAccount = Optional.ofNullable(
kakaoResourceResponse.kakaoAccount())
.orElseThrow(() -> new AuthException(AuthErrorType.NOT_EXIST_RESOURCE_RESPONSE));
String email = Optional.ofNullable(kakaoAccount.email())
.orElseThrow(() -> new AuthException(AuthErrorType.NOT_EXIST_RESOURCE_RESPONSE));
return OAuthResourceResponse.builder()
.id(String.valueOf(id))
.email(email)
.build();
} catch (Exception e) {
log.error("[KakaoOAuthRestClient] error", e);
throw new AuthException(AuthErrorType.RESOURCE_SERVER_UNAVAILABLE);
}
}
}

private MultiValueMap<String, String> createAccessTokenRequestBody(String authCode) {
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.add("client_id", kakaoOAuthProperty.getClientId());
parameters.add("client_secret", kakaoOAuthProperty.getClientSecret());
parameters.add("code", authCode);
parameters.add("grant_type", GRANT_TYPE);
parameters.add("redirect_uri", kakaoOAuthProperty.getRedirectUri());
return parameters;
}
private MultiValueMap<String, String> createAccessTokenRequestBody(String authCode) {
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.add("client_id", kakaoOAuthProperty.getClientId());
parameters.add("client_secret", kakaoOAuthProperty.getClientSecret());
parameters.add("code", authCode);
parameters.add("grant_type", GRANT_TYPE);
parameters.add("redirect_uri", kakaoOAuthProperty.getRedirectUri());
return parameters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import com.fasterxml.jackson.annotation.JsonProperty;

public record KakaoResourceResponse(
Long id,
Long id,

@JsonProperty("kakao_account")
Response kakaoAccount
@JsonProperty("kakao_account")
Response kakaoAccount
) {
public record Response(
String email,
Profile profile
) {
public record Profile(
String nickname
public record Response(
String email,
Profile profile
) {
public record Profile(
String nickname
) {
}
}
}
}

0 comments on commit 2ba4dfe

Please sign in to comment.