All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.dev.neeffect.nee.security.jwt.UserCoder.kt Maven / Gradle / Ivy

The newest version!
package dev.neeffect.nee.security.jwt

import io.fusionauth.jwt.domain.JWT
import io.vavr.collection.Map
import io.vavr.control.Option
import io.vavr.kotlin.toVavrMap

interface UserCoder {
    fun userToIdAndMapAnd(u: USER): Pair>
    fun mapToUser(id: String, m: Map): Option
    fun hasRole(u: USER, r: ROLE): Boolean
}

class JwtUsersCoder(val jwtCoder: JwtCoder, val coder: UserCoder) {
    fun encodeUser(user: USER): JWT = coder.userToIdAndMapAnd(user).let { (id, mapClaims) ->
        jwtCoder.createJwt(id, mapClaims)
    }

    @Suppress("MutableCollections")
    fun decodeUser(jwt: JWT): Option =
        coder.mapToUser(jwt.subject, jwt.allClaims.toVavrMap().mapValues { it.toString() })

    fun hasRole(u: USER, r: ROLE): Boolean = coder.hasRole(u, r)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy