Skip to content

Commit b42a167

Browse files
bugfix: TokenProvider cache writes (#3176)
Co-authored-by: hillcrest-reedley <[email protected]>
1 parent d4c6107 commit b42a167

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{
3+
"type": "bugfix",
4+
"category": "Token",
5+
"description": "Fixes bug in `TokenProvider::cache()` where tokens are incorrectly written to the cache."
6+
}
7+
]

src/Token/TokenProvider.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public static function cache(
212212
) {
213213
$cache->set(
214214
$cacheKey,
215-
$token,
215+
['token' => $token],
216216
null === $token->getExpiration() ?
217217
0 : $token->getExpiration() - time()
218218
);
@@ -269,4 +269,3 @@ public static function sso(
269269
return new SsoTokenProvider($profileName, $filename, $ssoClient);
270270
}
271271
}
272-

tests/Token/TokenProviderTest.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22
namespace Aws\Test\Token;
33

4-
use Aws\Exception\TokenException;
54
use Aws\LruArrayCache;
65
use Aws\Result;
7-
use Aws\SSOOIDC\SSOOIDCClient;
86
use Aws\Test\UsesServiceTrait;
97
use Aws\Token\SsoToken;
108
use Aws\Token\SsoTokenProvider;
119
use Aws\Token\Token;
10+
use Aws\Token\TokenInterface;
1211
use Aws\Token\TokenProvider;
12+
use GuzzleHttp\Promise;
1313
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
1414

1515
require_once __DIR__ . '/../Token/token_hack.php';
@@ -422,4 +422,35 @@ public function testTokenProviderFailureCases($cachedToken, $expectedException)
422422
$this->assertSame($token->getToken(), $found->getToken());
423423
$this->assertEquals($token->getExpiration(), $found->getExpiration());
424424
}
425+
426+
public function testCacheWritesAndReadsCorrectFormat()
427+
{
428+
$cache = new LruArrayCache;
429+
$key = 'test_write_read';
430+
$token = new Token('test-token', strtotime('+1 hour'));
431+
$providerCallCount = 0;
432+
433+
$provider = function() use ($token, &$providerCallCount) {
434+
$providerCallCount++;
435+
return Promise\Create::promiseFor($token);
436+
};
437+
438+
$cachedProvider = TokenProvider::cache($provider, $cache, $key);
439+
440+
// First call should invoke provider and write to cache
441+
$result1 = $cachedProvider()->wait();
442+
$this->assertEquals(1, $providerCallCount);
443+
$this->assertEquals('test-token', $result1->getToken());
444+
445+
// Verify cache structure
446+
$cachedValue = $cache->get($key);
447+
$this->assertIsArray($cachedValue, 'Cache should store an array');
448+
$this->assertArrayHasKey('token', $cachedValue, 'Cached array should have token key');
449+
$this->assertInstanceOf(TokenInterface::class, $cachedValue['token']);
450+
451+
// Second call should use cache without invoking provider
452+
$result2 = $cachedProvider()->wait();
453+
$this->assertEquals(1, $providerCallCount, 'Provider should not be called again');
454+
$this->assertEquals('test-token', $result2->getToken());
455+
}
425456
}

0 commit comments

Comments
 (0)