Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changes/nextrelease/bugfix-tokenprovider-cache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"type": "bugfix",
"category": "Token",
"description": "Fixes bug in `TokenProvider::cache()` where tokens are incorrectly written to the cache."
}
]
3 changes: 1 addition & 2 deletions src/Token/TokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public static function cache(
) {
$cache->set(
$cacheKey,
$token,
['token' => $token],
null === $token->getExpiration() ?
0 : $token->getExpiration() - time()
);
Expand Down Expand Up @@ -269,4 +269,3 @@ public static function sso(
return new SsoTokenProvider($profileName, $filename, $ssoClient);
}
}

35 changes: 33 additions & 2 deletions tests/Token/TokenProviderTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?php
namespace Aws\Test\Token;

use Aws\Exception\TokenException;
use Aws\LruArrayCache;
use Aws\Result;
use Aws\SSOOIDC\SSOOIDCClient;
use Aws\Test\UsesServiceTrait;
use Aws\Token\SsoToken;
use Aws\Token\SsoTokenProvider;
use Aws\Token\Token;
use Aws\Token\TokenInterface;
use Aws\Token\TokenProvider;
use GuzzleHttp\Promise;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;

require_once __DIR__ . '/../Token/token_hack.php';
Expand Down Expand Up @@ -422,4 +422,35 @@ public function testTokenProviderFailureCases($cachedToken, $expectedException)
$this->assertSame($token->getToken(), $found->getToken());
$this->assertEquals($token->getExpiration(), $found->getExpiration());
}

public function testCacheWritesAndReadsCorrectFormat()
{
$cache = new LruArrayCache;
$key = 'test_write_read';
$token = new Token('test-token', strtotime('+1 hour'));
$providerCallCount = 0;

$provider = function() use ($token, &$providerCallCount) {
$providerCallCount++;
return Promise\Create::promiseFor($token);
};

$cachedProvider = TokenProvider::cache($provider, $cache, $key);

// First call should invoke provider and write to cache
$result1 = $cachedProvider()->wait();
$this->assertEquals(1, $providerCallCount);
$this->assertEquals('test-token', $result1->getToken());

// Verify cache structure
$cachedValue = $cache->get($key);
$this->assertIsArray($cachedValue, 'Cache should store an array');
$this->assertArrayHasKey('token', $cachedValue, 'Cached array should have token key');
$this->assertInstanceOf(TokenInterface::class, $cachedValue['token']);

// Second call should use cache without invoking provider
$result2 = $cachedProvider()->wait();
$this->assertEquals(1, $providerCallCount, 'Provider should not be called again');
$this->assertEquals('test-token', $result2->getToken());
}
}