Skip to content

Commit

Permalink
[BLOOM-093] OAuth 로그 추가 (#94)
Browse files Browse the repository at this point in the history
* refactor: ClientCallException -> ExternalServerException 변경

* refactor: idToken log 추가
  • Loading branch information
stophwan authored Aug 31, 2024
1 parent 014541d commit 3395316
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dnd11th.blooming.api.service.user.oauth
import dnd11th.blooming.client.oauth.OidcPublicKeys
import dnd11th.blooming.common.exception.ErrorType
import dnd11th.blooming.common.exception.UnAuthorizedException
import dnd11th.blooming.common.util.Logger.Companion.log
import io.jsonwebtoken.Claims
import io.jsonwebtoken.Jwts
import org.springframework.stereotype.Component
Expand All @@ -18,7 +19,12 @@ class IdTokenValidator(
oidcPublicKeys: OidcPublicKeys,
): Claims {
val publicKey: PublicKey = publicKeyGenerator.generatePublicKey(header, oidcPublicKeys)
return Jwts.parser().verifyWith(publicKey).build().parseSignedClaims(idToken).payload
return try {
Jwts.parser().verifyWith(publicKey).build().parseSignedClaims(idToken).payload
} catch (e: Exception) {
log.error { "Signature verification failed: ${e.message}" }
throw e
}
}

fun verifyPayload(
Expand All @@ -27,9 +33,16 @@ class IdTokenValidator(
aud: String,
) {
payload.apply {
require(iss == this["iss"]) { throw UnAuthorizedException(ErrorType.INVALID_ID_TOKEN) }
require(aud == this["aud"]) { throw UnAuthorizedException(ErrorType.INVALID_ID_TOKEN) }
require(iss == this["iss"]) {
log.error { "iss is $iss but iss in payload is ${this["iss"]}" }
throw UnAuthorizedException(ErrorType.INVALID_ID_TOKEN)
}
require(aud == this["aud"]) {
log.error { "aud is $aud but aud in payload is ${this["aud"]}" }
throw UnAuthorizedException(ErrorType.INVALID_ID_TOKEN)
}
require((this["exp"] as Number).toLong() >= System.currentTimeMillis() / 1000) {
log.error { "token is expired exp in payload is ${this["exp"]}" }
throw UnAuthorizedException(ErrorType.INVALID_ID_TOKEN)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package dnd11th.blooming.client.oauth

import dnd11th.blooming.common.exception.ErrorType
import dnd11th.blooming.common.exception.ExternalServerException

data class OidcPublicKeys(
val keys: List<OidcPublicKey>,
) {
Expand All @@ -8,6 +11,6 @@ data class OidcPublicKeys(
alg: String,
): OidcPublicKey {
return keys.firstOrNull { it.kid == kid && it.alg == alg }
?: throw IllegalArgumentException("JWT 값의 kid 또는 alg 정보가 올바르지 않습니다.")
?: throw ExternalServerException(ErrorType.INVALID_MATCHING_KEY)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dnd11th.blooming.client.weather

import dnd11th.blooming.common.exception.ClientCallException
import dnd11th.blooming.common.exception.ErrorType
import dnd11th.blooming.common.exception.ExternalServerException

data class WeatherResponse(
val response: ResponseData,
Expand All @@ -13,7 +13,7 @@ data class WeatherResponse(

private fun validate() {
if (response.header.resultCode != "00") {
throw ClientCallException(ErrorType.OPEN_API_CALL_EXCEPTION)
throw ExternalServerException(ErrorType.OPEN_API_CALL_EXCEPTION)
}
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,16 @@ enum class ErrorType(val status: HttpStatus, var message: String, val logLevel:

// Auth
INVALID_JWT_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 토큰입니다", LogLevel.DEBUG),
INVALID_ID_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 ID TOKEN입니다.", LogLevel.DEBUG),
INVALID_ID_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 ID TOKEN입니다.", LogLevel.WARN),
INVALID_MATCHING_KEY(HttpStatus.BAD_GATEWAY, "응답값과 매칭되는 키가 존재하지 않습니다.", LogLevel.WARN),

INVALID_OAUTH_PROVIDER(HttpStatus.BAD_REQUEST, "지원하지 않는 provider입니다", LogLevel.DEBUG),

// User
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "존재하지 않은 사용자입니다.", LogLevel.DEBUG),

// OpenAPI
OPEN_API_CALL_EXCEPTION(HttpStatus.BAD_REQUEST, "OpenAPI 호출에 실패했습니다", LogLevel.WARN),
OPEN_API_CALL_EXCEPTION(HttpStatus.BAD_GATEWAY, "OpenAPI 호출에 실패했습니다", LogLevel.WARN),

// REGION
NOT_FOUND_REGION(HttpStatus.NOT_FOUND, "존재하지 않는 지역번호입니다.", LogLevel.DEBUG),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package dnd11th.blooming.common.exception

class ExternalServerException(errorType: ErrorType) : MyException(errorType)
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ class GlobalExceptionHandler {
val errorType = exception.errorType
when (errorType.logLevel) {
LogLevel.ERROR -> {
log.error { "${"Blooming Exception : {}"} ${errorType.message} $exception" }
log.error { "Blooming Exception: ${errorType.message}, Exception: $exception" }
}
LogLevel.WARN -> {
log.warn { "${"Blooming Exception : {}"} ${errorType.message} $exception" }
log.warn { "Blooming Exception: ${errorType.message}, Exception: $exception" }
}
else -> {
log.info { "${"Blooming Exception : {}"} ${errorType.message} $exception" }
log.info { "Blooming Exception: ${errorType.message}, Exception: $exception" }
}
}
return ResponseEntity
Expand Down

0 comments on commit 3395316

Please sign in to comment.