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
$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;
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",