Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Add the ability to set custom claims #9

Open
ludo237 opened this issue Feb 4, 2020 · 6 comments
Open

[Feature Request] Add the ability to set custom claims #9

ludo237 opened this issue Feb 4, 2020 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@ludo237
Copy link

ludo237 commented Feb 4, 2020

It would be nice to add custom claims before receiving the token from the Auth guard. What do you think?

@ollieread
Copy link
Member

You can actually already do this @ludo237 using custom generation. This is how tokens are generated without a custom generator, so you could just provide your own generation and add your own custom claims.

@ludo237
Copy link
Author

ludo237 commented Feb 4, 2020

Oh I saw the README I have two final questions: $instanteOfBuilder should be my custom guard class that implements the Guard contract therefore it should implements generateToken() right?

Should I put my setTokenGenerator() into AuthserviceProvider or in another location?

@ollieread
Copy link
Member

@ludo237 You do not need a custom guard.

If you put this in a service provider, doesn't matter which one as long as it's in the register() method, it will recreate the way it currently generates tokens. You can add your custom claims here.

Auth::guard('api')->setTokenGenerator(function (\Illuminate\Contracts\Auth\Authenticatable $user, \Sprocketbox\JWT\JWTGuard $guard) {
    $time   = Carbon::now();
    $expiry = new CarbonInterval($this->config['ttl']);
    
    return (new Builder)
        ->issuedBy(config('app.url'))
        ->permittedFor(config('app.url'))
        ->identifiedBy(Uuid::uuid4()->toString())
        ->issuedAt($time->timestamp)
        ->expiresAt($time->copy()->add($expiry)->timestamp)
        ->relatedTo($user->getAuthIdentifier());
});

The generator must return an instance of Lcobucci\JWT\Builder.

@ludo237
Copy link
Author

ludo237 commented Feb 4, 2020

oh interesting, I need to experiment a bit because I'd like to pass a custom parameter for the claim

@ollieread
Copy link
Member

@ludo237 You have given me an idea though.

@ollieread ollieread reopened this Feb 7, 2020
@ollieread ollieread self-assigned this Feb 7, 2020
@ollieread ollieread added the enhancement New feature or request label Feb 7, 2020
@ludo237
Copy link
Author

ludo237 commented Feb 7, 2020

Glad I did. if I can help with the code don't esitate ask me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants