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

Laravel 7.5.2 (Passport Api) + Vuejs Error : Undefined index: aud , \vendor\laravel\passport\src\Guards\TokenGuard.php #1243

Closed
mhabib555 opened this issue May 6, 2020 · 26 comments · Fixed by #1246

Comments

@mhabib555
Copy link

  • Passport Version: 9.0
  • Laravel Version: 7.5.2
  • PHP Version: 7.4.0
  • Database Driver & Version:
  • Database client version: libmysql - mysqlnd 7.4.0

Description:

I am using Laravel 7.5.2 with Vuejs. I am using passport for api authentication. I am getting the following error when sending ajax request to api

{
"message": "Undefined index: aud",
"exception": "ErrorException",
"file": "E:\\laravel\\vendor\\laravel\\passport\\src\\Guards\\TokenGuard.php",
"line": 140,
"trace": [
    {
...
}
]

Steps To Reproduce:

I have followed passport installation instruction on laravel website.

  • Has run composer require laravel/passport, php artisan migrate and php artisan passport:install
  • Has added HasApiTokens in user model
  • Has added Passport::routes() in AuthServiceProvider
  • Has added \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class in App\Http\Kernel web middlewareGroups
  • Has changed Api driver to passport in config/auth.php

The cookie named "laravel_token" is getting generated and sent with ajax request as I can see in developer tool.

In my controller I have included api middleware as

public function __construct(){
    $this->middleware('auth:api');
}

@TullariS
Copy link

TullariS commented May 6, 2020

Same here

@MUHAMMADSiyab
Copy link

Facing the same issue

@Zae
Copy link

Zae commented May 6, 2020

Having the same issue but with laravel 6 and passport 9.

@Zae
Copy link

Zae commented May 6, 2020

I think I might have found the issue.

Looks like /vendor/laravel/passport/src/ApiTokenCookieFactory.php:77 sets the index sub into the JWT token

But when decoding the cookie and trying to find the corresponding user \Laravel\Passport\Guards\TokenGuard::$clients tries to use the index aud to find the user.

However, the indexes have been like that for a LONG time now, so I'm not sure why this is suddenly a problem?

@joelennon
Copy link

joelennon commented May 6, 2020

I suspect it might be due to the removal of old php-jwt versions in this merged PR? #1236

Reverting to 8.x branch resolved the issue for me.

composer require laravel/passport:^8.0

@lcobucci
Copy link
Contributor

lcobucci commented May 6, 2020

I think I might have found the issue.

Looks like /vendor/laravel/passport/src/ApiTokenCookieFactory.php:77 sets the index sub into the JWT token

But when decoding the cookie and trying to find the corresponding user \Laravel\Passport\Guards\TokenGuard::$clients tries to use the index aud to find the user.

However, the indexes have been like that for a LONG time now, so I'm not sure why this is suddenly a problem?

The problem isn't with the sub claim but the aud one. Which is being used to identify the client (while the sub is meant for the user). The audience is just not being configured.

@lcobucci
Copy link
Contributor

lcobucci commented May 6, 2020

Looking at the diff, this seems to be the culprit (as it apparently assumes that aud claim is always there).

@driesvints do you have any idea on how can we solve this?

@driesvints
Copy link
Member

@lcobucci your link only shows all the commits between 8.5 and 9.0. Can you link to the specific file or pr that changed this?

@driesvints
Copy link
Member

Did everyone here read the upgrade guide and added the new provider column to the clients table?

https://github.com/laravel/passport/blob/9.x/UPGRADE.md

@lcobucci
Copy link
Contributor

lcobucci commented May 6, 2020

I've updated it already, sorry

@lcobucci
Copy link
Contributor

lcobucci commented May 6, 2020

@driesvints column is there in my case (I'm doing a clean php artisan passport:install -n btw). Created entries have null as provider (not sure if it helps).

@joelennon
Copy link

Hey @driesvints yeah this was happening for me on a fresh project, provider column is there.

@driesvints
Copy link
Member

It's mentioned that people are here using vue but I don't see any vuejs install steps in the steps to reproduce. Can anyone please post very specific steps to replicate this?

@akizor
Copy link

akizor commented May 6, 2020

Hey, I'm on a fresh install also. On version 9.0 I'm encountering the same error, while reverting to 8.5 solves it. Not using vue but old plain XHR request with following headers. The cookies are sent also.

'X-Requested-With': 'XMLHttpRequest',
'X-CSRF-TOKEN': THE_CSRF_TOKEN

@driesvints
Copy link
Member

If anyone can please post very specific steps to reproduce we can figure this out. Atm we can't reproduce this.

@billriess
Copy link
Contributor

billriess commented May 6, 2020

    /**
     * Get the user for the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return mixed
     */
    public function user(Request $request)
    {
        if ($request->bearerToken()) {
            return $this->hasValidProvider($request) ? $this->authenticateViaBearerToken($request) : null;
        } elseif ($request->cookie(Passport::cookie())) {
            return $this->authenticateViaCookie($request);
        }
    }

This may be the fix needed but I can't reproduce the error. Seeing how there is no client in this case makes sense logically but I need to verify. Can someone please give details steps as @driesvints has mentioned.

@joelennon
Copy link

In my instance I'm using React.

  1. laravel new test --auth
  2. cd test
  3. composer require laravel/passport
  4. php artisan migrate
  5. php artisan passport:install
  6. Add HasApiTokens to User model
  7. Change api guard to passport in config/auth.php
  8. Add Passport::routes() to AuthServiceProvider
  9. Add \Laravel\Passport\Http\Middleware\CreateFreshApiToken::class to web middleware group in app/Http/Kernel.php
  10. php artisan ui react --auth
  11. In home.blade.php add <div id="example"></div>
  12. Replace resources/js/components/Example.js with the following code:
import React, { useEffect } from 'react';
import ReactDOM from 'react-dom';

function Example() {
    const load = async () => {
        const data = await axios.get(`api/user`);

        console.log(data);
    }

    useEffect(() => {
        load();
    }, []);

    return null;
}

export default Example;

if (document.getElementById('example')) {
    ReactDOM.render(<Example />, document.getElementById('example'));
}
  1. npm install && npm run dev
  2. Open app in browser, create account, look in network requests and you'll see a 500 error
{
    "message": "Undefined index: aud",
    "exception": "ErrorException",
    "file": "/Users/joe/Code/Web/test/vendor/laravel/passport/src/Guards/TokenGuard.php",
    "line": 140,
    "trace": [
        {
            "file": "/Users/joe/Code/Web/test/vendor/laravel/passport/src/Guard
...

@billriess
Copy link
Contributor

Thanks, @joelennon for the detailed write-up. This was a great help in tracking this down. I have submitted a PR to resolve this. Please look at #1246

@driesvints
Copy link
Member

Thanks @joelennon, that was helpful 👍

@driesvints
Copy link
Member

Released v9.0.1 which should fix this.

@lcobucci
Copy link
Contributor

lcobucci commented May 6, 2020

Thanks everyone, the fix works nicely 👍

@driesvints
Copy link
Member

Thanks to everyone who reported this!

@mhabib555
Copy link
Author

Thanks everyone

@driesvints
Copy link
Member

If anyone here who has upgraded to v9 already and is using the new secrets hashing, please read https://blog.laravel.com/passport-v91-breaking-changes and #1252

@kbellpostman
Copy link

Ugg this cost me a lot of time earlier this week, happy to read I'm not crazy :)

@AbdullahiAbdulkabir
Copy link

I still can't resolve this in my case. I have a new laravel installation with the latest passport package installed.

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

Successfully merging a pull request may close this issue.