-
-
Notifications
You must be signed in to change notification settings - Fork 6
Caching autoDiscovery result #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| namespace Maicol07\OpenIDConnect\Traits; | ||
|
|
||
| use Illuminate\Http\Client\ConnectionException; | ||
| use Illuminate\Support\Facades\Cache; | ||
| use Illuminate\Support\Str; | ||
| use Maicol07\OpenIDConnect\ClientAuthMethod; | ||
| use Maicol07\OpenIDConnect\CodeChallengeMethod; | ||
|
|
@@ -38,12 +39,18 @@ trait AutoDiscovery | |
| public function autoDiscovery(?string $provider_url, array|string|null $query_params = null): void | ||
| { | ||
| if ($provider_url) { | ||
| $response = $this->client() | ||
| ->get("$provider_url/$this->DISCOVERY_PATH", $query_params); | ||
| $cacheKey = 'oidc:discovery:' . md5(json_encode([$provider_url, $query_params])); | ||
| /** @noinspection LaravelFunctionsInspection */ | ||
| $cacheTtl = env('OIDC_DISCOVERY_CACHE_TTL', 3600); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid using the
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is not a Laravel-specific library, but a generic PHP one, it should be better to get the ttl from a config parameter instead of an env |
||
|
|
||
| if ($response->ok()) { | ||
| $config = $response->collect(); | ||
| $config = Cache::remember($cacheKey, $cacheTtl, function () use ($provider_url, $query_params) { | ||
| $response = $this->client() | ||
| ->get("$provider_url/$this->DISCOVERY_PATH", $query_params); | ||
|
|
||
| return $response->ok() ? $response->collect() : null; | ||
| }); | ||
|
|
||
| if ($config) { | ||
| // Response types | ||
| $response_types = []; | ||
| $response_types_supported = $config->get('response_types_supported'); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The cache key generation is sensitive to the order of keys in the
$query_paramsarray. If the same parameters are passed in a different order, it will result in different MD5 hashes and unnecessary cache misses. To ensure consistent cache hits, consider sorting the query parameters (e.g., usingksort()) before encoding them into the cache key.