package be.simplenotes.domain.security import be.simplenotes.types.LoggedInUser import com.auth0.jwt.exceptions.JWTVerificationException import javax.inject.Singleton @Singleton class JwtPayloadExtractor(private val jwt: SimpleJwt) { operator fun invoke(token: String): LoggedInUser? = try { val decodedJWT = jwt.verifier.verify(token) val id = decodedJWT.getClaim(userIdField).asInt() ?: null val username = decodedJWT.getClaim(usernameField).asString() ?: null id?.let { username?.let { LoggedInUser(id, username) } } } catch (e: JWTVerificationException) { null } catch (e: IllegalArgumentException) { null } }