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": [
{
...
}
]
I have followed passport installation instruction on laravel website.
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');
}
Same here
Facing the same issue
Having the same issue but with laravel 6 and passport 9.
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?
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
I think I might have found the issue.
Looks like
/vendor/laravel/passport/src/ApiTokenCookieFactory.php:77sets the indexsubinto the JWT tokenBut when decoding the cookie and trying to find the corresponding user
\Laravel\Passport\Guards\TokenGuard::$clientstries to use the indexaudto 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.
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?
@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?
Did everyone here read the upgrade guide and added the new provider column to the clients table?
I've updated it already, sorry
@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).
Hey @driesvints yeah this was happening for me on a fresh project, provider column is there.
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?
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
If anyone can please post very specific steps to reproduce we can figure this out. Atm we can't reproduce this.
/**
* 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.
In my instance I'm using React.
laravel new test --authcd testcomposer require laravel/passportphp artisan migratephp artisan passport:installHasApiTokens to User modelapi guard to passport in config/auth.phpPassport::routes() to AuthServiceProvider\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class to web middleware group in app/Http/Kernel.phpphp artisan ui react --authhome.blade.php add <div id="example"></div>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'));
}
npm install && npm run dev{
"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
...
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 https://github.com/laravel/passport/pull/1246
Thanks @joelennon, that was helpful 馃憤
Released v9.0.1 which should fix this.
Thanks everyone, the fix works nicely :+1:
Thanks to everyone who reported this!
Thanks everyone
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 https://github.com/laravel/passport/issues/1252
Ugg this cost me a lot of time earlier this week, happy to read I'm not crazy :)
I still can't resolve this in my case. I have a new laravel installation with the latest passport package installed.
Most helpful comment
Released v9.0.1 which should fix this.