-
Notifications
You must be signed in to change notification settings - Fork 37
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 #2865 from wordpress-mobile/woo/improve-jwt-caching
Jetpack AI: improve caching logic of the JWT token
- Loading branch information
Showing
4 changed files
with
120 additions
and
52 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
example/src/androidTest/java/org/wordpress/android/fluxc/JWTTokenTests.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,47 @@ | ||
package org.wordpress.android.fluxc | ||
|
||
import android.util.Base64 | ||
import org.junit.Assert | ||
import org.junit.Test | ||
import org.wordpress.android.fluxc.model.JWTToken | ||
|
||
class JWTTokenTests { | ||
@Test | ||
fun given_a_valid_token__when_validateExpiryDate_is_called__then_return_it() { | ||
val token = generateToken(expired = false) | ||
|
||
val result = token.validateExpiryDate() | ||
|
||
Assert.assertNotNull(result) | ||
} | ||
|
||
@Test | ||
fun given_an_expired_token__when_validateExpiryDate_is_called__then_return_null() { | ||
val token = generateToken(expired = true) | ||
|
||
val result = token.validateExpiryDate() | ||
|
||
Assert.assertNull(result) | ||
} | ||
|
||
private fun generateToken(expired: Boolean): JWTToken { | ||
val expirationTime = System.currentTimeMillis() / 1000 + if (expired) -100 else 100 | ||
|
||
// Sample token from https://jwt.io/ modifier with an expiration time | ||
val header = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" | ||
val payload = Base64.encode( | ||
""" | ||
{ | ||
"sub": "1234567890", | ||
"name": "John Doe", | ||
"iat": 1516239022, | ||
"exp": $expirationTime, | ||
"expires": $expirationTime | ||
} | ||
""".trimIndent().toByteArray(), Base64.DEFAULT | ||
).decodeToString() | ||
val signature = "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" | ||
|
||
return JWTToken("$header.$payload.$signature") | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
fluxc/src/main/java/org/wordpress/android/fluxc/model/JWTToken.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,35 @@ | ||
package org.wordpress.android.fluxc.model | ||
|
||
import android.util.Base64 | ||
import org.json.JSONObject | ||
|
||
@JvmInline | ||
value class JWTToken( | ||
val value: String | ||
) { | ||
/** | ||
* Returns the token if it is still valid, or null if it is expired. | ||
*/ | ||
@Suppress("MagicNumber") | ||
fun validateExpiryDate(): JWTToken? { | ||
fun JSONObject.getLongOrNull(name: String) = this.optLong(name, Long.MAX_VALUE).takeIf { it != Long.MAX_VALUE } | ||
|
||
val payloadJson = getPayloadJson() | ||
val expiration = payloadJson.getLongOrNull("exp") | ||
?: payloadJson.getLongOrNull("expires") | ||
?: return null | ||
|
||
val now = System.currentTimeMillis() / 1000 | ||
|
||
return if (expiration > now) this else null | ||
} | ||
|
||
fun getPayloadItem(key: String): String? { | ||
return getPayloadJson().optString(key) | ||
} | ||
|
||
private fun getPayloadJson(): JSONObject { | ||
val payloadEncoded = this.value.split(".")[1] | ||
return JSONObject(String(Base64.decode(payloadEncoded, Base64.DEFAULT))) | ||
} | ||
} |
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