Skip to content

Commit

Permalink
fix: JwtConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Jul 24, 2024
1 parent dfe9640 commit c35d16a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 36 deletions.
20 changes: 0 additions & 20 deletions src/main/kotlin/com/hero/alignlab/config/jwt/JwtConfig.kt

This file was deleted.

35 changes: 35 additions & 0 deletions src/main/kotlin/com/hero/alignlab/config/jwt/JwtProperties.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.hero.alignlab.config.jwt

import com.hero.alignlab.domain.auth.application.JwtTokenService
import io.github.oshai.kotlinlogging.KotlinLogging
import jakarta.validation.constraints.NotBlank
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration

@Configuration
@EnableConfigurationProperties(JwtConfig.JwtProperties::class)
class JwtConfig {
private val logger = KotlinLogging.logger {}

@ConfigurationProperties(prefix = "auth.jwt")
data class JwtProperties(
@field:NotBlank
var secret: String = "",
@field:NotBlank
var accessExp: Int = 0,
@field:NotBlank
var refreshExp: Int = 0,
val issuer: String = "hero-alignlab-api",
val audience: String = "hero-alignlab-api",
)

@Bean
fun jwtTokenService(jwtProperties: JwtProperties): JwtTokenService {
logger.info { "initialized jwtTokenService. $jwtProperties" }
return JwtTokenService(jwtProperties)
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,49 @@ package com.hero.alignlab.domain.auth.application
import com.auth0.jwt.JWT
import com.auth0.jwt.algorithms.Algorithm
import com.fasterxml.jackson.module.kotlin.readValue
import com.hero.alignlab.common.extension.decodeBase64
import com.hero.alignlab.common.extension.mapper
import com.hero.alignlab.common.extension.toInstant
import com.hero.alignlab.config.jwt.JwtConfig
import com.hero.alignlab.domain.auth.model.AuthUserToken
import com.hero.alignlab.domain.auth.model.AuthUserTokenPayload
import com.hero.alignlab.exception.ErrorCode
import com.hero.alignlab.exception.InvalidTokenException
import com.hero.alignlab.common.extension.decodeBase64
import com.hero.alignlab.common.extension.mapper
import com.hero.alignlab.common.extension.toInstant
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.stereotype.Service
import reactor.core.publisher.Mono
import java.time.LocalDateTime
import java.util.*

private const val ACCESS_TOKEN = "accessToken"

@Service
class JwtTokenService(
private val jwtConfig: JwtConfig,
private val jwtProperties: JwtConfig.JwtProperties,
) {
private val logger = KotlinLogging.logger {}

private val accessJwtVerifier = JWT
.require(Algorithm.HMAC256(jwtConfig.secret))
.withIssuer(jwtConfig.issuer)
.withAudience(jwtConfig.audience)
.require(Algorithm.HMAC256(jwtProperties.secret))
.withIssuer(jwtProperties.issuer)
.withAudience(jwtProperties.audience)
.withClaim("type", ACCESS_TOKEN)
.build()

private val accessJwtVerifierWithExtendedExpiredAt = JWT
.require(Algorithm.HMAC256(jwtConfig.secret))
.withIssuer(jwtConfig.issuer)
.withAudience(jwtConfig.audience)
.require(Algorithm.HMAC256(jwtProperties.secret))
.withIssuer(jwtProperties.issuer)
.withAudience(jwtProperties.audience)
.withClaim("type", ACCESS_TOKEN)
.acceptExpiresAt(jwtConfig.refreshExp.toLong())
.acceptExpiresAt(jwtProperties.refreshExp.toLong())
.build()

fun createToken(id: Long, tokenExpiredAt: LocalDateTime): String {
return JWT.create().apply {
this.withIssuer(jwtConfig.issuer)
this.withAudience(jwtConfig.audience)
this.withIssuer(jwtProperties.issuer)
this.withAudience(jwtProperties.audience)
this.withClaim("id", id)
this.withClaim("type", ACCESS_TOKEN)
this.withExpiresAt(Date.from(tokenExpiredAt.toInstant()))
}.sign(Algorithm.HMAC256(jwtConfig.secret))
}.sign(Algorithm.HMAC256(jwtProperties.secret))
}

fun verifyToken(token: AuthUserToken): AuthUserTokenPayload {
Expand Down

0 comments on commit c35d16a

Please sign in to comment.