Skip to content

Commit

Permalink
Token Expiry Time and Refresh Time Fix
Browse files Browse the repository at this point in the history
Expires at and refresh expires at are now set outside of the Token object
  • Loading branch information
Aidan Laing committed Feb 13, 2018
1 parent 513f1e7 commit 6706f01
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ private constructor(
override fun getToken(code: String?): Observable<Token> {
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
}
}

/**
Expand All @@ -59,6 +64,11 @@ private constructor(
override fun refreshToken(token: Token): Observable<Token> {
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
Expand Down

0 comments on commit 6706f01

Please sign in to comment.