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 Auth0::exchange() to handle missing id_token #318

Merged
merged 2 commits into from
Feb 4, 2019

Conversation

joshcanhelp
Copy link
Contributor

@joshcanhelp joshcanhelp commented Jan 31, 2019

Changes

Fix Auth0::exchange() to properly handle code exchanges that do not return an id_token.

References

Issue #317

Testing

  • This change adds test coverage
  • This change has been tested up to PHP 7.1.16

@joshcanhelp joshcanhelp force-pushed the fix-exchange-method-id-token-missing branch from 4c98328 to 3ef19d3 Compare January 31, 2019 23:40
*
* @var array
*/
protected $idTokenDecoded;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Store a decoded version of the ID token (remains null if none was returned/stored).

@@ -283,6 +301,11 @@ public function __construct(array $config)
$this->guzzleOptions = $config['guzzle_options'];
}

$this->skipUserinfo = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Default to false so existing behavior is retained.

@@ -492,8 +515,10 @@ public function getRefreshToken()
/**
* Exchange authorization code for access, ID, and refresh tokens
*
* @throws CoreException - if an active session already or state cannot be validated.
* @throws ApiException - if access token is invalid.
* @throws CoreException If the state value is missing or invalid.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method does not throw any new Exceptions, just adding docs for missing ones.

$idToken = false;
if (isset($response['id_token'])) {
$idToken = $response['id_token'];
if (! empty($response['id_token'])) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Main bug fix is here ... only call setIdToken() (which validates) if there is one returned.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see you're using 3 different ways of checking if the value is set for the given properties before calling the associated "setProperty" method.

  • if (isset($response['refresh_token']))
  • if (! empty($response['id_token']))
  • if ($user)

Why is that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I kept if (isset($response['refresh_token'])) because that's what it was before, no really good reason to change. This one won't error out attempting to set an empty one.

if (! empty($response['id_token']) is there because if it is empty then it will error out so need to be more careful. It's unlikely that it would be there but empty but still, better to be safe in this case.

if ($user) needs to be not empty and not null. empty($user), in this case would be the same.


$user = $this->authentication->userinfo($accessToken);
$this->setUser($user);
if ($user) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Method requires an array so we need to make sure we have something before attempting to set.

Copy link
Contributor

Choose a reason for hiding this comment

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

isn't user something that must always be set? what happens if you don't set it, shouldn't it be considered an error scenario?

In that case, why is this function returning true always?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isn't user something that must always be set?

Not if you only ask for API scopes including an audience param when authorizing. It's possible to use this function to handle a code exchange for an scoped API token without an ID token or access to /userinfo.

In that case, why is this function returning true always?

The exchange happened without an error so exchange() is true. It's not the most well-composed method but it's well-documented and used so we need to retain the basic functionality.

@@ -0,0 +1,152 @@
<?php
Copy link
Contributor Author

Choose a reason for hiding this comment

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

See Add failing test commit to see initial tests failing with existing functionality.

* @throws \Auth0\SDK\Exception\ApiException
* @throws \Auth0\SDK\Exception\CoreException
*/
public function testThatExchangeSucceedsWithIdToken()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This succeeded before the changes and after.

* @throws \Auth0\SDK\Exception\ApiException
* @throws \Auth0\SDK\Exception\CoreException
*/
public function testThatExchangeSucceedsWithNoIdToken()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This failed before the change and after.

* @throws \Auth0\SDK\Exception\ApiException
* @throws \Auth0\SDK\Exception\CoreException
*/
public function testThatExchangeSkipsUserinfo()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Additional test for the skip_userinfo config.

@joshcanhelp joshcanhelp merged commit 7c1113c into master Feb 4, 2019
@joshcanhelp joshcanhelp added this to the v5-Next-Minor milestone Feb 4, 2019
@joshcanhelp joshcanhelp deleted the fix-exchange-method-id-token-missing branch February 12, 2019 00:27
@github-actions
Copy link
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants