From da4037df770027c8f7163595a29ec434f705f8b1 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Thu, 1 Sep 2022 11:07:07 -0600 Subject: [PATCH] fix: add eager refresh (#411) * fix: add eager refresh * add test --- src/FetchAuthTokenCache.php | 7 ++++- tests/FetchAuthTokenCacheTest.php | 46 ++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/src/FetchAuthTokenCache.php b/src/FetchAuthTokenCache.php index 0f870977f..28caea3b9 100644 --- a/src/FetchAuthTokenCache.php +++ b/src/FetchAuthTokenCache.php @@ -37,6 +37,11 @@ class FetchAuthTokenCache implements */ private $fetcher; + /** + * @var int + */ + private $eagerRefreshThresholdSeconds = 10; + /** * @param FetchAuthTokenInterface $fetcher A credentials fetcher * @param array $cacheConfig Configuration for the cache @@ -250,7 +255,7 @@ private function fetchAuthTokenFromCache($authUri = null) // (for JwtAccess and ID tokens) return $cached; } - if (time() < $cached['expires_at']) { + if ((time() + $this->eagerRefreshThresholdSeconds) < $cached['expires_at']) { // access token is not expired return $cached; } diff --git a/tests/FetchAuthTokenCacheTest.php b/tests/FetchAuthTokenCacheTest.php index 088540d34..e88470eed 100644 --- a/tests/FetchAuthTokenCacheTest.php +++ b/tests/FetchAuthTokenCacheTest.php @@ -251,7 +251,7 @@ public function testShouldReturnValueWhenNotExpired() { $cacheKey = 'myKey'; $token = '2/abcdef1234567890'; - $expiresAt = time() + 10; + $expiresAt = time() + 20; $cachedValue = [ 'access_token' => $token, 'expires_at' => $expiresAt, @@ -328,6 +328,50 @@ public function testShouldNotReturnValueWhenExpired() $this->assertEquals($newToken, $accessToken); } + public function testShouldNotReturnValueWhenExpiredWithinEagerThreshold() + { + $cacheKey = 'myKey'; + $token = '2/abcdef1234567890'; + $expiresAt = time() + 5; + $cachedValue = [ + 'access_token' => $token, + 'expires_at' => $expiresAt, + ]; + $newToken = ['access_token' => '3/abcdef1234567890']; + $this->mockCacheItem->isHit() + ->shouldBeCalledTimes(1) + ->willReturn(true); + $this->mockCacheItem->get() + ->shouldBeCalledTimes(1) + ->willReturn($cachedValue); + $this->mockCacheItem->set($newToken) + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); + $this->mockCacheItem->expiresAfter(1500) + ->shouldBeCalledTimes(1) + ->willReturn($this->mockCacheItem->reveal()); + $this->mockCache->getItem($cacheKey) + ->shouldBeCalledTimes(2) + ->willReturn($this->mockCacheItem->reveal()); + $this->mockFetcher->fetchAuthToken(null) + ->shouldBeCalledTimes(1) + ->willReturn($newToken); + $this->mockFetcher->getCacheKey() + ->shouldBeCalled() + ->willReturn($cacheKey); + $this->mockCache->save($this->mockCacheItem) + ->shouldBeCalledTimes(1); + + // Run the test. + $cachedFetcher = new FetchAuthTokenCache( + $this->mockFetcher->reveal(), + null, + $this->mockCache->reveal() + ); + $accessToken = $cachedFetcher->fetchAuthToken(); + $this->assertEquals($newToken, $accessToken); + } + public function testGetsCachedAuthTokenUsingCachePrefix() { $prefix = 'test_prefix_';