Skip to content

Commit 811af25

Browse files
authored
Add cache keying by metadata configuration (#1046)
1 parent 8d3791f commit 811af25

1 file changed

Lines changed: 14 additions & 12 deletions

File tree

Provider.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,6 @@ class Provider extends AbstractProvider implements SocialiteProvider
9696
*/
9797
protected $config;
9898

99-
public const CACHE_NAMESPACE = 'socialite_saml2';
100-
public const METADATA_CACHE_KEY = self::CACHE_NAMESPACE.'_metadata';
101-
public const METADATA_CACHE_KEY_TTL = self::METADATA_CACHE_KEY.'_ttl';
102-
public const ID_CACHE_PREFIX = self::CACHE_NAMESPACE.'_id_';
103-
10499
public const ATTRIBUTE_MAP = [
105100
'email' => [
106101
ClaimTypes::EMAIL_ADDRESS,
@@ -300,21 +295,21 @@ protected function getIdentityProviderEntityDescriptorFromXml(): EntityDescripto
300295
protected function getIdentityProviderEntityDescriptorFromUrl(): EntityDescriptor
301296
{
302297
$metadataUrl = $this->getConfig('metadata');
303-
$xml = Cache::get(self::METADATA_CACHE_KEY);
304-
$ttl = Cache::get(self::METADATA_CACHE_KEY_TTL);
298+
$xml = Cache::get($this->cacheKey('metadata'));
299+
$ttl = Cache::get($this->cacheKey('metadata_ttl'));
305300

306301
if ($xml && $ttl && $ttl + $this->getConfig('ttl', 86400) > time()) {
307302
return $this->getIdpEntityDescriptorFromXml($xml);
308303
}
309304

310-
Cache::forever(self::METADATA_CACHE_KEY_TTL, time());
305+
Cache::forever($this->cacheKey('metadata_ttl'), time());
311306

312307
try {
313308
$xml = (string) $this->getHttpClient()
314309
->get($metadataUrl)
315310
->getBody();
316311

317-
Cache::forever(self::METADATA_CACHE_KEY, $xml);
312+
Cache::forever($this->cacheKey('metadata'), $xml);
318313
} catch (GuzzleException $e) {
319314
if (!$xml) {
320315
throw $e;
@@ -494,7 +489,7 @@ protected function validateRecipient(): void
494489
protected function validateRepeatedId(): void
495490
{
496491
$assertion = $this->getFirstAssertion();
497-
$key = collect([self::ID_CACHE_PREFIX, $assertion->getIssuer()->getValue(), $assertion->getId()])->join('-');
492+
$key = $this->cacheKey(collect(['id', $assertion->getIssuer()->getValue(), $assertion->getId()])->join('_'));
498493

499494
if (Cache::has($key)) {
500495
throw new LightSamlValidationException('The identity provider repeated an assertion id');
@@ -652,8 +647,8 @@ public function getServiceProviderMetadata(): Response
652647

653648
public function clearIdentityProviderMetadataCache()
654649
{
655-
Cache::forget(self::METADATA_CACHE_KEY);
656-
Cache::forget(self::METADATA_CACHE_KEY_TTL);
650+
Cache::forget($this->cacheKey('metadata'));
651+
Cache::forget($this->cacheKey('metadata_ttl'));
657652
}
658653

659654
protected function signature(X509Credential $credential): SignatureWriter
@@ -736,4 +731,11 @@ protected function mapUserToObject(array $user)
736731
{
737732
throw new NotSupportedException();
738733
}
734+
735+
protected function cacheKey(string $key): string
736+
{
737+
$hash = md5($this->getConfig('acs') ?: $this->getConfig('metadata'));
738+
739+
return sprintf('socialite_saml2_%s_%s', $hash, $key);
740+
}
739741
}

0 commit comments

Comments
 (0)