Skip to content

Commit 44b636c

Browse files
committed
Cache certificate as per cache control headers
1 parent c6d26c0 commit 44b636c

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/OpenIdVerificator.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class OpenIdVerificator
1717

1818
private $guzzle;
1919
private $rsa;
20+
private $maxAge = [];
2021

2122
public function __construct(Client $guzzle, RSA $rsa)
2223
{
@@ -26,9 +27,12 @@ public function __construct(Client $guzzle, RSA $rsa)
2627

2728
public function getPublicKey($kid = null)
2829
{
29-
$v3Certs = Cache::rememberForever(self::V3_CERTS, function () {
30-
return $this->getv3Certs();
31-
});
30+
if (Cache::has(self::V3_CERTS)) {
31+
$v3Certs = Cache::get(self::V3_CERTS);
32+
} else {
33+
$v3Certs = $this->getv3Certs();
34+
Cache::put(self::V3_CERTS, $v3Certs, $this->maxAge[self::URL_OPENID_CONFIG]);
35+
}
3236

3337
$cert = $kid ? collect($v3Certs)->firstWhere('kid', '=', $kid) : $v3Certs[0];
3438

@@ -63,6 +67,14 @@ private function callApiAndReturnValue($url, $value)
6367

6468
$data = json_decode($response->getBody(), true);
6569

70+
$maxAge = 0;
71+
foreach ($response->getHeader('Cache-Control') as $line) {
72+
preg_match('/max-age=(\d+)/', $line, $matches);
73+
$maxAge = isset($matches[1]) ? (int) $matches[1] : 0;
74+
}
75+
76+
$this->maxAge[$url] = $maxAge;
77+
6678
return Arr::get($data, $value);
6779
}
6880

0 commit comments

Comments
 (0)