Skip to content
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

Error when decoding a JWT with JWK set #544

Open
nscarlato-ith opened this issue Nov 13, 2023 · 0 comments
Open

Error when decoding a JWT with JWK set #544

nscarlato-ith opened this issue Nov 13, 2023 · 0 comments

Comments

@nscarlato-ith
Copy link

I am trying to decode a JWT with a JWK set and get the error "kid" invalid, unable to lookup correct key from JWT::getKey() for the following code:

$jwt = 'eyJraWQiOiIyMDE5LTA1LTIyVDIwOjQwOjM4Ljc4MS5lYyIsInR5cCI6IkpXVCIsImFsZyI6IkVTMjU2In0.[...]'
$keys = file_get_contents(self::JWK);
$jsonKeys = json_decode($keys, true);

// This filtering was needed otherwise when parsing the keys it would fail. I feel like it should fail and continue trying with other keys of the array...
$jsonKeys = array_filter(
    $jsonKeys['keys'],
    fn ($key) => isset($key['kty'])
        && isset($key['crv'])
        && $key['crv'] === self::JWT_ALGORITHM_CURVE
);

$jwtSet = JWK::parseKeySet(['keys' => $jsonKeys], 'ES256');
$decodedToken = (array) JWT::decode($jwt, $jwkSet);

I checked the JWT on JWT.io and I can see the 'kid' header.

I tried decoding with only one key changing the code to $decodedToken = (array) JWT::decode($jwt, array_shift($jwkSet)); and it worked fine. Same as when parsing keys, I believe it should be able to handle several keys (if one fails, try the others).

I don't think I am doing something wrong, but in case someone sees something odd please correct me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant