-
Notifications
You must be signed in to change notification settings - Fork 1
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
[Feat] 구글 로그인 구현 #27
Merged
+651
−1
Merged
[Feat] 구글 로그인 구현 #27
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
422c7a3
chore: dev, prod 환경 env 변수명 갱신
Hoya324 49fa256
refac: auth token properties 폴더 구조 및 네이밍 변경
Hoya324 ea21356
feat: 구글 인증 코드 요청 Provider
Hoya324 a302bde
feat: 구글 유저 조회 client
Hoya324 59513aa
feat: 구글 oauth 예외코드 추가
Hoya324 cb03931
chore: 구글 oauth env 설정
Hoya324 6c11531
chore: CORS origin url pattern 을 통한 유연성 추가
Hoya324 2dec7bd
refac: Google client 에서 token 발급과 정보 조회 분리
Hoya324 cf7d197
feat: token 발급과 정보 조회 dto 추가
Hoya324 2c3ed53
feat: 예외 handler 추가 및 예러 코드 추가
Hoya324 387d912
test: google 소셜 로그인 mock test
Hoya324 31b9b81
test: 구글 provider test 공통 Mock 클래스 추가
Hoya324 6629db4
refac: 애매한 예외 변수명 변경
Hoya324 b6dcf68
refac: 예외 메시지를 테스트에서 검증을 위한 수정
Hoya324 e9b53d9
test: 예외 발생 시점 수정
Hoya324 f26915e
refac: 환경변수 properties 공통 적용 방식으로 수정
Hoya324 d4bd09a
refac: 토큰 요청 시 request dto 추가
Hoya324 30b8473
refac: 폴더 구조 변경
Hoya324 a1676d9
refac: 카카오 로그인 구현 충돌 해결
Hoya324 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 36 additions & 0 deletions
36
...ain/java/com/evenly/took/feature/auth/client/google/GoogleAuthCodeRequestUrlProvider.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,36 @@ | ||
package com.evenly.took.feature.auth.client.google; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import com.evenly.took.feature.auth.client.AuthCodeRequestUrlProvider; | ||
import com.evenly.took.feature.auth.domain.OAuthType; | ||
import com.evenly.took.global.config.properties.auth.GoogleProperties; | ||
import com.evenly.took.global.config.properties.auth.GoogleUrlProperties; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class GoogleAuthCodeRequestUrlProvider implements AuthCodeRequestUrlProvider { | ||
|
||
private final GoogleProperties googleProperties; | ||
private final GoogleUrlProperties googleUrlProperties; | ||
|
||
@Override | ||
public OAuthType supportType() { | ||
return OAuthType.GOOGLE; | ||
} | ||
|
||
@Override | ||
public String provide() { | ||
return UriComponentsBuilder.fromUriString(googleUrlProperties.authorizationUri()) | ||
.queryParam("client_id", googleProperties.clientId()) | ||
.queryParam("redirect_uri", googleProperties.redirectUri()) | ||
.queryParam("response_type", "code") | ||
.queryParam("scope", googleProperties.scope()) | ||
.queryParam("access_type", "offline") | ||
.build(true) | ||
.toUriString(); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/com/evenly/took/feature/auth/client/google/GoogleTokenProvider.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,46 @@ | ||
package com.evenly.took.feature.auth.client.google; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestClient; | ||
|
||
import com.evenly.took.feature.auth.client.google.dto.request.GoogleTokenRequest; | ||
import com.evenly.took.feature.auth.client.google.dto.response.GoogleTokenResponse; | ||
import com.evenly.took.feature.auth.client.google.error.GoogleTokenProviderErrorHandler; | ||
import com.evenly.took.global.config.properties.auth.GoogleProperties; | ||
import com.evenly.took.global.config.properties.auth.GoogleUrlProperties; | ||
|
||
@Component | ||
public class GoogleTokenProvider { | ||
|
||
private final RestClient restClient; | ||
private final GoogleProperties googleProperties; | ||
private final GoogleUrlProperties googleUrlProperties; | ||
|
||
public GoogleTokenProvider(GoogleProperties googleProperties, | ||
GoogleUrlProperties googleUrlProperties, | ||
RestClient.Builder restClientBuilder, | ||
GoogleTokenProviderErrorHandler errorHandler) { | ||
|
||
this.googleProperties = googleProperties; | ||
this.googleUrlProperties = googleUrlProperties; | ||
this.restClient = restClientBuilder | ||
.defaultStatusHandler(errorHandler) | ||
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE, | ||
StandardCharsets.UTF_8.name()) | ||
.build(); | ||
} | ||
|
||
public GoogleTokenResponse fetchAccessToken(String authCode) { | ||
GoogleTokenRequest request = GoogleTokenRequest.of(googleProperties, authCode); | ||
return restClient.post() | ||
.uri(googleUrlProperties.tokenUri()) | ||
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE) | ||
.body(request.toMultiValueMap()) | ||
.retrieve() | ||
.body(GoogleTokenResponse.class); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/com/evenly/took/feature/auth/client/google/GoogleUserClient.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,52 @@ | ||
package com.evenly.took.feature.auth.client.google; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
|
||
import com.evenly.took.feature.auth.client.UserClient; | ||
import com.evenly.took.feature.auth.client.google.dto.response.GoogleTokenResponse; | ||
import com.evenly.took.feature.auth.client.google.dto.response.GoogleUserInfoResponse; | ||
import com.evenly.took.feature.auth.domain.OAuthIdentifier; | ||
import com.evenly.took.feature.auth.domain.OAuthType; | ||
import com.evenly.took.feature.auth.exception.AuthErrorCode; | ||
import com.evenly.took.feature.common.exception.TookException; | ||
import com.evenly.took.feature.user.domain.User; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class GoogleUserClient implements UserClient { | ||
|
||
private final GoogleTokenProvider googleTokenProvider; | ||
private final GoogleUserInfoProvider googleUserInfoProvider; | ||
|
||
@Override | ||
public OAuthType supportType() { | ||
return OAuthType.GOOGLE; | ||
} | ||
|
||
@Override | ||
public User fetch(String authCode) { | ||
try { | ||
GoogleTokenResponse tokenResponse = googleTokenProvider.fetchAccessToken(authCode); | ||
GoogleUserInfoResponse userInfoResponse = googleUserInfoProvider.fetchUserInfo(tokenResponse.accessToken()); | ||
|
||
return generateUser(userInfoResponse.sub(), userInfoResponse.name()); | ||
} catch (HttpClientErrorException ex) { | ||
throw new TookException(AuthErrorCode.INVALID_GOOGLE_CONNECTION); | ||
} | ||
} | ||
|
||
private User generateUser(String oauthId, String name) { | ||
OAuthIdentifier oauthIdentifier = OAuthIdentifier.builder() | ||
.oauthId(oauthId) | ||
.oauthType(OAuthType.GOOGLE) | ||
.build(); | ||
|
||
return User.builder() | ||
.oauthIdentifier(oauthIdentifier) | ||
.name(name) | ||
.build(); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/java/com/evenly/took/feature/auth/client/google/GoogleUserInfoProvider.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.evenly.took.feature.auth.client.google; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestClient; | ||
|
||
import com.evenly.took.feature.auth.client.google.dto.response.GoogleUserInfoResponse; | ||
import com.evenly.took.feature.auth.client.google.error.GoogleUserInfoProviderErrorHandler; | ||
import com.evenly.took.global.config.properties.auth.GoogleUrlProperties; | ||
|
||
@Component | ||
public class GoogleUserInfoProvider { | ||
|
||
private final RestClient restClient; | ||
private final GoogleUrlProperties googleUrlProperties; | ||
|
||
public GoogleUserInfoProvider(GoogleUrlProperties googleUrlProperties, | ||
RestClient.Builder restClientBuilder, | ||
GoogleUserInfoProviderErrorHandler errorHandler) { | ||
|
||
this.googleUrlProperties = googleUrlProperties; | ||
this.restClient = restClientBuilder | ||
.defaultStatusHandler(errorHandler) | ||
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE, | ||
StandardCharsets.UTF_8.name()) | ||
.build(); | ||
Hoya324 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
public GoogleUserInfoResponse fetchUserInfo(String accessToken) { | ||
return restClient.get() | ||
.uri(googleUrlProperties.userInfoUrl()) | ||
.header(HttpHeaders.AUTHORIZATION, "Bearer " + accessToken) | ||
.retrieve() | ||
.body(GoogleUserInfoResponse.class); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/evenly/took/feature/auth/client/google/dto/request/GoogleTokenRequest.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,33 @@ | ||
package com.evenly.took.feature.auth.client.google.dto.request; | ||
|
||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
import com.evenly.took.global.config.properties.auth.GoogleProperties; | ||
|
||
public record GoogleTokenRequest( | ||
String grantType, | ||
String clientId, | ||
String redirectUri, | ||
String code, | ||
String clientSecret | ||
) { | ||
|
||
public static GoogleTokenRequest of(GoogleProperties googleProperties, String code) { | ||
return new GoogleTokenRequest("authorization_code", | ||
googleProperties.clientId(), | ||
googleProperties.redirectUri(), | ||
code, | ||
googleProperties.clientSecret()); | ||
} | ||
|
||
public MultiValueMap<String, Object> toMultiValueMap() { | ||
MultiValueMap<String, Object> map = new LinkedMultiValueMap<>(); | ||
map.add("grant_type", grantType); | ||
map.add("client_id", clientId); | ||
map.add("redirect_uri", redirectUri); | ||
map.add("code", code); | ||
map.add("client_secret", clientSecret); | ||
return map; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ain/java/com/evenly/took/feature/auth/client/google/dto/response/GoogleTokenResponse.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,22 @@ | ||
package com.evenly.took.feature.auth.client.google.dto.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record GoogleTokenResponse( | ||
|
||
@JsonProperty("access_token") | ||
String accessToken, | ||
|
||
@JsonProperty("expires_in") | ||
Integer expiresIn, | ||
|
||
@JsonProperty("scope") | ||
String scope, | ||
|
||
@JsonProperty("token_type") | ||
String tokenType, | ||
|
||
@JsonProperty("id_token") | ||
String idToken | ||
) { | ||
} |
13 changes: 13 additions & 0 deletions
13
.../java/com/evenly/took/feature/auth/client/google/dto/response/GoogleUserInfoResponse.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,13 @@ | ||
package com.evenly.took.feature.auth.client.google.dto.response; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public record GoogleUserInfoResponse( | ||
|
||
@JsonProperty("sub") | ||
String sub, | ||
|
||
@JsonProperty("name") | ||
String name | ||
) { | ||
} |
38 changes: 38 additions & 0 deletions
38
...ava/com/evenly/took/feature/auth/client/google/error/GoogleTokenProviderErrorHandler.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,38 @@ | ||
package com.evenly.took.feature.auth.client.google.error; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StreamUtils; | ||
import org.springframework.web.client.ResponseErrorHandler; | ||
|
||
import com.evenly.took.feature.auth.exception.AuthErrorCode; | ||
import com.evenly.took.feature.common.exception.TookException; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Component | ||
@Slf4j | ||
public class GoogleTokenProviderErrorHandler implements ResponseErrorHandler { | ||
|
||
@Override | ||
public boolean hasError(ClientHttpResponse response) throws IOException { | ||
return response.getStatusCode().isError(); | ||
} | ||
|
||
@Override | ||
public void handleError(URI uri, HttpMethod method, ClientHttpResponse response) throws IOException { | ||
String responseBody = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); | ||
|
||
if (response.getStatusCode().is4xxClientError()) { | ||
throw new TookException(AuthErrorCode.INVALID_GOOGLE_TOKEN_REQUEST); | ||
} | ||
|
||
log.error("Google 승인토큰 오류: {}", responseBody); | ||
throw new TookException(AuthErrorCode.INVALID_GOOGLE_CONNECTION); | ||
} | ||
Hoya324 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
42 changes: 42 additions & 0 deletions
42
.../com/evenly/took/feature/auth/client/google/error/GoogleUserInfoProviderErrorHandler.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 com.evenly.took.feature.auth.client.google.error; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.StreamUtils; | ||
import org.springframework.web.client.ResponseErrorHandler; | ||
|
||
import com.evenly.took.feature.auth.exception.AuthErrorCode; | ||
import com.evenly.took.feature.common.exception.TookException; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Component | ||
@Slf4j | ||
public class GoogleUserInfoProviderErrorHandler implements ResponseErrorHandler { | ||
|
||
@Override | ||
public boolean hasError(ClientHttpResponse response) throws IOException { | ||
return response.getStatusCode().isError(); | ||
} | ||
|
||
@Override | ||
public void handleError(URI uri, HttpMethod method, ClientHttpResponse response) throws IOException { | ||
String responseBody = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8); | ||
|
||
if (response.getStatusCode().is4xxClientError()) { | ||
throw new TookException(AuthErrorCode.INVALID_GOOGLE_USER_REQUEST); | ||
} | ||
|
||
if (response.getStatusCode().is5xxServerError()) { | ||
throw new TookException(AuthErrorCode.INVALID_GOOGLE_SERVER_ERROR); | ||
} | ||
|
||
log.error("Google 사용자정보 알 수 없는 오류: {}", responseBody); | ||
throw new TookException(AuthErrorCode.INVALID_GOOGLE_CONNECTION); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
혹시 각 GoogleXXXProvider에서 에러 핸들러를 통해 HttpClientErrorException을 잡고 있어도 잡히지 않는 예외가 있었나요?? 혹시 있었다면 저도 처리해야할 것 같아서 여쭤봐요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 이건 제가 handler 적용 전에 했던건데 놓쳤네요!! 괜찮을거 같습니다!