Skip to content

Commit

Permalink
add secret rotation example in readme. close #310
Browse files Browse the repository at this point in the history
  • Loading branch information
jfromaniello committed Dec 22, 2022
1 parent bfb03cf commit 0000a44
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ app.get(
);
```

### Secret rotation

The getSecret callback could also be used in cases where the same issuer might issue tokens with different keys at certain point:

```js
var getSecret = async function (req, token) {
const { iss } = token.payload;
const { kid } = token.header;
// get the verification key by a given key-id and issuer.
return verificationKey;
};
```

### Revoked tokens

It is possible that some tokens will need to be revoked so they cannot be used any longer. You can provide a function as the `isRevoked` option. The signature of the function is `function(req, payload, done)`:
Expand Down
15 changes: 8 additions & 7 deletions test/jwt.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import * as jwt from 'jsonwebtoken';
import * as express from 'express';
import { expressjwt, UnauthorizedError, ExpressJwtRequest, GetVerificationKey } from '../src';
import { expressjwt, UnauthorizedError, Request, GetVerificationKey } from '../src';
import * as assert from 'assert';


Expand Down Expand Up @@ -279,7 +279,7 @@ describe('work tests', function () {
it('should work if authorization header is valid jwt', function (done) {
const secret = 'shhhhhh';
const token = jwt.sign({ foo: 'bar' }, secret);
const req = {} as ExpressJwtRequest;
const req = {} as Request;
const res = {} as express.Response;
req.headers = {};
req.headers.authorization = 'Bearer ' + token;
Expand All @@ -292,7 +292,7 @@ describe('work tests', function () {
it('should work if authorization header is valid with a buffer secret', function (done) {
const secret = Buffer.from('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', 'base64');
const token = jwt.sign({ foo: 'bar' }, secret);
const req = {} as ExpressJwtRequest;
const req = {} as Request;
const res = {} as express.Response;

req.headers = {};
Expand All @@ -306,7 +306,7 @@ describe('work tests', function () {
it('should work if Authorization header is capitalized (lambda environment)', function (done) {
const secret = Buffer.from('AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', 'base64');
const token = jwt.sign({ foo: 'bar' }, secret);
const req = {} as ExpressJwtRequest;
const req = {} as Request;
const res = {} as express.Response;

req.headers = {};
Expand Down Expand Up @@ -349,7 +349,7 @@ describe('work tests', function () {
});

it('should work with a custom getToken function', function (done) {
const req = {} as ExpressJwtRequest;
const req = {} as Request;
const res = {} as express.Response;
const secret = 'shhhhhh';
const token = jwt.sign({ foo: 'bar' }, secret);
Expand All @@ -373,7 +373,7 @@ describe('work tests', function () {
});

it('should work with an async getToken function', function (done) {
const req = {} as ExpressJwtRequest;
const req = {} as Request;
const res = {} as express.Response;
const secret = 'shhhhhh';
const token = jwt.sign({ foo: 'bar' }, secret);
Expand All @@ -397,10 +397,11 @@ describe('work tests', function () {
});

it('should work with a secretCallback function that accepts header argument', function (done) {
const req = {} as ExpressJwtRequest;
const req = {} as Request;
const res = {} as express.Response;
const secret = 'shhhhhh';
const getSecret: GetVerificationKey = async (req, token) => {
// @ts-ignore
assert.equal(token.header.alg, 'HS256');
// @ts-ignore
assert.equal(token.payload.foo, 'bar');
Expand Down

0 comments on commit 0000a44

Please sign in to comment.