Skip to content

Commit 73eac83

Browse files
committed
Fixed OIDC JWT key parsing in microsoft environments
Made existence of 'alg' optional when JWK array set so we instead infer it as RSA256 if not existing. Fixes #3206
1 parent c11f795 commit 73eac83

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

app/Auth/Access/Oidc/OidcJwtSigningKey.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ protected function loadFromPath(string $path)
6060
*/
6161
protected function loadFromJwkArray(array $jwk)
6262
{
63-
if ($jwk['alg'] !== 'RS256') {
64-
throw new OidcInvalidKeyException("Only RS256 keys are currently supported. Found key using {$jwk['alg']}");
63+
// 'alg' is optional for a JWK, but we will still attempt to validate if
64+
// it exists otherwise presume it will be compatible.
65+
$alg = $jwk['alg'] ?? null;
66+
if ($jwk['kty'] !== 'RSA' || !(is_null($alg) || $alg === 'RS256')) {
67+
throw new OidcInvalidKeyException("Only RS256 keys are currently supported. Found key using {$alg}");
6568
}
6669

6770
if (empty($jwk['use'])) {

app/Auth/Access/Oidc/OidcProviderSettings.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ protected function loadSettingsFromIssuerDiscovery(ClientInterface $httpClient):
164164
protected function filterKeys(array $keys): array
165165
{
166166
return array_filter($keys, function (array $key) {
167-
return $key['kty'] === 'RSA' && $key['use'] === 'sig' && $key['alg'] === 'RS256';
167+
$alg = $key['alg'] ?? null;
168+
return $key['kty'] === 'RSA' && $key['use'] === 'sig' && (is_null($alg) || $alg === 'RS256');
168169
});
169170
}
170171

tests/Auth/OidcTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,31 @@ public function test_autodiscovery_calls_are_cached()
318318
$this->assertCount(4, $transactions);
319319
}
320320

321+
public function test_auth_login_with_autodiscovery_with_keys_that_do_not_have_alg_property()
322+
{
323+
$this->withAutodiscovery();
324+
325+
$keyArray = OidcJwtHelper::publicJwkKeyArray();
326+
unset($keyArray['alg']);
327+
328+
$this->mockHttpClient([
329+
$this->getAutoDiscoveryResponse(),
330+
new Response(200, [
331+
'Content-Type' => 'application/json',
332+
'Cache-Control' => 'no-cache, no-store',
333+
'Pragma' => 'no-cache',
334+
], json_encode([
335+
'keys' => [
336+
$keyArray,
337+
],
338+
])),
339+
]);
340+
341+
$this->assertFalse(auth()->check());
342+
$this->runLogin();
343+
$this->assertTrue(auth()->check());
344+
}
345+
321346
protected function withAutodiscovery()
322347
{
323348
config()->set([

0 commit comments

Comments
 (0)