5

i just used laravel-passport,it is same as jwt auth.

i want to add some custom claims to my accessToken, is it possible ??

i want to pass 2fa_status => true in access token and while API call with this access token i also want that claim from token.

For e.g (Expected Claims Of Token)

{
  "aud": "7",
  "jti": "123",
  "iat": 1568368682,
  "nbf": 1568368682,
  "exp": 1599991082,
  "sub": "2",
  "scopes": [],
  "2fa_status": false
}

I'm generating token as below:

  $tokenResult = $user->createToken('Personal Access Token');
Vishal Ribdiya
  • 840
  • 6
  • 18

3 Answers3

1

Think something you can do is very similar to an answer of the this question: Customising token response Laravel Passport

While in your own BearerTokenResponse class, override generateHttpResponse method, inside it you can add whatever to the access token before convert it to JWT:

    public function generateHttpResponse(ResponseInterface $response)
    {
        $expireDateTime = $this->accessToken->getExpiryDateTime()->getTimestamp();

        // add custom claims here, ie. $this->accessToken->withClaim('name', 'value');

        $jwtAccessToken = $this->accessToken->convertToJWT($this->privateKey);

        ...
ImLeo
  • 991
  • 10
  • 17
0

I'm in a similar situation but I used password grant client to issue token to user during authentication and I need to used the personal access token for user to generate an access token for their self to be used in their 3rd party applications. You can do a workaround by updating the scope.I also need to verify the user if the 2fa has been pass.

in your

AuthServiceProvider.php

    public function boot()
    {
        $this->registerPolicies();

        Passport::routes(function ($router) {
            $router->forAccessTokens();
            $router->forPersonalAccessTokens();
            $router->forTransientTokens(); // register the transient token. skip if all routes are enabled
        });

        // Add scope to verify the user
        // take note that here 2 scope for this due to refresh token scope
        Passport::tokensCan([
            '2fa-pass' => '2FA Pass',
            '2fa-not-pass' => '2FA Pass',
        ]);
    }

next step is in your Authentication process where you send the password grant type

  // I'm using route::dispatch to do a proxy request
  // you can use Guzzle if you want
  $request->request->add([
    'grant_type' => 'password',
    'client_id' => 'client-id',
     client_secret' => 'client-secret',
    'username' => $credentials['email'],
    'password' => $credentials['password'],
    'scope' => '2fa-not-pass 2fa-pass' // take note that I added the two scope here
  ]);

  $tokenRequest = Request::create('/oauth/token', 'POST');

  $response = \Route::dispatch($tokenRequest);

then in your 2FA Verification process


   // your process of verifying the 2FA code

   // after that you need to update the scope by doing a refresh token
   $request->request->add([
     'grant_type' => 'refresh_token',
     'refresh_token' => $request->input('refresh_token'),
     'client_id' => 'client-id',
     'client_secret' => 'client-secret',
     'scope' => '2fa-pass' // I removed the 2fa-not-pass in refreshing the token
    ]);

  $tokenRequest = Request::create('/oauth/token', 'POST');

  $response = \Route::dispatch($tokenRequest);

Take note about the scopes, when you refresh the token, you can only obtain identical or narrower scopes than the original access token. If you attempt to get a scope that was not provided by the original access token, you will get an error. - patricus

answer here: https://stackoverflow.com/a/45856634/11537130

take note that this will be generate a new token with new scope

user11537130
  • 264
  • 4
  • 10
-1

it is possible

add this to AuthServiceProvider

Passport::routes();
Passport::personalAccessClientId(1); //<-- this 1 is id of your personal key
Passport::tokensExpireIn(now()->addDays(15));
Passport::refreshTokensExpireIn(now()->addDays(30));

now your can create new token like this

$user->createToken('email')->accessToken; // you can change email to any for remember why this code generated like social facebook

according to documents for add more params try this

$user->createToken('email', ['extra' => 'params'])->accessToken;

hope this helps

sid heart
  • 1,140
  • 1
  • 9
  • 38