From 08328e219e61c34bc9504c8011a0c1f7f7f8435f Mon Sep 17 00:00:00 2001 From: Walter Ching Date: Fri, 7 Feb 2020 13:45:59 +0100 Subject: [PATCH] Fix bug on grantNewAccessToken method A dependency on the reference of token existed and when the token was made immutable the tests failed. The solution was to separate everything from the onSuccess path and add a map that updates the value if needed. --- .../heimdall2/OAuth2AccessTokenManager.kt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/library/src/main/java/de/rheinfabrik/heimdall2/OAuth2AccessTokenManager.kt b/library/src/main/java/de/rheinfabrik/heimdall2/OAuth2AccessTokenManager.kt index 9d93f19..d9e4c90 100644 --- a/library/src/main/java/de/rheinfabrik/heimdall2/OAuth2AccessTokenManager.kt +++ b/library/src/main/java/de/rheinfabrik/heimdall2/OAuth2AccessTokenManager.kt @@ -29,16 +29,23 @@ open class OAuth2AccessTokenManager( calendar: Calendar = Calendar.getInstance() ): Single = grant.grantNewAccessToken() - .doOnSuccess { token -> - token.expiresIn?.let { + .map { + val token = if (it.expiresIn != null) { val newExpirationDate = (calendar.clone() as Calendar).apply { - add(Calendar.SECOND, it) + add(Calendar.SECOND, it.expiresIn) } - mStorage.storeAccessToken( - token = token.copy(expirationDate = newExpirationDate) - ) + it.copy(expirationDate = newExpirationDate) + } else { + it } - }.cache() + token + } + .doOnSuccess { token -> + mStorage.storeAccessToken( + token = token + ) + } + .cache() /** * Returns an Observable emitting an unexpired access token.