Passport: How to get The authenticated Client for client_credentials grant

Created on 8 Oct 2016  路  5Comments  路  Source: laravel/passport

i'm using client_credentials grant type for Authorization to protect some resources and it's working fine,
i need to get id of the authenticated client
How to do that
thx

Most helpful comment

FYI this seems to be changed using latest Passport and Laravel 7. getHeaders no longer contains jti, this is moved into payload and can now be retrieved via getClaim().

package versions used are:
"laravel/framework": "^7.0", "laravel/passport": "^8.0",

use Laravel\Passport\Token;
use Lcobucci\JWT\Parser;

$bearerToken = request()->bearerToken();
$tokenId = (new Parser())->parse($bearerToken)->getClaim('jti');
$client = Token::find($tokenId)->client;
return $client;

All 5 comments

$bearerToken=$request->bearerToken();
$tokenId= (new Parser())->parse($bearerToken)->getHeader('jti');
$client = Token::find($tokenId)->client;

For anybody looking, the "Parser" class used here is
\Lcobucci\JWTParser

"Token" class used here is:
\Laravel\PassportToken

( I think?)

FYI this seems to be changed using latest Passport and Laravel 7. getHeaders no longer contains jti, this is moved into payload and can now be retrieved via getClaim().

package versions used are:
"laravel/framework": "^7.0", "laravel/passport": "^8.0",

use Laravel\Passport\Token;
use Lcobucci\JWT\Parser;

$bearerToken = request()->bearerToken();
$tokenId = (new Parser())->parse($bearerToken)->getClaim('jti');
$client = Token::find($tokenId)->client;
return $client;

@Megachill Your upgraded solution still works on "laravel/framework": "^8.12", "laravel/passport": "^10.0",
Thanks!

With lcobucci/jwt:^4.0 (check by running composer show -i Lcobucci/*), the following code works for me:

use Laravel\Passport\Token;
use Lcobucci\JWT\Configuration;


$bearerToken = request()->bearerToken();
$tokenId = Configuration::forUnsecuredSigner()->parser()->parse($bearerToken)->claims()->get('jti');
$client = Token::find($tokenId)->client;
Was this page helpful?
0 / 5 - 0 ratings

Related issues

billriess picture billriess  路  29Comments

lucasmichot picture lucasmichot  路  71Comments

monoland picture monoland  路  28Comments

adaojunior picture adaojunior  路  52Comments

corbosman picture corbosman  路  44Comments