Skip to content

Commit

Permalink
feat: generate jwt token as util
Browse files Browse the repository at this point in the history
  • Loading branch information
ShiinaKin committed Sep 14, 2024
1 parent 31d8234 commit 1ddea5b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
20 changes: 6 additions & 14 deletions app/src/main/kotlin/io/sakurasou/service/auth/AuthServiceImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import io.sakurasou.exception.UserNotFoundException
import io.sakurasou.model.DatabaseSingleton.dbQuery
import io.sakurasou.model.dao.relation.RelationDao
import io.sakurasou.model.dao.user.UserDao
import io.sakurasou.model.entity.User
import io.sakurasou.util.JwtUtils
import kotlinx.datetime.Clock
import kotlinx.datetime.toJavaInstant
import java.security.KeyFactory
Expand All @@ -36,20 +38,10 @@ class AuthServiceImpl(
val isCorrectPassword = BCrypt.verifyer().verify(loginRequest.password.toCharArray(), user.password)
if (!isCorrectPassword.verified) throw UnauthorizedAccessException()

val role: List<String> = relationDao.listRoleByGroupId(user.groupId)
val roles: List<String> = dbQuery {
relationDao.listRoleByGroupId(user.groupId)
}

val publicKey = jwkProvider.get("6f8856ed-9189-488f-9011-0ff4b6c08edc").publicKey
val keySpecPKCS8 = PKCS8EncodedKeySpec(publicKey.encoded)
val privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpecPKCS8)
val token = JWT.create()
.withAudience(audience)
.withIssuer(issuer)
.withClaim("id", user.id)
.withClaim("username", user.name)
.withClaim("groupId", user.groupId)
.withClaim("role", role)
.withExpiresAt(Clock.System.now().plus(Duration.parse("3d")).toJavaInstant())
.sign(Algorithm.RSA256(publicKey as RSAPublicKey, privateKey as RSAPrivateKey))
return token
return JwtUtils.generateJwtToken(user, roles)
}
}
30 changes: 30 additions & 0 deletions app/src/main/kotlin/io/sakurasou/util/JwtUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.sakurasou.util

import com.auth0.jwt.JWT
import com.auth0.jwt.algorithms.Algorithm
import io.sakurasou.config.JwtConfig.audience
import io.sakurasou.config.JwtConfig.issuer
import io.sakurasou.config.JwtConfig.secret
import io.sakurasou.model.entity.User
import kotlinx.datetime.Clock
import kotlinx.datetime.toJavaInstant
import kotlin.time.Duration

/**
* @author ShiinaKin
* 2024/9/14 13:06
*/
object JwtUtils {
fun generateJwtToken(user: User, role: List<String>): String {
val token = JWT.create()
.withAudience(audience)
.withIssuer(issuer)
.withClaim("id", user.id)
.withClaim("username", user.name)
.withClaim("groupId", user.groupId)
.withClaim("role", role)
.withExpiresAt(Clock.System.now().plus(Duration.parse("3d")).toJavaInstant())
.sign(Algorithm.HMAC256(secret))
return token
}
}
8 changes: 4 additions & 4 deletions app/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ktor:
watch:
- classes
jwt:
secret: "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAtfJaLrzXILUg1U3N1KV8yJr92GHn5OtYZR7qWk1Mc4cy4JGjklYup7weMjBD9f3bBVoIsiUVX6xNcYIr0Ie0AQIDAQABAkEAg+FBquToDeYcAWBe1EaLVyC45HG60zwfG1S4S3IB+y4INz1FHuZppDjBh09jptQNd+kSMlG1LkAc/3znKTPJ7QIhANpyB0OfTK44lpH4ScJmCxjZV52mIrQcmnS3QzkxWQCDAiEA1Tn7qyoh+0rOO/9vJHP8U/beo51SiQMw0880a1UaiisCIQDNwY46EbhGeiLJR1cidr+JHl86rRwPDsolmeEF5AdzRQIgK3KXL3d0WSoS//K6iOkBX3KMRzaFXNnDl0U/XyeGMuUCIHaXv+n+Brz5BDnRbWS+2vkgIe9bUNlkiArpjWvX+2we"
issuer: "https://sakurasou.io/"
audience: "jwt-audience"
realm: "ktor sample app"
secret: $JWT_SECRET
issuer: $JWT_ISSUER
audience: $JWT_AUDIENCE
realm: $JWT_REALM

0 comments on commit 1ddea5b

Please sign in to comment.