corbosman / laravel-passport-claims

Add claims to Laravel Passport JWT Tokens
MIT License
82 stars 11 forks source link

add new claim to token #19

Closed chaotic98 closed 1 year ago

chaotic98 commented 1 year ago

how can i add new claim when receving a access_token in /oauth/token route? i need to add more user details for my access_token

"token_type": "Bearer",
    "expires_in": 900,
    "access_token": "{token}",
    "refresh_token": "{refresh_token}"
corbosman commented 1 year ago

This package is not able to add claims to existing tokens. This package hooks into the Laravel Passport token generation and adds claims during creation of a token. AFAIK it's not possible to add claims to an existing JWT. You'd have to decode it, then take the existing claims, and create a new token with those claims and your own claims.

chaotic98 commented 1 year ago

ok i did as the readme said and added the user email info claim the code is like this `` $user = User::find($token->getUserIdentifier());

    $token->addClaim('email', $user->email);
    return $next($token);

`` the function works and if i dd() the $user i can see the information but when i decode it i cant see the email. i use this package https://github.com/peterpetrus/passport-token

corbosman commented 1 year ago

It looks like that package does not return custom claims. Look:

A token as created by passport using my package with an additional username field:

eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJhdWQiOiI0IiwianRpIjoiM2U2OWE2NTVhNWY1MDc3N2FjYjY0OWUyZjcxMDkyNmM5ZWQzNGUzYmVhMDc4NmFiM2VhMDFjM2IyODQyMTg3NmE0ZTIyNWY1ZDU0YzY3NTkiLCJpYXQiOjE2NzMyNjc0NTcsIm5iZiI6MTY3MzI2NzQ1NywiZXhwIjoxNjczMzAzNDU3LCJzdWIiOiIxIiwic2NvcGVzIjpbImxvZ2luIiwibmFtZSIsImVtYWlsIiwiZ3JhdmF0YXIiXSwidXNlcm5hbWUiOiJjb3IiLCJjbGllbnRfbmFtZSI6Im1haWxhcmNoaXZlIn0.cdIJ20nvZ3qoYP50iMbo_BTSWEV18PCYLQJHKoqudyTkQn5e2vAg7olbPQUgXISO6Z2Ndb9px9GLs1lk2Bus8K83U2EXTVK3f07KYUd8mmxltLMU-SNde_Ptj4BGo3UjxfWhSrkcviyRsy3cLzsKXSW1EoO3mIeBGJBO1h1yY8qEvhLByMueXaDzQaorexbl3qegkxT7UMqm8JKW1kXFmiMxwRsmKjmOyYAoZdBL8oC2NRu-EzJjk85n2dZzniBKkPmdPpgdxM7ugORvwZerQStFOU3gfV85h35GQDlEv9lvReGk85JlmjhqmnQkqgeKP-mYJFe0OJGzh6uR_sKsVF9vYotkW59kLFb1Obk_grfdVmW-vAvPI1m2iO8B06cTx4-SeRkjK9vJN8h10oNPq9aNM4cPCgD6IzCdgqShYHOgAnm-fGj30s_S2rTv9jXBokoyif7XjA75Tf7rzeSQ8w0sa11sGGR_CJx0508oeX-9MupeOhgYxLS6BBrRLh6IU2zFyFtZ9Aefy47KXBRm8GVBPu2sscoHghb19GQGqaHnj_CtlTx3ndT_sUd2k8aDGSJCglt9Zp0fsThnQanU7hY9xlujPBAIxmLlEakh9NnIb72aodGl1yHvw8YTb3KB1WeB7l2YOgbBNlLLAHRXezWe4NEdOxrfRdVos7tGNj0  

The package you mentioned returns the following decoded token:

[2023-01-09 13:30:58] local.DEBUG: array (
  'token_id' => '3e69a655a5f50777acb649e2f710926c9ed34e3bea0786ab3ea01c3b28421876a4e225f5d54c6759',
  'user_id' => '1',
  'expecting' => false,
  'start_at_unix' => 1673267457,
  'start_at' => '2023-01-09T13:30:57+0100',
  'incorrect' => false,
  'created_at_unix' => 1673267457,
  'created_at' => '2023-01-09T13:30:57+0100',
  'expired' => false,
  'expires_at_unix' => 1673303457,
  'expires_at' => '2023-01-09T23:30:57+0100',
  'error' => false,
  'errors' => 
  array (
  ),
  'valid' => true,
)  

But if you use a service like https://jwt.io you see the following screenshot: Screenshot 2023-01-09 at 13 33 30

chaotic98 commented 1 year ago

thanks a lot, you're a life saver ❤