commonMain.com.appstractive.jwt.JWT.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jwt-kt-jvm Show documentation
Show all versions of jwt-kt-jvm Show documentation
JWT creating, parsing, signing and verifying implementation for Kotlin Multiplatform
The newest version!
package com.appstractive.jwt
import com.appstractive.jwt.utils.urlEncoded
@DslMarker @Target(AnnotationTarget.CLASS, AnnotationTarget.TYPE) annotation class JwtDsl
/** @see JSON Web Token (JWT) */
data class UnsignedJWT(
val header: Header,
val claims: Claims,
) {
override fun toString(): String {
return listOf(
header.urlEncoded(),
claims.urlEncoded(),
"",
)
.joinToString(".")
}
}
/** @see JSON Web Token (JWT) */
data class JWT(
val header: Header,
val claims: Claims,
val signature: ByteArray,
) {
override fun toString(): String {
return listOf(
header.urlEncoded(),
claims.urlEncoded(),
urlEncoded(signature),
)
.joinToString(".")
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || this::class != other::class) return false
other as JWT
if (header != other.header) return false
if (claims != other.claims) return false
if (!signature.contentEquals(other.signature)) return false
return true
}
override fun hashCode(): Int {
var result = header.hashCode()
result = 31 * result + claims.hashCode()
result = 31 * result + signature.contentHashCode()
return result
}
companion object
}
fun jwt(builder: JwtBuilder.() -> Unit): UnsignedJWT {
val jwtBuilder = JwtBuilder().apply(builder)
return jwtBuilder.build()
}
@JwtDsl
class JwtBuilder {
private var header: HeaderBuilder = HeaderBuilder()
private var claims: ClaimsBuilder? = null
fun header(header: HeaderBuilder.() -> Unit) {
this.header = HeaderBuilder().apply(header)
}
fun claims(builder: ClaimsBuilder.() -> Unit) {
this.claims = ClaimsBuilder().apply(builder)
}
internal fun build(): UnsignedJWT {
val header = header.build()
val claims = claims?.build() ?: Claims(emptyMap())
return UnsignedJWT(
header = header,
claims = claims,
)
}
}