From d2997ac5f0125847cbeff8ef290693c10d708d33 Mon Sep 17 00:00:00 2001 From: cabbage16 Date: Sun, 24 Nov 2024 22:10:56 +0900 Subject: [PATCH 1/3] =?UTF-8?q?perf(#26):=20=EC=9B=B9=EC=97=90=EC=84=9C=20?= =?UTF-8?q?Google=20=EA=B3=84=EC=A0=95=EC=9C=BC=EB=A1=9C=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EA=B3=BC=EC=A0=95=20=ED=86=B5=ED=95=A9=20?= =?UTF-8?q?-=20code=EB=A5=BC=20=ED=86=B5=ED=95=B4=20access=20token?= =?UTF-8?q?=EC=9D=84=20=EB=B0=9C=EA=B8=89=EB=B0=9B=EB=8A=94=20=EA=B3=BC?= =?UTF-8?q?=EC=A0=95=EA=B3=BC=20access=20token=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EC=9C=A0=EC=A0=80=20=EC=A0=95=EB=B3=B4=EB=A5=BC=20=EA=B0=80?= =?UTF-8?q?=EC=A0=B8=EC=98=A4=EB=8A=94=20=EA=B3=BC=EC=A0=95=EC=9D=B4=20?= =?UTF-8?q?=EA=B5=B3=EC=9D=B4=20=EB=B6=84=EB=A6=AC=EB=90=98=EC=96=B4=20?= =?UTF-8?q?=EC=A1=B4=EC=9E=AC=ED=95=A0=20=ED=95=84=EC=9A=94=EA=B0=80=20?= =?UTF-8?q?=EC=97=86=EB=8B=A4=EA=B3=A0=20=ED=8C=90=EB=8B=A8=EB=90=98?= =?UTF-8?q?=EC=96=B4=20=ED=86=B5=ED=95=A9=ED=96=88=EC=96=B4=EC=9A=94.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/GetGoogleAccessTokenUseCase.java | 32 ------------------- .../auth/GoogleAuthLinkUseCase.java | 4 +-- .../auth/GoogleAuthWebUseCase.java | 22 +++++++++++-- .../presentation/auth/AuthController.java | 16 ++-------- .../properties/GoogleOAuthProperties.java | 6 ++-- src/main/resources/application.yml | 13 +++----- 6 files changed, 30 insertions(+), 63 deletions(-) delete mode 100644 src/main/java/com/bamdoliro/sinabro/application/auth/GetGoogleAccessTokenUseCase.java diff --git a/src/main/java/com/bamdoliro/sinabro/application/auth/GetGoogleAccessTokenUseCase.java b/src/main/java/com/bamdoliro/sinabro/application/auth/GetGoogleAccessTokenUseCase.java deleted file mode 100644 index b43ca85..0000000 --- a/src/main/java/com/bamdoliro/sinabro/application/auth/GetGoogleAccessTokenUseCase.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.bamdoliro.sinabro.application.auth; - -import com.bamdoliro.sinabro.infrastructure.oauth.google.feign.GoogleAuthClient; -import com.bamdoliro.sinabro.infrastructure.oauth.google.feign.dto.request.GoogleAuthRequest; -import com.bamdoliro.sinabro.shared.annotation.UseCase; -import com.bamdoliro.sinabro.shared.config.properties.GoogleOAuthProperties; -import lombok.RequiredArgsConstructor; -import org.springframework.transaction.annotation.Transactional; - -@UseCase -@RequiredArgsConstructor -public class GetGoogleAccessTokenUseCase { - - private final GoogleAuthClient googleAuthClient; - private final GoogleOAuthProperties googleOAuthProperties; - - @Transactional - public String execute(String code) { - return googleAuthClient - .getAccessToken(createGoogleAuthRequest(code)) - .getAccessToken(); - } - - private GoogleAuthRequest createGoogleAuthRequest(String code) { - return GoogleAuthRequest.builder() - .code(code) - .clientId(googleOAuthProperties.getWeb().getClientId()) - .redirectUri(googleOAuthProperties.getWeb().getRedirectUri()) - .clientSecret(googleOAuthProperties.getWeb().getClientSecret()) - .build(); - } -} diff --git a/src/main/java/com/bamdoliro/sinabro/application/auth/GoogleAuthLinkUseCase.java b/src/main/java/com/bamdoliro/sinabro/application/auth/GoogleAuthLinkUseCase.java index 1a935b1..c19f0b3 100644 --- a/src/main/java/com/bamdoliro/sinabro/application/auth/GoogleAuthLinkUseCase.java +++ b/src/main/java/com/bamdoliro/sinabro/application/auth/GoogleAuthLinkUseCase.java @@ -14,7 +14,7 @@ public class GoogleAuthLinkUseCase { "scope=https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"; public String execute() { - return googleOAuthProperties.getWeb().getBaseUrl() - + String.format(QUERY_STRING, googleOAuthProperties.getWeb().getClientId(), googleOAuthProperties.getWeb().getRedirectUri()); + return googleOAuthProperties.getGoogle().getBaseUrl() + + String.format(QUERY_STRING, googleOAuthProperties.getGoogle().getClientId(), googleOAuthProperties.getGoogle().getRedirectUri()); } } diff --git a/src/main/java/com/bamdoliro/sinabro/application/auth/GoogleAuthWebUseCase.java b/src/main/java/com/bamdoliro/sinabro/application/auth/GoogleAuthWebUseCase.java index 466f514..64cc890 100644 --- a/src/main/java/com/bamdoliro/sinabro/application/auth/GoogleAuthWebUseCase.java +++ b/src/main/java/com/bamdoliro/sinabro/application/auth/GoogleAuthWebUseCase.java @@ -1,11 +1,13 @@ package com.bamdoliro.sinabro.application.auth; import com.bamdoliro.sinabro.domain.auth.service.GoogleAuthService; +import com.bamdoliro.sinabro.infrastructure.oauth.google.feign.GoogleAuthClient; import com.bamdoliro.sinabro.infrastructure.oauth.google.feign.GoogleInformationWebClient; +import com.bamdoliro.sinabro.infrastructure.oauth.google.feign.dto.request.GoogleAuthRequest; import com.bamdoliro.sinabro.infrastructure.oauth.google.feign.dto.response.GoogleInformation; -import com.bamdoliro.sinabro.presentation.auth.dto.request.GoogleTokenRequest; import com.bamdoliro.sinabro.presentation.auth.dto.response.TokenResponse; import com.bamdoliro.sinabro.shared.annotation.UseCase; +import com.bamdoliro.sinabro.shared.config.properties.GoogleOAuthProperties; import lombok.RequiredArgsConstructor; import org.springframework.transaction.annotation.Transactional; @@ -15,11 +17,25 @@ public class GoogleAuthWebUseCase { private final GoogleInformationWebClient googleInformationWebClient; private final GoogleAuthService googleAuthService; + private final GoogleAuthClient googleAuthClient; + private final GoogleOAuthProperties googleOAuthProperties; @Transactional - public TokenResponse execute(GoogleTokenRequest request) { - GoogleInformation information = googleInformationWebClient.getUserInformation("Bearer " + request.getToken()); + public TokenResponse execute(String code) { + String accessToken = googleAuthClient + .getAccessToken(createGoogleAuthRequest(code)) + .getAccessToken(); + GoogleInformation information = googleInformationWebClient.getUserInformation("Bearer " + accessToken); return googleAuthService.execute(information); } + + private GoogleAuthRequest createGoogleAuthRequest(String code) { + return GoogleAuthRequest.builder() + .code(code) + .clientId(googleOAuthProperties.getGoogle().getClientId()) + .redirectUri(googleOAuthProperties.getGoogle().getRedirectUri()) + .clientSecret(googleOAuthProperties.getGoogle().getClientSecret()) + .build(); + } } diff --git a/src/main/java/com/bamdoliro/sinabro/presentation/auth/AuthController.java b/src/main/java/com/bamdoliro/sinabro/presentation/auth/AuthController.java index 127d22b..50ee93c 100644 --- a/src/main/java/com/bamdoliro/sinabro/presentation/auth/AuthController.java +++ b/src/main/java/com/bamdoliro/sinabro/presentation/auth/AuthController.java @@ -19,7 +19,6 @@ public class AuthController { private final GoogleAuthLinkUseCase googleAuthLinkUseCase; - private final GetGoogleAccessTokenUseCase getGoogleAccessTokenUseCase; private final RefreshAccessTokenUseCase refreshAccessTokenUseCase; private final LogOutUseCase logOutUseCase; private final GoogleAuthWebUseCase googleAuthWebUseCase; @@ -32,17 +31,10 @@ public SingleCommonResponse getGoogleAuthUrl() { ); } - @GetMapping("/google") - public SingleCommonResponse getGoogleAccessToken(@RequestParam String code) { - return CommonResponse.ok( - getGoogleAccessTokenUseCase.execute(code) - ); - } - @PostMapping("/google/web") - public SingleCommonResponse authWithGoogleWeb(@RequestBody @Valid GoogleTokenRequest request) { + public SingleCommonResponse authWithGoogleWeb(@RequestParam String code) { return CommonResponse.ok( - googleAuthWebUseCase.execute(request) + googleAuthWebUseCase.execute(code) ); } @@ -62,9 +54,7 @@ public SingleCommonResponse refreshAccessToken(@RequestHeader(Htt @ResponseStatus(HttpStatus.NO_CONTENT) @DeleteMapping - public void logOut( - @AuthenticationPrincipal User user - ) { + public void logOut(@AuthenticationPrincipal User user) { logOutUseCase.execute(user); } } diff --git a/src/main/java/com/bamdoliro/sinabro/shared/config/properties/GoogleOAuthProperties.java b/src/main/java/com/bamdoliro/sinabro/shared/config/properties/GoogleOAuthProperties.java index 2ea6144..d1d3bca 100644 --- a/src/main/java/com/bamdoliro/sinabro/shared/config/properties/GoogleOAuthProperties.java +++ b/src/main/java/com/bamdoliro/sinabro/shared/config/properties/GoogleOAuthProperties.java @@ -8,12 +8,10 @@ @Getter @Setter @Configuration -@ConfigurationProperties("auth.google") +@ConfigurationProperties("auth") public class GoogleOAuthProperties { - private OAuth web; - private OAuth android; - private OAuth ios; + private OAuth google; @Getter @Setter diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e2d08f1..006487b 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -35,15 +35,10 @@ spring: auth: google: - web: - base-url: ${GOOGLE_WEB_BASE_URL} - client-id: ${GOOGLE_WEB_CLIENT_ID} - client-secret: ${GOOGLE_WEB_CLIENT_SECRET} - redirect-uri: ${GOOGLE_WEB_REDIRECT_URI} - android: - client-id: ${GOOGLE_ANDROID_CLIENT_ID} - ios: - client-id: ${GOOGLE_IOS_CLIENT_ID} + base-url: ${GOOGLE_BASE_URL} + client-id: ${GOOGLE_CLIENT_ID} + client-secret: ${GOOGLE_CLIENT_SECRET} + redirect-uri: ${GOOGLE_REDIRECT_URI} jwt: refresh-expiration-time: 1296000000 # 15일 From 3010fa3db7500d3621a97a8cafa7dd0720375c4b Mon Sep 17 00:00:00 2001 From: cabbage16 Date: Sun, 24 Nov 2024 22:11:58 +0900 Subject: [PATCH 2/3] =?UTF-8?q?test(#26):=20=EC=9B=B9=20Google=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EA=B3=BC=EC=A0=95=20=ED=86=B5=ED=95=A9?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95=20-=20?= =?UTF-8?q?=EC=9B=B9=EC=97=90=EC=84=9C=20Google=20=EA=B3=84=EC=A0=95?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EA=B3=BC=EC=A0=95=EC=9D=B4=20=ED=86=B5=ED=95=A9?= =?UTF-8?q?=EB=90=98=EB=A9=B4=EC=84=9C=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=EB=A5=BC=20=EC=88=98=EC=A0=95=ED=96=88?= =?UTF-8?q?=EC=96=B4=EC=9A=94.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/auth/AuthControllerTest.java | 37 ++++--------------- .../sinabro/shared/util/ControllerTest.java | 18 +++------ 2 files changed, 13 insertions(+), 42 deletions(-) diff --git a/src/test/java/com/bamdoliro/sinabro/presentation/auth/AuthControllerTest.java b/src/test/java/com/bamdoliro/sinabro/presentation/auth/AuthControllerTest.java index 6413a06..9a904f8 100644 --- a/src/test/java/com/bamdoliro/sinabro/presentation/auth/AuthControllerTest.java +++ b/src/test/java/com/bamdoliro/sinabro/presentation/auth/AuthControllerTest.java @@ -41,50 +41,27 @@ class AuthControllerTest extends RestDocsTestSupport { verify(googleAuthLinkUseCase, times(1)).execute(); } - @Test - void 유저가_웹에서_구글_액세스_토큰을_발급받는다() throws Exception { - given(getGoogleAccessTokenUseCase.execute(any(String.class))).willReturn(AuthFixture.createGoogleToken()); - - mockMvc.perform(get("/auth/google") - .queryParam("code", AuthFixture.createGoogleOAuthCode()) - .accept(MediaType.APPLICATION_JSON) - ) - - .andExpect(status().isOk()) - - .andDo(restDocs.document( - queryParameters( - parameterWithName("code") - .description("Google OAuth 인증 코드. 리다이렉트시 url에 포함됨") - ) - )); - verify(getGoogleAccessTokenUseCase, times(1)).execute(any(String.class)); - } - @Test void 유저가_웹에서_구글로_로그인한다() throws Exception { - GoogleTokenRequest request = new GoogleTokenRequest(AuthFixture.createGoogleToken()); TokenResponse response = new TokenResponse(AuthFixture.createAccessTokenString(), AuthFixture.createRefreshTokenString()); - given(googleAuthWebUseCase.execute(any(GoogleTokenRequest.class))).willReturn(response); + given(googleAuthWebUseCase.execute(any(String.class))).willReturn(response); mockMvc.perform(post("/auth/google/web") + .queryParam("code", AuthFixture.createGoogleOAuthCode()) .accept(MediaType.APPLICATION_JSON) - .contentType(MediaType.APPLICATION_JSON) - .content(toJson(request)) ) .andExpect(status().isOk()) .andDo(restDocs.document( - requestFields( - fieldWithPath("token") - .type(JsonFieldType.STRING) - .description("구글에서 발급받은 액세스 토큰 혹은 아이디 토큰") + queryParameters( + parameterWithName("code") + .description("Google OAuth 인증 코드. 리다이렉트시 url에 포함됨") ) )); - verify(googleAuthWebUseCase, times(1)).execute(any(GoogleTokenRequest.class)); + verify(googleAuthWebUseCase, times(1)).execute(any(String.class)); } @Test @@ -106,7 +83,7 @@ class AuthControllerTest extends RestDocsTestSupport { requestFields( fieldWithPath("token") .type(JsonFieldType.STRING) - .description("구글에서 발급받은 액세스 토큰 혹은 아이디 토큰") + .description("구글에서 발급받은 아이디 토큰") ) )); diff --git a/src/test/java/com/bamdoliro/sinabro/shared/util/ControllerTest.java b/src/test/java/com/bamdoliro/sinabro/shared/util/ControllerTest.java index b49a9d9..04a3b77 100644 --- a/src/test/java/com/bamdoliro/sinabro/shared/util/ControllerTest.java +++ b/src/test/java/com/bamdoliro/sinabro/shared/util/ControllerTest.java @@ -1,25 +1,22 @@ package com.bamdoliro.sinabro.shared.util; -import com.bamdoliro.sinabro.application.auth.GoogleAuthLinkUseCase; -import com.bamdoliro.sinabro.application.auth.LogOutUseCase; -import com.bamdoliro.sinabro.application.auth.RefreshAccessTokenUseCase; import com.bamdoliro.sinabro.application.auth.*; import com.bamdoliro.sinabro.application.character.GetCharacterUseCase; -import com.bamdoliro.sinabro.application.diary.*; -import com.bamdoliro.sinabro.application.notification.SendNotificationToAllUserUseCase; -import com.bamdoliro.sinabro.application.question.*; -import com.bamdoliro.sinabro.domain.auth.service.TokenService; -import com.bamdoliro.sinabro.presentation.auth.AuthController; -import com.bamdoliro.sinabro.presentation.diary.DiaryController; import com.bamdoliro.sinabro.application.character.SelectCharacterUseCase; +import com.bamdoliro.sinabro.application.diary.*; import com.bamdoliro.sinabro.application.fcm.token.DeleteFCMTokenUseCase; import com.bamdoliro.sinabro.application.fcm.token.SaveFCMTokenUseCase; import com.bamdoliro.sinabro.application.letter.GenerateLetterUseCase; import com.bamdoliro.sinabro.application.letter.GetAllLetterUseCase; import com.bamdoliro.sinabro.application.letter.GetLetterUseCase; import com.bamdoliro.sinabro.application.notification.QueryNotificationListUseCase; +import com.bamdoliro.sinabro.application.notification.SendNotificationToAllUserUseCase; import com.bamdoliro.sinabro.application.notification.SendNotificationUseCase; +import com.bamdoliro.sinabro.application.question.*; +import com.bamdoliro.sinabro.domain.auth.service.TokenService; +import com.bamdoliro.sinabro.presentation.auth.AuthController; import com.bamdoliro.sinabro.presentation.character.CharacterController; +import com.bamdoliro.sinabro.presentation.diary.DiaryController; import com.bamdoliro.sinabro.presentation.fcm.token.FCMTokenController; import com.bamdoliro.sinabro.presentation.letter.LetterController; import com.bamdoliro.sinabro.presentation.notification.NotificationController; @@ -64,9 +61,6 @@ public abstract class ControllerTest { @MockBean protected GoogleAuthLinkUseCase googleAuthLinkUseCase; - @MockBean - protected GetGoogleAccessTokenUseCase getGoogleAccessTokenUseCase; - @MockBean protected GoogleAuthWebUseCase googleAuthWebUseCase; From 233f7acfe9baaca620ae6edc3e0a3a8ba8e1a753 Mon Sep 17 00:00:00 2001 From: cabbage16 Date: Sun, 24 Nov 2024 22:12:28 +0900 Subject: [PATCH 3/3] =?UTF-8?q?docs(#26):=20=EC=9B=B9=20Google=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EA=B3=BC=EC=A0=95=20=ED=86=B5=ED=95=A9?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20API=20=EB=AC=B8?= =?UTF-8?q?=EC=84=9C=20=EC=88=98=EC=A0=95=20-=20=EC=9B=B9=EC=97=90?= =?UTF-8?q?=EC=84=9C=20Google=20=EA=B3=84=EC=A0=95=EC=9C=BC=EB=A1=9C=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=ED=95=98=EB=8A=94=20=EA=B3=BC?= =?UTF-8?q?=EC=A0=95=EC=9D=B4=20=ED=86=B5=ED=95=A9=EB=90=98=EB=A9=B4?= =?UTF-8?q?=EC=84=9C=20API=20=EB=AC=B8=EC=84=9C=EB=A5=BC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=ED=96=88=EC=96=B4=EC=9A=94.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/docs/asciidoc/auth.adoc | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/src/docs/asciidoc/auth.adoc b/src/docs/asciidoc/auth.adoc index 2e9ac58..eb7882d 100644 --- a/src/docs/asciidoc/auth.adoc +++ b/src/docs/asciidoc/auth.adoc @@ -1,7 +1,7 @@ == 인증 Auth === Google 계정으로 로그인/회원가입 링크 발급 -구글 로그인/회원가입 링크를 발급받을 수 있습니다. +Google 계정으로 로그인 링크를 발급받을 수 있습니다. ==== 요청 형식 @@ -13,42 +13,37 @@ include::{snippets}/auth-controller-test/구글_로그인_링크를_발급받는 ===== 정상 응답 include::{snippets}/auth-controller-test/구글_로그인_링크를_발급받는다/http-response.adoc[] -=== Google Access Token 발급 -Google Authorization Code를 통해서 Access Token을 발급받을 수 있습니다. +=== 웹에서 Google 계정으로 로그인 +웹에서 Google 계정을 통해서 로그인/회원가입을 할 수 있습니다 ==== 요청 형식 ===== Query Parameter -include::{snippets}/auth-controller-test/유저가_웹에서_구글_액세스_토큰을_발급받는다/query-parameters.adoc[] +include::{snippets}/auth-controller-test/유저가_웹에서_구글로_로그인한다/query-parameters.adoc[] ==== 요청 -include::{snippets}/auth-controller-test/유저가_웹에서_구글_액세스_토큰을_발급받는다/http-request.adoc[] +include::{snippets}/auth-controller-test/유저가_웹에서_구글로_로그인한다/http-request.adoc[] -==== 응답 +===== 응답 ===== 정상 응답 -include::{snippets}/auth-controller-test/유저가_웹에서_구글_액세스_토큰을_발급받는다/response-body.adoc[] +include::{snippets}/auth-controller-test/유저가_웹에서_구글로_로그인한다/http-response.adoc[] -=== Google 계정으로 로그인 -Google 계정을 통해서 로그인/회원가입을 할 수 있습니다 +=== 앱에서 Google 계정으로 로그인 +앱에서 Google 계정을 통해서 로그인/회원가입을 할 수 있습니다 ==== 요청 형식 ===== Request Fields -include::{snippets}/auth-controller-test/유저가_웹에서_구글로_로그인한다/request-fields.adoc[] +include::{snippets}/auth-controller-test/유저가_앱에서_구글로_로그인한다/request-fields.adoc[] ==== 요청 - -===== 웹에서 로그인 하는 경우 -include::{snippets}/auth-controller-test/유저가_웹에서_구글로_로그인한다/http-request.adoc[] - -===== 앱에서 로그인 하는 경우 include::{snippets}/auth-controller-test/유저가_앱에서_구글로_로그인한다/http-request.adoc[] -===== 응답 +==== 응답 ===== 정상 응답 -include::{snippets}/auth-controller-test/유저가_웹에서_구글로_로그인한다/http-response.adoc[] +include::{snippets}/auth-controller-test/유저가_앱에서_구글로_로그인한다/http-response.adoc[] === 액세스 토큰 재발급 리프레시 토큰을 입력해 액세스 토큰을 재발급할 수 있습니다.