Jwt

object Jwt(source)

Entry point for the KJWT library.

Create a signed JWT (JWS):

val token = Jwt.builder()
.subject("user123")
.issuer("myapp")
.expiration(Clock.System.now() + 1.hours)
.signWith(JwsAlgorithm.HS256, hmacKey)

Parse and verify a signed JWT:

val jws = Jwt.parser()
.verifyWith(JwsAlgorithm.HS256, hmacKey)
.requireIssuer("myapp")
.build()
.parseSigned(token)
val subject = jws.payload.subject

Create an encrypted JWT (JWE):

val token = Jwt.builder()
.subject("user123")
.encryptWith(rsaPublicKey, JweKeyAlgorithm.RsaOaep256, JweContentAlgorithm.A256GCM)

Decrypt a JWE:

val jwe = Jwt.parser()
.decryptWith(rsaPrivateKey)
.build()
.parseEncrypted(token)

Note: This library depends on cryptography-core interfaces. You must register a CryptographyProvider before use. The recommended approach is to add cryptography-provider-optimal to your app dependencies — it auto-registers on startup.

Properties

Link copied to clipboard

Functions

Link copied to clipboard
fun builder(jsonInstance: Json = defaultJsonParser): JwtBuilder

Creates a new JwtBuilder for constructing JWS or JWE tokens.

Link copied to clipboard
fun parser(jsonInstance: Json = defaultJsonParser): JwtParserBuilder

Creates a new JwtParserBuilder for parsing and validating JWS or JWE tokens.