From bf1fae3cf41494ec57f362f35ad33c94b6bff524 Mon Sep 17 00:00:00 2001 From: seeunjang Date: Mon, 20 May 2024 19:29:10 +0900 Subject: [PATCH] =?UTF-8?q?refactor=20:=20=EB=A1=9C=EA=B7=B8=EC=95=84?= =?UTF-8?q?=EC=9B=83=20API=20=EC=9A=94=EC=B2=AD=20=ED=98=95=EC=8B=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD(#108)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/application/dto/Logout.kt | 9 --------- .../domain/user/application/service/AuthService.kt | 9 +++++---- .../apimodule/domain/user/presentation/AuthApi.kt | 4 +++- .../domain/user/presentation/AuthController.kt | 14 ++++---------- .../domain/user/presentation/AuthControllerTest.kt | 14 ++++++++------ 5 files changed, 20 insertions(+), 30 deletions(-) delete mode 100644 Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/dto/Logout.kt diff --git a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/dto/Logout.kt b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/dto/Logout.kt deleted file mode 100644 index 8a9af1e3..00000000 --- a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/dto/Logout.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.bamyanggang.apimodule.domain.user.application.dto - -class Logout { - - data class Request( - val refreshToken: String - ) - -} \ No newline at end of file diff --git a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/AuthService.kt b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/AuthService.kt index c69f62bf..b00a0d64 100644 --- a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/AuthService.kt +++ b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/application/service/AuthService.kt @@ -1,6 +1,5 @@ package com.bamyanggang.apimodule.domain.user.application.service -import com.bamyanggang.apimodule.domain.user.application.dto.Logout import com.bamyanggang.apimodule.domain.user.application.dto.Reissue import com.bamyanggang.apimodule.domain.user.application.dto.SocialLogin import com.bamyanggang.apimodule.domain.user.application.service.handler.AuthHandler @@ -26,7 +25,8 @@ class AuthService( private val tokenAppender: TokenAppender, private val claimsExtractor: ClaimsExtractor, private val tokenRemover: TokenRemover, - private val tokenReader : TokenReader + private val tokenReader : TokenReader, + private val tokenExtractor: TokenExtractor ){ fun executeSocialLogin(provider: SocialLoginProvider, request: SocialLogin.Request): SocialLogin.Response { val socialLoginHandler = authHandlerManager.getHandler(provider) @@ -63,7 +63,8 @@ class AuthService( } @Transactional - fun logout(logoutRequest: Logout.Request) { - tokenReader.readToken(logoutRequest.refreshToken).also { tokenRemover.removeToken(it) } + fun logout(refreshToken: String) { + val refreshToken = tokenExtractor.extractValue(refreshToken) + tokenReader.readToken(refreshToken).also { tokenRemover.removeToken(it) } } } diff --git a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthApi.kt b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthApi.kt index 592babb6..0a8108be 100644 --- a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthApi.kt +++ b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthApi.kt @@ -7,4 +7,6 @@ object AuthApi { const val LOGOUT = "$BASE_URL/logout" const val REISSUE = "$BASE_URL/reissue" -} \ No newline at end of file + const val REFRESH_TOKEN_HEADER = "RefreshToken" + +} diff --git a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthController.kt b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthController.kt index 5e5a6f70..21171d5a 100644 --- a/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthController.kt +++ b/Api-Module/src/main/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthController.kt @@ -1,16 +1,10 @@ package com.bamyanggang.apimodule.domain.user.presentation -import com.bamyanggang.apimodule.domain.user.application.dto.Logout import com.bamyanggang.apimodule.domain.user.application.dto.Reissue import com.bamyanggang.apimodule.domain.user.application.dto.SocialLogin import com.bamyanggang.apimodule.domain.user.application.service.AuthService import com.bamyanggang.domainmodule.domain.user.enums.SocialLoginProvider -import org.springframework.web.bind.annotation.DeleteMapping -import org.springframework.web.bind.annotation.PathVariable -import org.springframework.web.bind.annotation.PostMapping -import org.springframework.web.bind.annotation.PutMapping -import org.springframework.web.bind.annotation.RequestBody -import org.springframework.web.bind.annotation.RestController +import org.springframework.web.bind.annotation.* @RestController class AuthController( @@ -30,9 +24,9 @@ class AuthController( @DeleteMapping(AuthApi.LOGOUT) fun logout( - @RequestBody request: Logout.Request + @RequestHeader(AuthApi.REFRESH_TOKEN_HEADER) refreshTokenHeader: String ) { - authService.logout(request) + authService.logout(refreshTokenHeader) } -} \ No newline at end of file +} diff --git a/Api-Module/src/test/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthControllerTest.kt b/Api-Module/src/test/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthControllerTest.kt index f35343cd..b5d017c7 100644 --- a/Api-Module/src/test/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthControllerTest.kt +++ b/Api-Module/src/test/kotlin/com/bamyanggang/apimodule/domain/user/presentation/AuthControllerTest.kt @@ -1,7 +1,6 @@ package com.bamyanggang.apimodule.domain.user.presentation import com.bamyanggang.apimodule.BaseRestDocsTest -import com.bamyanggang.apimodule.domain.user.application.dto.Logout import com.bamyanggang.apimodule.domain.user.application.dto.Reissue import com.bamyanggang.apimodule.domain.user.application.dto.SocialLogin import com.bamyanggang.apimodule.domain.user.application.service.AuthService @@ -18,6 +17,8 @@ import org.springframework.restdocs.request.RequestDocumentation.parameterWithNa import org.springframework.restdocs.request.RequestDocumentation.pathParameters import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status import com.bamyanggang.commonmodule.fixture.generateFixture +import org.springframework.restdocs.headers.HeaderDocumentation.headerWithName +import org.springframework.restdocs.headers.HeaderDocumentation.requestHeaders @WebMvcTest(AuthController::class) class AuthControllerTest : BaseRestDocsTest(){ @@ -125,19 +126,20 @@ class AuthControllerTest : BaseRestDocsTest(){ @DisplayName("로그아웃 요청시 성공한다.") fun logout() { //given - val refreshToken : String= generateFixture() - val logoutRequest = Reissue.Request(refreshToken) val request = RestDocumentationRequestBuilders.delete(AuthApi.LOGOUT) + .header("Authorization","Bearer accessToken") + .header("RefreshToken","Bearer refreshToken") .contentType(MediaType.APPLICATION_JSON_VALUE) - .content(objectMapper.writeValueAsString(logoutRequest)) + //when val result = mockMvc.perform(request) //then result.andExpect(status().isOk) .andDo( resultHandler.document( - requestFields( - fieldWithPath("refreshToken").description("로그아웃을 위한 refreshToken"), + requestHeaders( + headerWithName("Authorization").description("Access Token"), + headerWithName("RefreshToken").description("refreshToken") ) ) )