Skip to content

Commit ce9c8f1

Browse files
fix: improve OidcDiscoveryTokenHandler with claims and headers check (#490)
1 parent 3ca671d commit ce9c8f1

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

api/config/packages/jose.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ jose:
1010
signature_algorithms: ['HS256', 'RS256', 'ES256']
1111
header_checkers: ['alg', 'iat', 'nbf', 'exp', 'aud', 'iss']
1212
is_public: true
13+
checkers:
14+
claims:
15+
oidc:
16+
is_public: true
17+
claims: ['iat', 'nbf', 'exp']
18+
headers:
19+
oidc:
20+
is_public: true
21+
headers: ['alg', 'iss', 'aud']
1322

1423
services:
1524
_defaults:

api/src/Security/Http/AccessToken/Oidc/OidcDiscoveryTokenHandler.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace App\Security\Http\AccessToken\Oidc;
66

7+
use Jose\Component\Checker\ClaimCheckerManager;
8+
use Jose\Component\Checker\HeaderCheckerManager;
79
use Jose\Component\Core\JWKSet;
810
use Jose\Component\Signature\JWSLoader;
911
use Psr\Log\LoggerInterface;
1012
use Symfony\Component\DependencyInjection\Attribute\Autowire;
1113
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1214
use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface;
13-
use Symfony\Component\Security\Http\AccessToken\Oidc\Exception\MissingClaimException;
1415
use Symfony\Component\Security\Http\AccessToken\Oidc\OidcTokenHandler;
1516
use Symfony\Component\Security\Http\AccessToken\Oidc\OidcTrait;
1617
use Symfony\Component\Security\Http\Authenticator\FallbackUserLoader;
@@ -21,8 +22,11 @@
2122

2223
/**
2324
* Completes {@see OidcTokenHandler} with OIDC Discovery and configuration stored in cache.
25+
* It verifies access tokens, extracts user information, and creates user badges for Symfony's security system.
26+
*
27+
* @see https://github.com/symfony/symfony/pull/54932
2428
*/
25-
final class OidcDiscoveryTokenHandler implements AccessTokenHandlerInterface
29+
final readonly class OidcDiscoveryTokenHandler implements AccessTokenHandlerInterface
2630
{
2731
use OidcTrait;
2832

@@ -31,7 +35,11 @@ public function __construct(
3135
private CacheInterface $cache,
3236
#[Autowire('@jose.jws_loader.oidc')]
3337
private JWSLoader $jwsLoader,
34-
private readonly HttpClientInterface $securityAuthorizationClient,
38+
#[Autowire('@jose.claim_checker.oidc')]
39+
private ClaimCheckerManager $claimCheckerManager,
40+
#[Autowire('@jose.header_checker.oidc')]
41+
private HeaderCheckerManager $headerCheckerManager,
42+
private HttpClientInterface $securityAuthorizationClient,
3543
private string $claim = 'email',
3644
private int $ttl = 600,
3745
private ?LoggerInterface $logger = null,
@@ -86,9 +94,8 @@ public function getUserBadgeFrom(string $accessToken): UserBadge
8694
);
8795

8896
$claims = json_decode($jws->getPayload(), true);
89-
if (empty($claims[$this->claim])) {
90-
throw new MissingClaimException(\sprintf('"%s" claim not found.', $this->claim));
91-
}
97+
$this->claimCheckerManager->check(claims: $claims, mandatoryClaims: [$this->claim]);
98+
$this->headerCheckerManager->check(jwt: $jws, index: 0);
9299

93100
// UserLoader argument can be overridden by a UserProvider on AccessTokenAuthenticator::authenticate
94101
return new UserBadge($claims[$this->claim], new FallbackUserLoader(fn () => $this->createUser($claims)), $claims);

0 commit comments

Comments
 (0)