-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from Liftric/feature/proper-token-payload-access
feat(jwt): serialize tokens, fixes #13
- Loading branch information
Showing
15 changed files
with
774 additions
and
93 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
11 changes: 0 additions & 11 deletions
11
auth/src/androidMain/kotlin/com/liftric/auth/base/Base64.kt
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
auth/src/androidMain/kotlin/com/liftric/auth/jwt/Base64.kt
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,16 @@ | ||
package com.liftric.auth.jwt | ||
|
||
import android.util.Base64 | ||
import java.io.UnsupportedEncodingException | ||
|
||
internal actual class Base64 { | ||
actual companion object { | ||
actual fun decode(string: String): String? { | ||
return try { | ||
String(Base64.decode(string, Base64.URL_SAFE), Charsets.UTF_8) | ||
} catch (e: Exception) { | ||
null | ||
} | ||
} | ||
} | ||
} |
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
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
87 changes: 87 additions & 0 deletions
87
auth/src/commonMain/kotlin/com/liftric/auth/jwt/AccessToken.kt
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,87 @@ | ||
package com.liftric.auth.jwt | ||
|
||
/** | ||
* Access Token containing claims specified by IETF: | ||
* https://tools.ietf.org/html/rfc7519#section-4 | ||
*/ | ||
interface AccessToken { | ||
/** | ||
* Audience | ||
*/ | ||
val aud: String? | ||
|
||
/** | ||
* Expiration Time | ||
*/ | ||
val exp: Long | ||
|
||
/** | ||
* Issued at | ||
*/ | ||
val iat: Long | ||
|
||
/** | ||
* Issuer | ||
*/ | ||
val iss: String | ||
|
||
/** | ||
* JWT ID | ||
*/ | ||
val jti: String | ||
|
||
/** | ||
* Not Before | ||
*/ | ||
val nbf: Long? | ||
|
||
/** | ||
* Subject | ||
*/ | ||
val sub: String | ||
} | ||
|
||
/** | ||
* Access Token extension for Cognito | ||
*/ | ||
interface AccessTokenExtension { | ||
/** | ||
* Time when the authentication occurred. JSON number that represents the number of seconds from 1970-01-01T0:0:0Z as measured in UTC format | ||
*/ | ||
val authTime: Long | ||
|
||
/** | ||
* Client id | ||
*/ | ||
val clientId: String | ||
|
||
/** | ||
* List of groups the user belongs to | ||
*/ | ||
val cognitoGroups: List<String> | ||
|
||
/** | ||
* Device key | ||
*/ | ||
val deviceKey: String? | ||
|
||
/** | ||
* Event id | ||
*/ | ||
val eventId: String? | ||
|
||
/** | ||
* List of Oauth 2.0 scopes that define what access the token provides | ||
*/ | ||
val scope: String? | ||
|
||
/** | ||
* Intended purpose of this token. Its value is always access | ||
*/ | ||
val tokenUse: String | ||
|
||
/** | ||
* Username | ||
*/ | ||
val username: String | ||
} |
2 changes: 1 addition & 1 deletion
2
...in/kotlin/com/liftric/auth/base/Base64.kt → ...ain/kotlin/com/liftric/auth/jwt/Base64.kt
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,4 +1,4 @@ | ||
package com.liftric.auth.base | ||
package com.liftric.auth.jwt | ||
|
||
internal expect class Base64 { | ||
companion object { | ||
|
43 changes: 43 additions & 0 deletions
43
auth/src/commonMain/kotlin/com/liftric/auth/jwt/CognitoAccessToken.kt
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,43 @@ | ||
package com.liftric.auth.jwt | ||
|
||
import kotlinx.serialization.SerialName | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
|
||
class InvalidCognitoAccessTokenException(message:String): Exception(message) | ||
|
||
@Serializable | ||
data class CognitoAccessTokenClaims( | ||
override val aud: String? = null, | ||
override val exp: Long, | ||
override val iat: Long, | ||
override val iss: String, | ||
override val jti: String, | ||
override val nbf: Long? = null, | ||
override val sub: String, | ||
@SerialName("auth_time") | ||
override val authTime: Long, | ||
@SerialName("client_id") | ||
override val clientId: String, | ||
@SerialName("cognito:groups") | ||
override val cognitoGroups: List<String>, | ||
@SerialName("device_key") | ||
override val deviceKey: String? = null, | ||
@SerialName("event_id") | ||
override val eventId: String? = null, | ||
override val scope: String? = null, | ||
@SerialName("token_use") | ||
override val tokenUse: String, | ||
override val username: String | ||
): AccessToken, AccessTokenExtension | ||
|
||
class CognitoAccessToken(accessTokenString: String): JWT<CognitoAccessTokenClaims>(accessTokenString) { | ||
override val claims: CognitoAccessTokenClaims | ||
get() { | ||
try { | ||
return Json.decodeFromString(CognitoAccessTokenClaims.serializer(), getPayload()) | ||
} catch (e: Exception) { | ||
throw InvalidCognitoAccessTokenException("This is not a valid access token") | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
auth/src/commonMain/kotlin/com/liftric/auth/jwt/CognitoIdToken.kt
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,52 @@ | ||
package com.liftric.auth.jwt | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.json.Json | ||
|
||
class InvalidCognitoIdTokenException(message:String): Exception(message) | ||
|
||
@Serializable(with = CustomAttributesSerializer::class) | ||
data class CognitoIdTokenClaims( | ||
override val sub: String? = null, | ||
override val name: String? = null, | ||
override val givenName: String? = null, | ||
override val familyName: String? = null, | ||
override val middleName: String? = null, | ||
override val nickname: String? = null, | ||
override val preferredUsername: String? = null, | ||
override val profile: String? = null, | ||
override val picture: String? = null, | ||
override val website: String? = null, | ||
override val email: String? = null, | ||
override val emailVerified: Boolean? = null, | ||
override val gender: String? = null, | ||
override val birthdate: String? = null, | ||
override val zoneinfo: String? = null, | ||
override val locale: String? = null, | ||
override val phoneNumber: String? = null, | ||
override val phoneNumberVerified: Boolean? = null, | ||
override val address: Address? = null, | ||
override val updatedAt: Long? = null, | ||
override val aud: String, | ||
override val authTime: Long, | ||
override val cognitoGroups: List<String>, | ||
override val cognitoUsername: String, | ||
override val exp: Long, | ||
override val eventId: String, | ||
override val iss: String, | ||
override val iat: Long, | ||
override val scope: String? = null, | ||
override val tokenUse: String, | ||
override val customAttributes: Map<String, String>? = null | ||
): IdToken, IdTokenExtension | ||
|
||
class CognitoIdToken(idTokenString: String): JWT<CognitoIdTokenClaims>(idTokenString) { | ||
override val claims: CognitoIdTokenClaims | ||
get() { | ||
try { | ||
return Json.decodeFromString(CognitoIdTokenClaims.serializer(), getPayload()) | ||
} catch (e: SerializationException) { | ||
throw InvalidCognitoIdTokenException("This is not a valid cognito id token") | ||
} | ||
} | ||
} |
Oops, something went wrong.