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

Fix #46 #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion src/Token/AppleAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function __construct(array $keys, array $options = [])
try {
$decoded = JWT::decode($options['id_token'], $key);
} catch (\UnexpectedValueException $e) {
$decoded = JWT::decode($options['id_token'], $key, ['RS256']);
Copy link
Author

@SystematicCZ SystematicCZ Sep 11, 2023

Choose a reason for hiding this comment

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

The 3rd parameter should not be there cause method had previously only two parameters

$headers = (object) ["alg" => 'RS256'];
$decoded = JWT::decode($options['id_token'], $key, $headers);
Comment on lines +50 to +51
Copy link

Choose a reason for hiding this comment

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

Third parameter of the Firebase\JWT\JWT::decode() method has changed over time:

  • version 5.2.0 - 5.5.1 had array $allowed_algs = array() (as third) parameter
  • version 6.0.0 removed array $allowed_algs = array() parameter (no third parameters)
  • version 6.6.0 introduced stdClass &$headers = null (as third) parameter

Proposed changes are valid for current version of firebase/php-jwt, but fails on tests using version 5.2.0 - 5.5.1. For backwards compatibility, we have to check, whether the third parameter exists for the decode() method, and which type is required. Unfortunately, using ReflectionParameter::getType() is not possible, because it was introduced in PHP 7 :(

Suggested change
$headers = (object) ["alg" => 'RS256'];
$decoded = JWT::decode($options['id_token'], $key, $headers);
$decodeMethodReflection = new \ReflectionMethod(JWT::class, 'decode');
$decodeMethodParameters = $decodeMethodReflection->getParameters();
// Backwards compatibility for firebase/php-jwt >=5.2.0 <=5.5.1 supported by PHP 5.6
if (array_key_exists(2, $decodeMethodParameters) && 'allowed_algs' === $decodeMethodParameters[2]->getName()) {
$decoded = JWT::decode($options['id_token'], $key, ['RS256']);
} else {
$headers = (object) ['alg' => 'RS256'];
$decoded = JWT::decode($options['id_token'], $key, $headers);
}

Couldn't think of any other way to ensure backwards compatibility with PHP 5.6, since this library test suite includes PHP 5.6. Tested with:

docker run -v .:/code phpdockerio/php56-fpm bash -c "cd code && composer update --prefer-lowest && vendor/bin/phpunit"

docker run -v .:/code phpdockerio/php80-fpm bash -c "cd code && composer update && vendor/bin/phpunit"

Copy link
Contributor

Choose a reason for hiding this comment

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

I just ran into the same issue. This solution should pass the tests
@Kreyu can you open a new PR with this change?
Hopefully it will be merged (if tests pass there are no reasons not to)

Copy link
Owner

Choose a reason for hiding this comment

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

If the tests are passing there is nothing against to merge it. Thats right.
And if I would find the time I can also implement it by myself but no time yet 🙁

Copy link
Contributor

Choose a reason for hiding this comment

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

If the tests are passing there is nothing against to merge it. Thats right. And if I would find the time I can also implement it by myself but no time yet 🙁

@patrickbussmann I tried in #54 - locally unit tests seem to work

Copy link
Owner

Choose a reason for hiding this comment

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

You can see the tests here because locally your version is different. The tests here check multiple PHP versions 👍

still failing for lowest setup. Maybe we can adjust something to passing here

Copy link
Contributor

Choose a reason for hiding this comment

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

@patrickbussmann i see only a coding standard failure in https://github.com/patrickbussmann/oauth2-apple/actions/runs/8357382263 - should be fixed now in #54

}
break;
} catch (\Exception $exception) {
Expand Down
Loading