kotlin-jwt

JWT creation library for Kotlin

License

License

Categories

Categories

Kotlin Languages
GroupId

GroupId

com.brendangoldberg
ArtifactId

ArtifactId

kotlin-jwt
Last Version

Last Version

1.3.1
Release Date

Release Date

Type

Type

pom.sha512
Description

Description

kotlin-jwt
JWT creation library for Kotlin
Project URL

Project URL

https://github.com/brendangoldberg/kotlin-jwt
Source Code Management

Source Code Management

https://github.com/brendangoldberg/kotlin-jwt

Download kotlin-jwt

Dependencies

runtime (2)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-stdlib jar 1.4.30
org.jetbrains.kotlinx : kotlinx-serialization-json-jvm jar 1.1.0

Project Modules

There are no modules declared in this project.

Kotlin JWT Library

License

This is a JSON Web Token (JWT) library written fully in Kotlin utilizing Kotlin Serializer.

For introduction to JWTs please visit https://jwt.io/introduction/.

Features

Examples

import com.brendangoldberg.kotlin_jwt.KtJwtCreator
import com.brendangoldberg.kotlin_jwt.KtJwtDecoder
import com.brendangoldberg.kotlin_jwt.KtJwtVerifier
import com.brendangoldberg.kotlin_jwt.algorithms.HSAlgorithm
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class CustomClaim(
    @SerialName("my_custom_value") val customValue: String
)

fun main() {
    // Declare which signing algorithm to use see import com.brendangoldberg.kotlin_jwt.algorithms.* for available algorithms.
    val algorithm = HSAlgorithm.HS256("my-super-secret")

    val customClaim = CustomClaim("myCustomClaim")

    // Create JWT
    val jwt = KtJwtCreator.init().addClaim("custom_claim", customClaim, CustomClaim.serializer()).sign(algorithm)

    // Verify JWT
    val verified = KtJwtVerifier(algorithm).verify(jwt)

    println("verified: $verified")

    // Decode JWT
    val decoded = KtJwtDecoder.decode(jwt)

    println("custom claim: ${decoded.getClaim("custom_claim", CustomClaim.serializer())}")
}

Setup

Gradle

All versions can be found in the maven repo, or maven search.

repositories { 
  mavenCentral()
}

dependencies {
  implementation("com.brendangoldberg.kotlin-jwt:<latest-version>")

}

Versions

Version
1.3.1
1.3.0
1.2.0