Skip to content

Commit 6160252

Browse files
committed
Add SymfonyCacheCredential
1 parent 58d63d0 commit 6160252

File tree

6 files changed

+129
-10
lines changed

6 files changed

+129
-10
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"symfony/deprecation-contracts": "^2.1 || ^3.0",
1313
"symfony/http-client": "^4.4.16 || ^5.1.7 || ^6.0 || ^7.0",
1414
"symfony/http-client-contracts": "^1.1.8 || ^2.0 || ^3.0",
15-
"symfony/service-contracts": "^1.0 || ^2.0 || ^3.0"
15+
"symfony/service-contracts": "^1.0 || ^2.0 || ^3.0",
16+
"symfony/cache-contracts": "^1.0 || ^2.0 || ^3.0"
1617
},
1718
"require-dev": {
1819
},

composer.lock

Lines changed: 78 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Client/AadClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ protected function fetchToken(array $scopes, array $params): ?TokenInterface
5959
]);
6060
$result = $response->toArray();
6161
} catch (DecodingExceptionInterface $e) {
62-
$this->logger->info('Failed to decode Credentials.', ['exception' => $e]);
62+
$this->logger->info('Failed to decode token response.', ['exception' => $e]);
6363

6464
return null;
6565
} catch (TransportExceptionInterface|HttpExceptionInterface $e) {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Azure\Identity\Credential;
4+
5+
use Azure\Identity\TokenInterface;
6+
use Psr\Cache\CacheException;
7+
use Psr\Log\LoggerInterface;
8+
use Symfony\Contracts\Cache\CacheInterface;
9+
use Symfony\Contracts\Cache\ItemInterface;
10+
11+
class SymfonyCacheCredential implements TokenCredentialInterface
12+
{
13+
public function __construct(private TokenCredentialInterface $decorated, private CacheInterface $cache, private ?LoggerInterface $logger = null)
14+
{
15+
}
16+
17+
public function getToken(array $scopes, array $options = []): ?TokenInterface
18+
{
19+
$credential = $this->decorated;
20+
$closure = \Closure::fromCallable(static function (ItemInterface $item) use ($scopes, $options, $credential) {
21+
$token = $credential->getToken($scopes, $options);
22+
23+
if (null !== $token && null !== $exp = $token->getExpireDate()) {
24+
$item->expiresAt($exp);
25+
} else {
26+
$item->expiresAfter(0);
27+
}
28+
29+
return $token;
30+
});
31+
32+
try {
33+
return $this->cache->get('Azure.Credentials.' . sha1(serialize([$scopes, $options, \get_class($this->decorated)])), $closure);
34+
} catch (CacheException $e) {
35+
$this->logger?->error('Failed to get Azure credentials from cache.', ['exception' => $e]);
36+
37+
return $credential->getToken($scopes, $options);
38+
}
39+
}
40+
}

src/Token.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@ class Token implements TokenInterface
88

99
private $expireDate;
1010

11-
/**
12-
* @throws \Exception
13-
*/
14-
public function __construct(public string $token, public int $expiresIn)
11+
public function __construct(public string $accessToken, public int $expiresIn)
1512
{
1613
$this->expireDate = (new \DateTimeImmutable())
1714
->add(new \DateInterval(sprintf('PT%dS', $expiresIn)))
@@ -24,8 +21,13 @@ public function isExpired(): bool
2421
}
2522

2623
/**
27-
* @throws \Exception
24+
* @return \DateTimeImmutable
2825
*/
26+
public function getExpireDate(): \DateTimeImmutable
27+
{
28+
return $this->expireDate;
29+
}
30+
2931
public static function fromArray(array $token): TokenInterface
3032
{
3133
return new self($token['access_token'], $token['expires_in']);

src/TokenInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
namespace Azure\Identity;
44

55
/**
6-
* @property-read string $token
76
* @property-read string $accessToken
87
*/
98
interface TokenInterface
109
{
1110
public function isExpired(): bool;
11+
public function getExpireDate(): \DateTimeImmutable;
1212
}

0 commit comments

Comments
 (0)