Skip to content

Allow secret to be passed as an option to sign and verify - #336

Merged
kamilmysliwiec merged 2 commits into
nestjs:masterfrom
wprk:master
Jul 9, 2020
Merged

Allow secret to be passed as an option to sign and verify#336
kamilmysliwiec merged 2 commits into
nestjs:masterfrom
wprk:master

Conversation

@wprk

@wprk wprk commented Jul 1, 2020

Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Other... Please describe:

What is the current behavior?

There is currently no way to override the secret passed in from the module. This makes it difficult if you want to use a different secret for two different sign calls for example and access token and a refresh token

Issue Number: #302

What is the new behavior?

The new behaviour allows you to pass a secret in to the sign, signAsync, verify and verifyAsync methods to override the secret from the one passed into the module. It does not override the secretOrKeyProvider though.

Does this PR introduce a breaking change?

[ ] Yes
[x] No

Other information

It's possible you may want the secret to override the secretOrKeyProvider but I wasn't sure. Happy to tweak this PR if you'd prefer it that way.

wprk added 2 commits July 1, 2020 20:43
This allows you to pass the `secret` property in the options of sign, signAsync, verify and verifyAsync. It does not override a secretOrKeyProvider though only the secret passed into the JwtModule.

Resolves #302
Comment thread lib/jwt.service.ts
sign(payload: string | Buffer | object, options?: JwtSignOptions): string {
const signOptions = this.mergeJwtOptions(
options,
{ ...options },

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this spread for some TypeScript error?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spread it because I needed a copy of the object so I could remove the secret property in the mergeJwtOptions function without it affecting the original object.

@kamilmysliwiec
kamilmysliwiec merged commit 4f67f04 into nestjs:master Jul 9, 2020
@kamilmysliwiec

Copy link
Copy Markdown
Member

Thank you!

@maitrungduc1410

maitrungduc1410 commented Sep 8, 2020

Copy link
Copy Markdown
Contributor

Hi @kamilmysliwiec , do you think this should also be possible to override publicKey and privateKey.

I think in production many people (including me) using RSA key instead secret to sign JWT token. And if secret can be override then public/private Key should be same.

Thank you

@kamilmysliwiec

Copy link
Copy Markdown
Member

@maitrungduc1410 would you like to create a PR for this?

@maitrungduc1410

Copy link
Copy Markdown
Contributor

@kamilmysliwiec I just created PR #401 for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants