Reduce cookie size
This commit is contained in:
@@ -10,8 +10,8 @@ data class JwtPayload(val userId: Int, val username: String) {
|
||||
class JwtPayloadExtractor(private val jwt: SimpleJwt) {
|
||||
operator fun invoke(token: String): JwtPayload? = try {
|
||||
val decodedJWT = jwt.verifier.verify(token)
|
||||
val id = decodedJWT.getClaim("id").asInt() ?: null
|
||||
val username = decodedJWT.getClaim("username").asString() ?: null
|
||||
val id = decodedJWT.getClaim(userIdField).asInt() ?: null
|
||||
val username = decodedJWT.getClaim(usernameField).asString() ?: null
|
||||
id?.let { username?.let { JwtPayload(id, username) } }
|
||||
} catch (e: JWTVerificationException) {
|
||||
null
|
||||
|
||||
@@ -7,14 +7,17 @@ import com.auth0.jwt.algorithms.Algorithm
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
|
||||
internal const val userIdField = "i"
|
||||
internal const val usernameField = "u"
|
||||
|
||||
class SimpleJwt(jwtConfig: JwtConfig) {
|
||||
private val validityInMs = TimeUnit.MILLISECONDS.convert(jwtConfig.validity, jwtConfig.timeUnit)
|
||||
private val algorithm = Algorithm.HMAC256(jwtConfig.secret)
|
||||
|
||||
val verifier: JWTVerifier = JWT.require(algorithm).build()
|
||||
fun sign(jwtPayload: JwtPayload): String = JWT.create()
|
||||
.withClaim("id", jwtPayload.userId)
|
||||
.withClaim("username", jwtPayload.username)
|
||||
.withClaim(userIdField, jwtPayload.userId)
|
||||
.withClaim(usernameField, jwtPayload.username)
|
||||
.withExpiresAt(getExpiration())
|
||||
.sign(algorithm)
|
||||
|
||||
|
||||
@@ -21,8 +21,8 @@ internal class JwtPayloadExtractorTest {
|
||||
private fun createToken(username: String? = null, id: Int? = null, secret: String = jwtConfig.secret): Token {
|
||||
val algo = Algorithm.HMAC256(secret)
|
||||
return JWT.create().apply {
|
||||
username?.let { withClaim("username", it) }
|
||||
id?.let { withClaim("id", it) }
|
||||
username?.let { withClaim(usernameField, it) }
|
||||
id?.let { withClaim(userIdField, it) }
|
||||
}.sign(algo)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user