JWT Kotlin Multiplatform


A kotlin multiplatform library for creating, parsing, signing and verifying JWTs.
Supported Algorithms
- HS256
- HS384
- HS512
- RS256
- RS384
- RS512
- PS256
- PS384
- PS512
- ES256
- ES384
- ES512
Usage
Installation
Gradle:
commonMain.dependencies {
implementation("com.appstractive:jwt-kt:1.2.0")
}
Create JWT
val jwt: UnsignedJWT = jwt {
claims {
issuer =
subject =
audience =
expires(Clock.System.now() + hours)
notBefore()
issuedAt()
id =
claim(, )
claim(, )
claim(, )
claim(, )
claim(, )
objectClaim() { put(, JsonPrimitive()) }
arrayClaim() {
add(JsonPrimitive())
add(JsonPrimitive())
add(JsonPrimitive())
}
}
}
Parse JWT
val jwt = JWT.from("eyJraWQiOiJzLWRmZmZkMDJlLTlhNDItNDQzMC1hNT...")
val userId = jwt.subject
Sign JWT
For specific algorithms see:
Verify JWT
val jwt = JWT.from("eyJraWQiOiJzLWRmZmZkMDJlLTlhNDItNDQzMC1hNT...")
val isValid = jwt.verify {
issuer("example.com")
audience("api.example.com")
expiresAt()
notBefore()
}
For signature verification see:
License
Copyright 2024 Andreas Schulz.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance the License.
You may obtain a copy of the License at
http:
Unless applicable law agreed to writing, software
distributed under the License distributed an BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
See the License the specific language governing permissions
limitations under the License.