Skip to content

Commit

Permalink
refactor: Apply PicException, Pic ApiResponse with enum
Browse files Browse the repository at this point in the history
  • Loading branch information
210-reverof committed Jun 14, 2024
1 parent a53ad3e commit 57a8148
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -24,8 +19,8 @@ class AuthController(
@PostMapping("/login")
fun login(
@Valid @RequestBody loginRequest: LoginRequest
): ResponseEntity<LoginResponse> {
return ResponseEntity.ok(authApplicationService.login(loginRequest.toServiceRequest()))
): ApiResponse<LoginResponse> {
return ApiResponse.success(authApplicationService.login(loginRequest.toServiceRequest()))
}

}
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand All @@ -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<String, Any> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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<JwksResponse>()!!
}
Expand Down

0 comments on commit 57a8148

Please sign in to comment.