From 57a8148910d89cfa308a43d9b21c7a55d07f5965 Mon Sep 17 00:00:00 2001 From: 210-reverof Date: Fri, 14 Jun 2024 13:04:07 +0900 Subject: [PATCH] refactor: Apply PicException, Pic ApiResponse with enum --- .../applicationService/dto/LoginServiceRequest.kt | 6 +++++- .../com/mashup/pic/auth/controller/AuthController.kt | 11 +++-------- .../mashup/pic/auth/controller/dto/LoginRequest.kt | 4 +++- .../com/mashup/pic/security/jwt/JwtTokenFilter.kt | 2 +- .../mashup/pic/security/oidc/KakaoIdTokenValidator.kt | 8 +++++--- .../com/mashup/pic/external/kakao/KakaoJwksClient.kt | 4 ++-- 6 files changed, 19 insertions(+), 16 deletions(-) diff --git a/pic-api/src/main/kotlin/com/mashup/pic/auth/applicationService/dto/LoginServiceRequest.kt b/pic-api/src/main/kotlin/com/mashup/pic/auth/applicationService/dto/LoginServiceRequest.kt index 12085f7..b458b1d 100644 --- a/pic-api/src/main/kotlin/com/mashup/pic/auth/applicationService/dto/LoginServiceRequest.kt +++ b/pic-api/src/main/kotlin/com/mashup/pic/auth/applicationService/dto/LoginServiceRequest.kt @@ -2,7 +2,11 @@ package com.mashup.pic.auth.applicationService.dto data class LoginServiceRequest( val idToken: String, - val provider: String, + val provider: LoginProvider, val nickname: String, val profileImage: String ) + +enum class LoginProvider { + KAKAO, NAVER, GOOGLE +} diff --git a/pic-api/src/main/kotlin/com/mashup/pic/auth/controller/AuthController.kt b/pic-api/src/main/kotlin/com/mashup/pic/auth/controller/AuthController.kt index ce8e8ec..5c42407 100644 --- a/pic-api/src/main/kotlin/com/mashup/pic/auth/controller/AuthController.kt +++ b/pic-api/src/main/kotlin/com/mashup/pic/auth/controller/AuthController.kt @@ -5,15 +5,10 @@ import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import com.mashup.pic.auth.applicationService.AuthApplicationService -import com.mashup.pic.auth.applicationService.dto.LoginServiceRequest import com.mashup.pic.auth.controller.dto.LoginRequest import com.mashup.pic.auth.controller.dto.LoginResponse -import com.mashup.pic.domain.user.User -import com.mashup.pic.security.authentication.UserInfo +import com.mashup.pic.common.ApiResponse import jakarta.validation.Valid -import org.springframework.http.ResponseEntity -import org.springframework.security.core.annotation.AuthenticationPrincipal - @RestController @RequestMapping("/api/v1/auth") @@ -24,8 +19,8 @@ class AuthController( @PostMapping("/login") fun login( @Valid @RequestBody loginRequest: LoginRequest - ): ResponseEntity { - return ResponseEntity.ok(authApplicationService.login(loginRequest.toServiceRequest())) + ): ApiResponse { + return ApiResponse.success(authApplicationService.login(loginRequest.toServiceRequest())) } } diff --git a/pic-api/src/main/kotlin/com/mashup/pic/auth/controller/dto/LoginRequest.kt b/pic-api/src/main/kotlin/com/mashup/pic/auth/controller/dto/LoginRequest.kt index d48a228..a5c9f73 100644 --- a/pic-api/src/main/kotlin/com/mashup/pic/auth/controller/dto/LoginRequest.kt +++ b/pic-api/src/main/kotlin/com/mashup/pic/auth/controller/dto/LoginRequest.kt @@ -1,14 +1,16 @@ package com.mashup.pic.auth.controller.dto +import com.mashup.pic.auth.applicationService.dto.LoginProvider import com.mashup.pic.auth.applicationService.dto.LoginServiceRequest import jakarta.validation.constraints.NotBlank data class LoginRequest( @NotBlank val idToken: String, - @NotBlank val provider: String, + @NotBlank val provider: LoginProvider, @NotBlank val nickname: String, @NotBlank val profileImage: String ) { + fun toServiceRequest(): LoginServiceRequest { return LoginServiceRequest( idToken = idToken, diff --git a/pic-api/src/main/kotlin/com/mashup/pic/security/jwt/JwtTokenFilter.kt b/pic-api/src/main/kotlin/com/mashup/pic/security/jwt/JwtTokenFilter.kt index f44a47c..c7a4782 100644 --- a/pic-api/src/main/kotlin/com/mashup/pic/security/jwt/JwtTokenFilter.kt +++ b/pic-api/src/main/kotlin/com/mashup/pic/security/jwt/JwtTokenFilter.kt @@ -40,7 +40,7 @@ class JwtTokenFilter( private fun extractToken(authorizationHeader: String): String { return authorizationHeader.takeIf { hasValidBearer(it) }?.substring(BEARER_PREFIX.length) - ?: throw BadCredentialsException("Wrong bearer prefix") // TODO: Replace Exception to Pic exception message + ?: throw BadCredentialsException("Wrong bearer prefix") } private fun setAuthentication(token: String) { diff --git a/pic-api/src/main/kotlin/com/mashup/pic/security/oidc/KakaoIdTokenValidator.kt b/pic-api/src/main/kotlin/com/mashup/pic/security/oidc/KakaoIdTokenValidator.kt index 3298aea..66b7ae8 100644 --- a/pic-api/src/main/kotlin/com/mashup/pic/security/oidc/KakaoIdTokenValidator.kt +++ b/pic-api/src/main/kotlin/com/mashup/pic/security/oidc/KakaoIdTokenValidator.kt @@ -1,6 +1,8 @@ package com.mashup.pic.security.oidc import com.fasterxml.jackson.databind.ObjectMapper +import com.mashup.pic.common.exception.PicException +import com.mashup.pic.common.exception.PicExceptionType import com.mashup.pic.external.common.response.JwkKey import com.mashup.pic.external.kakao.KakaoJwksClient import io.jsonwebtoken.Jwts @@ -33,7 +35,7 @@ class KakaoIdTokenValidator( private fun extractSub(idToken: String): String { val payload = decodePayload(idToken) - return payload[SUB_KEY] as String? ?: throw Exception("SUB 없음") + return payload[SUB_KEY] as String? ?: throw PicException.of(PicExceptionType.ARGUMENT_NOT_VALID,"Can't extract SUB") } private fun verifyPayload(idToken: String, nickname: String) { @@ -54,7 +56,7 @@ class KakaoIdTokenValidator( private fun extractKid(idToken: String): String { val header = decodeHeader(idToken) - return header[KID_KEY] as String? ?: throw Exception("KID 없음") + return header[KID_KEY] as String? ?: throw PicException.of(PicExceptionType.ARGUMENT_NOT_VALID,"Can't extract KID") } private fun getPublicKey(kid: String): Key { @@ -68,7 +70,7 @@ class KakaoIdTokenValidator( private fun getJwkByKid(kid: String): JwkKey { return kakaoJwksClient.getJwks().getJwkKeyByKid(kid) ?: kakaoJwksClient.refreshAndGetJwks().getJwkKeyByKid(kid) - ?: throw Exception("공개키를 가져올 수 없음") + ?: throw PicException.of(PicExceptionType.ARGUMENT_NOT_VALID,"Can't find the Jwk matching the KID") } private fun decodePayload(idToken: String): Map { diff --git a/pic-external/src/main/kotlin/com/mashup/pic/external/kakao/KakaoJwksClient.kt b/pic-external/src/main/kotlin/com/mashup/pic/external/kakao/KakaoJwksClient.kt index 47e72fb..bdc69e7 100644 --- a/pic-external/src/main/kotlin/com/mashup/pic/external/kakao/KakaoJwksClient.kt +++ b/pic-external/src/main/kotlin/com/mashup/pic/external/kakao/KakaoJwksClient.kt @@ -1,7 +1,6 @@ package com.mashup.pic.external.kakao import com.mashup.pic.external.common.JwksClient -import com.mashup.pic.external.common.response.JwkKey import com.mashup.pic.external.common.response.JwksResponse import org.springframework.beans.factory.annotation.Value import org.springframework.cache.annotation.CachePut @@ -10,6 +9,7 @@ import org.springframework.http.HttpStatusCode import org.springframework.stereotype.Component import org.springframework.web.client.RestClient import org.springframework.web.client.body +import java.io.IOException @Component @@ -33,7 +33,7 @@ class KakaoJwksClient( .uri(jwkUri) .retrieve() .onStatus(HttpStatusCode::is4xxClientError) { _, response -> - // TODO: throw Pic custom runtime exception + throw IOException("Error fetching JWKS: ${response.statusCode}") } .body()!! }