Skip to content

Commit 544385a

Browse files
committed
Fix Microsoft OpenID configuration cache serialization
getOpenIdConfiguration() caches json_decode output as stdClass. On deserialization from file/database cache drivers, PHP returns __PHP_Incomplete_Class, breaking subsequent login attempts. Changes: - Return associative arrays from json_decode instead of stdClass - Update all property access to array syntax - Bump cache keys to openid-v2 and jwks-v2 so stale stdClass entries from prior versions are never read - Update property type from mixed to ?array - Update docblock @return from mixed to array<string, mixed>
1 parent 258fd05 commit 544385a

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

src/Microsoft/Provider.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Provider extends AbstractProvider
2020

2121
private const JWKS_CACHE_TTL_SECONDS = 300;
2222

23-
private mixed $openIdConfiguration = null;
23+
private ?array $openIdConfiguration = null;
2424

2525
private ?array $jwtKeys = null;
2626

@@ -303,8 +303,8 @@ private function getJWTKeysWithCache(bool $forceRefresh): array
303303
return $this->jwtKeys;
304304
}
305305

306-
$jwksUri = $this->getOpenIdConfiguration()->jwks_uri;
307-
$cacheKey = 'socialite:microsoft:jwks:' . sha1((string) $jwksUri);
306+
$jwksUri = $this->getOpenIdConfiguration()['jwks_uri'];
307+
$cacheKey = 'socialite:microsoft:jwks-v2:' . sha1((string) $jwksUri);
308308

309309
$fetch = function () use ($jwksUri, $forceRefresh) {
310310
$options = [
@@ -341,11 +341,11 @@ private function getJWTKeysWithCache(bool $forceRefresh): array
341341
/**
342342
* Get OpenID Configuration.
343343
*
344-
* @return mixed
344+
* @return array<string, mixed>
345345
*
346346
* @throws \Laravel\Socialite\Two\InvalidStateException
347347
*/
348-
private function getOpenIdConfiguration(): mixed
348+
private function getOpenIdConfiguration(): array
349349
{
350350
if ($this->openIdConfiguration !== null) {
351351
return $this->openIdConfiguration;
@@ -358,13 +358,13 @@ private function getOpenIdConfiguration(): mixed
358358
//
359359
$discovery = sprintf('https://login.microsoftonline.com/%s/v2.0/.well-known/openid-configuration', $this->getConfig('tenant', 'common'));
360360

361-
$cacheKey = 'socialite:microsoft:openid:' . sha1((string) $discovery);
361+
$cacheKey = 'socialite:microsoft:openid-v2:' . sha1((string) $discovery);
362362

363363
if (class_exists(\Illuminate\Support\Facades\Cache::class)) {
364364
$this->openIdConfiguration = \Illuminate\Support\Facades\Cache::remember($cacheKey, self::OPENID_CONFIGURATION_CACHE_TTL_SECONDS, function () use ($discovery) {
365365
$response = $this->getHttpClient()->get($discovery, [RequestOptions::PROXY => $this->getConfig('proxy')]);
366366

367-
return json_decode((string) $response->getBody());
367+
return json_decode((string) $response->getBody(), true);
368368
});
369369

370370
return $this->openIdConfiguration;
@@ -375,7 +375,7 @@ private function getOpenIdConfiguration(): mixed
375375
throw new InvalidStateException("Error on getting OpenID Configuration. {$ex}");
376376
}
377377

378-
$this->openIdConfiguration = json_decode((string) $response->getBody());
378+
$this->openIdConfiguration = json_decode((string) $response->getBody(), true);
379379

380380
return $this->openIdConfiguration;
381381
}
@@ -390,7 +390,7 @@ private function getTokenSigningAlgorithm($jwtHeader): string
390390
{
391391
return $jwtHeader?->alg ?? (string) collect(
392392
array_merge(
393-
$this->getOpenIdConfiguration()->id_token_signing_alg_values_supported,
393+
$this->getOpenIdConfiguration()['id_token_signing_alg_values_supported'],
394394
[$this->getConfig('default_algorithm', 'RS256')]
395395
)
396396
)->first();
@@ -437,7 +437,7 @@ private function validate(string $idToken)
437437
// iss validation - a security token service (STS) URI
438438
// Identifies the STS that constructs and returns the token, and the Microsoft Entra tenant of the authenticated user.
439439
// https://learn.microsoft.com/en-au/entra/identity-platform/access-tokens#multitenant-applications
440-
$issuer = str_replace('{tenantid}', $jwtPayload->tid, $this->getOpenIdConfiguration()->issuer);
440+
$issuer = str_replace('{tenantid}', $jwtPayload->tid, $this->getOpenIdConfiguration()['issuer']);
441441
if (strcmp($iss = $jwtPayload->iss, $issuer)) {
442442
throw new InvalidStateException('iss on id_token does not match issuer value on the OpenID configuration');
443443
}

0 commit comments

Comments
 (0)