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

Auth0::exchange() assumes a valid id_token #317

Closed
macdaddyaz opened this issue Jan 28, 2019 · 5 comments
Closed

Auth0::exchange() assumes a valid id_token #317

macdaddyaz opened this issue Jan 28, 2019 · 5 comments
Labels
Scope: Bug Addressing unexpected problems or unintended behavior.
Milestone

Comments

@macdaddyaz
Copy link

macdaddyaz commented Jan 28, 2019

I am running into an error from the Auth0\SDK\Auth0::exchange() method. In the following snippet, it attempts to pull the id_token out of the code_exchange response:

        $idToken = false;
        if (isset($response['id_token'])) {
            $idToken = $response['id_token'];
        }

If there is no id_token, then the $idToken variable is just left with the value false. It then passes that value directly to the setIdToken method, which is not prepared to handle a non-JWT value. This results in the script throwing an exception:

[2019-01-28 18:11:49] local.ERROR: Wrong number of segments {"exception":"[object] (Auth0\\SDK\\Exception\\InvalidTokenException(code: 0): Wrong number of segments at /path/to/project/vendor/auth0/auth0-php/src/JWTVerifier.php:176)

It seems that the fix could be as simple as:

        $this->setAccessToken($accessToken);
        if ($idToken) { // Guard the setIdToken method
            $this->setIdToken($idToken);
        }
        $this->setRefreshToken($refreshToken);
@joshcanhelp
Copy link
Contributor

joshcanhelp commented Jan 29, 2019

@macdaddyaz - You're correct, this should definitely be more careful about that call. Happy to put through a PR and release to fix that.

That said, this works fine for a default setup. Can you provide me with the code you're using to generate a token response without an ID token?

@macdaddyaz
Copy link
Author

Well, that's a different matter entirely 😁 I'm using an auth code that is getting pushed to my app after an IdP-initiated SAML handshake. I actually have an open ticket to find out why that code is not associated with any scopes (including openid or profile). So it isn't my code per se that is getting such a token.

However, I could see someone legitimately needing a pure access token, like if they are only trying to invoke some API on the user's behalf. In that case, they might invoke login with an audience and some custom scopes, but not the openid scope (because the API isn't going to invoke the /userinfo endpoint). So that would be a case where the exchange would not return an ID token.

@joshcanhelp
Copy link
Contributor

joshcanhelp commented Jan 30, 2019

OK, that makes sense. I wanted to see what your specific case is but we can recreate it with exactly what you explained in your second paragraph:

// login.php
$auth0 = new Auth0( [
    'domain'        => AUTH0_DOMAIN,
    'client_id'     => AUTH0_CLIENT_ID,
    'client_secret' => AUTH0_CLIENT_SECRET,
    'redirect_uri'  => 'https://localhost/callback.php',
    'audience'      => 'https://api.identifier',
    'scope'         => 'read:messages',
] );
$auth0->login();

// callback.php
$auth0 = new Auth0( [
    'domain'        => AUTH0_DOMAIN,
    'client_id'     => AUTH0_CLIENT_ID,
    'client_secret' => AUTH0_CLIENT_SECRET,
] );
$auth0->getUser();

... throws:

Fatal error: Uncaught Auth0\SDK\Exception\InvalidTokenException: Wrong number of segments in /Users/josh/Sites/php-auth0/auth0/src/JWTVerifier.php on line 176

Thanks again for the report, I'll get a PR in for that ASAP.

@joshcanhelp
Copy link
Contributor

Fixed in master! We have a couple more tasks to do for the next minor and we'll get this out.

@joshcanhelp joshcanhelp added this to the v5-Next-Minor milestone Feb 4, 2019
@joshcanhelp joshcanhelp added the Scope: Bug Addressing unexpected problems or unintended behavior. label Feb 4, 2019
@github-actions
Copy link
Contributor

This issue 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
Scope: Bug Addressing unexpected problems or unintended behavior.
Projects
None yet
Development

No branches or pull requests

2 participants