Skip to content
Open
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
15 changes: 11 additions & 4 deletions src/Traits/AutoDiscovery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The cache key generation is sensitive to the order of keys in the $query_params array. 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., using ksort()) before encoding them into the cache key.

/** @noinspection LaravelFunctionsInspection */
$cacheTtl = env('OIDC_DISCOVERY_CACHE_TTL', 3600);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Avoid using the env() helper directly in application logic. In Laravel, env() should ideally only be used within configuration files. When the application configuration is cached (via php artisan config:cache), all env() calls outside of configuration files return null. This causes the code to ignore the actual environment variable and fall back to the default value of 3600. It is recommended to define a configuration key (e.g., in config/openid.php) and use the config() helper instead.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The 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');
Expand Down