-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c72dba6
commit 6ba6106
Showing
5 changed files
with
67 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,41 @@ | ||
package klite.oauth | ||
|
||
import klite.base64Decode | ||
import klite.Email | ||
import klite.base64UrlDecode | ||
import klite.json.* | ||
import java.time.Instant | ||
import java.util.* | ||
|
||
data class JWT(val token: String) { | ||
private val parts = token.split(".").map { it.base64Decode().decodeToString() } | ||
val header get() = parts[0] | ||
val payload get() = parts[1] | ||
companion object { | ||
private val jsonMapper = JsonMapper() | ||
} | ||
|
||
private val parts = token.split(".").map { it.base64UrlDecode().decodeToString() } | ||
val headerJson get() = parts[0] | ||
val payloadJson get() = parts[1] | ||
val signature get() = parts[2] | ||
|
||
val header by lazy { Header(jsonMapper.parse<JsonNode>(headerJson)) } | ||
val payload by lazy { Payload(jsonMapper.parse<JsonNode>(payloadJson)) } | ||
|
||
data class Header(val fields: JsonNode): JsonNode by fields { | ||
val alg by fields | ||
val typ by fields | ||
} | ||
|
||
/** https://www.iana.org/assignments/jwt/jwt.xhtml#claims */ | ||
data class Payload(val claims: JsonNode): JsonNode by claims { | ||
val subject get() = getString("sub") | ||
val audience get() = getString("aud") | ||
val issuedAt get() = getOrNull<Number>("iat")?.let { Instant.ofEpochSecond(it.toLong()) } | ||
val issuer = getOrNull<String>("iss") | ||
val expiresAt get() = getOrNull<Number>("exp")?.let { Instant.ofEpochSecond(it.toLong()) } | ||
val name get() = getOrNull<String>("name") | ||
val email get() = getOrNull<String>("email")?.let { Email(it) } | ||
val emailVerified get() = getOrNull<Boolean>("email_verified") | ||
val locale get() = getOrNull<String>("locale")?.let { Locale.forLanguageTag(it) } | ||
} | ||
|
||
data class Signature(val fields: JsonNode): JsonNode by fields | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import ch.tutteli.atrium.api.fluent.en_GB.toEqual | ||
import ch.tutteli.atrium.api.verbs.expect | ||
import klite.base64UrlDecode | ||
import klite.oauth.JWT | ||
import org.junit.jupiter.api.Test | ||
import java.time.Instant | ||
|
||
class JWTTest { | ||
@Test fun parse() { | ||
val jwt = JWT("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c") | ||
expect(jwt.header.alg).toEqual("HS256") | ||
expect(jwt.header.typ).toEqual("JWT") | ||
expect(jwt.payload.subject).toEqual("1234567890") | ||
expect(jwt.payload.name).toEqual("John Doe") | ||
expect(jwt.payload.issuedAt).toEqual(Instant.ofEpochSecond(1516239022)) | ||
expect(jwt.signature).toEqual("SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c".base64UrlDecode().decodeToString()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters