From 6706f0197cd331c8ae974aa6c9e35eac9bfa4620 Mon Sep 17 00:00:00 2001 From: Aidan Laing Date: Tue, 13 Feb 2018 15:40:37 -0800 Subject: [PATCH] Token Expiry Time and Refresh Time Fix Expires at and refresh expires at are now set outside of the Token object --- .../bc/gov/mobileauthentication/data/models/Token.kt | 12 ++++++------ .../data/repos/token/TokenRemoteDataSource.kt | 10 ++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/mobileauthentication/src/main/java/ca/bc/gov/mobileauthentication/data/models/Token.kt b/mobileauthentication/src/main/java/ca/bc/gov/mobileauthentication/data/models/Token.kt index 5cf6186..2e8ceae 100644 --- a/mobileauthentication/src/main/java/ca/bc/gov/mobileauthentication/data/models/Token.kt +++ b/mobileauthentication/src/main/java/ca/bc/gov/mobileauthentication/data/models/Token.kt @@ -15,18 +15,18 @@ data class Token( @SerializedName("id_token") val idToken: String?, @SerializedName("not-before-policy") val notBeforePolicy: Long?, @SerializedName("session_state") val sessionState: String?, - @SerializedName("expires_at") val expiresAt: Long? = System.currentTimeMillis() + ((expiresIn ?: 0) * 1000), - @SerializedName("refresh_expires_at") val refreshExpiresAt: Long? = System.currentTimeMillis() + ((refreshExpiresIn ?: 0) * 1000) + @SerializedName("expires_at") var expiresAt: Long?, + @SerializedName("refresh_expires_at") var refreshExpiresAt: Long? ) { fun isExpired(currentTime: Long = System.currentTimeMillis()): Boolean { - if (expiresAt == null) return true - return expiresAt > currentTime + val currExpiresAt = expiresAt ?: return true + return currExpiresAt > currentTime } fun isRefreshExpired(currentTime: Long = System.currentTimeMillis()): Boolean { - if (refreshExpiresAt == null) return true - return refreshExpiresAt > currentTime + val currRefreshExpiresAt = refreshExpiresAt ?: return true + return currRefreshExpiresAt > currentTime } } \ No newline at end of file diff --git a/mobileauthentication/src/main/java/ca/bc/gov/mobileauthentication/data/repos/token/TokenRemoteDataSource.kt b/mobileauthentication/src/main/java/ca/bc/gov/mobileauthentication/data/repos/token/TokenRemoteDataSource.kt index 3c62e64..5997285 100644 --- a/mobileauthentication/src/main/java/ca/bc/gov/mobileauthentication/data/repos/token/TokenRemoteDataSource.kt +++ b/mobileauthentication/src/main/java/ca/bc/gov/mobileauthentication/data/repos/token/TokenRemoteDataSource.kt @@ -43,6 +43,11 @@ private constructor( override fun getToken(code: String?): Observable { if (code == null) return Observable.error(NoCodeException()) return authApi.getToken(realmName, grantType, redirectUri, clientId, code) + .map { token -> + token.expiresAt = System.currentTimeMillis() + ((token.expiresIn ?: 0) * 1000) + token.refreshExpiresAt = System.currentTimeMillis() + ((token.refreshExpiresIn ?: 0) * 1000) + token + } } /** @@ -59,6 +64,11 @@ private constructor( override fun refreshToken(token: Token): Observable { val refreshToken = token.refreshToken ?: return Observable.error(NoRefreshTokenException()) return authApi.refreshToken(realmName, redirectUri, clientId, refreshToken) + .map { refreshedToken -> + refreshedToken.expiresAt = System.currentTimeMillis() + ((refreshedToken.expiresIn ?: 0) * 1000) + refreshedToken.refreshExpiresAt = System.currentTimeMillis() + ((refreshedToken.refreshExpiresIn ?: 0) * 1000) + refreshedToken + } } /** * Invalid operation for remote data source