Move user infos to JWT tokens #19

Merged
william merged 12 commits from feature/12-user-info-in-jwt into develop 2021-12-02 21:58:27 -05:00
1 changed files with 14 additions and 5 deletions
Showing only changes of commit 975ebae553 - Show all commits

View File

@ -12,11 +12,14 @@ data class Jwt(
val subject: String,
val secret: String,
val duration: Long? = null,
val signatureAlgorithm: SignatureAlgorithm = SignatureAlgorithm.HS512
val signatureAlgorithm: SignatureAlgorithm = SignatureAlgorithm.HS512,
val claims: Map<String, Any?> = mapOf()
) {
val token: String by lazy {
val builder = Jwts.builder()
.signWith(keyFromSecret(secret))
.setSubject(subject)
.addClaims(claims.filterValues { it != null })
duration?.let {
val expirationMs = System.currentTimeMillis() + it
@ -25,15 +28,21 @@ data class Jwt(
builder.setExpiration(expirationDate)
}
builder
.signWith(keyFromSecret(secret))
.compact()
builder.compact()
}
}
/** Build a [Jwt] for the given [User]. */
fun User.buildJwt(secret: String, duration: Long?) =
Jwt(this.id.toString(), secret, duration)
Jwt(
subject = this.id.toString(),
secret,
duration,
claims = mapOf(
"groupId" to this.group?.id,
"groupName" to this.group?.name
)
)
/** Parses the given [jwt] string. */
fun parseJwt(jwt: String, secret: String) =