Skip to content

Commit 5655ec4

Browse files
authored
Merge pull request #301 from carboneater/master
fix: types-compabitility for express-jwt @ 7
2 parents 9eab681 + c98ac1f commit 5655ec4

File tree

4 files changed

+145
-78
lines changed

4 files changed

+145
-78
lines changed

index.d.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { SecretCallback, SecretCallbackLong } from 'express-jwt';
21
import { Agent as HttpAgent } from 'http';
32
import { Agent as HttpsAgent } from 'https';
3+
import type {Jwt, Secret} from 'jsonwebtoken'
44

55
declare function JwksRsa(options: JwksRsa.Options): JwksRsa.JwksClient;
66

@@ -55,7 +55,24 @@ declare namespace JwksRsa {
5555

5656
type SigningKey = CertSigningKey | RsaSigningKey;
5757

58-
function expressJwtSecret(options: ExpressJwtOptions): SecretCallbackLong;
58+
/**
59+
* Types are duplicated from express-jwt@6/7
60+
* due to numerous breaking changes in the lib's types
61+
* whilst this lib supportd both <=6 & >=7 implementations
62+
*
63+
* express-jwt's installed version (or its @types)
64+
* will be the types used at transpilation time
65+
*/
66+
67+
/** Types from express-jwt@<=6 */
68+
type secretType = string|Buffer;
69+
type SecretCallbackLong = (req: Express.Request, header: any, payload: any, done: (err: any, secret?: secretType) => void) => void;
70+
type SecretCallback = (req: Express.Request, payload: any, done: (err: any, secret?: secretType) => void) => void;
71+
72+
/** Types from express-jwt@>=7 */
73+
type GetVerificationKey = (req: Express.Request, token: Jwt | undefined) => Secret | Promise<Secret>;
74+
75+
function expressJwtSecret(options: ExpressJwtOptions): SecretCallbackLong|GetVerificationKey;
5976

6077
function passportJwtSecret(options: ExpressJwtOptions): SecretCallback;
6178

package-lock.json

+99-61
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@
1212
"node": ">=10 < 13 || >=14"
1313
},
1414
"dependencies": {
15-
"@types/express-jwt": "0.0.42",
15+
"@types/express": "^4.17.13",
1616
"debug": "^4.3.4",
1717
"jose": "^2.0.5",
1818
"limiter": "^1.1.5",
1919
"lru-memoizer": "^2.1.4"
2020
},
2121
"devDependencies": {
2222
"@types/chai": "^4.2.11",
23+
"@types/express-jwt": "^6.0.4",
24+
"@types/jsonwebtoken": "^8.5.8",
2325
"@types/mocha": "^5.2.7",
2426
"@types/nock": "^11.0.0",
2527
"@types/node": "^14.14.12",

tests/ts-definitions.tests.ts

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import * as jwksRsaType from '../index';
22
import {expect} from 'chai';
3+
import expressjwt6 from "express-jwt";
4+
import { expressjwt as expressjwt7, GetVerificationKey } from "express-jwt-v7";
35
const { jwksEndpoint } = require('../tests/mocks/jwks');
46
const { publicKey } = require('../tests/mocks/keys');
7+
const { x5cSingle } = require('../tests/keys.js');
58
const jwksRsa: typeof jwksRsaType = require('../src');
69

710
describe('typescript definition', () => {
8-
const jwksHost = 'http://my-authz-server';
11+
const jwksHost = 'http://localhost';
912

1013
describe('hapiJwt2KeyAsync', () => {
1114
it('should return a secret provider function', async () => {
@@ -24,24 +27,31 @@ describe('typescript definition', () => {
2427
});
2528
});
2629

27-
describe('getKeysInterceptor', async () => {
28-
const keySetResponse = {
29-
keys: [
30-
{
31-
alg: 'RS256',
32-
kty: 'RSA',
33-
use: 'sig',
34-
kid: 'NkFCNEE1NDFDNTQ5RTQ5OTE1QzRBMjYyMzY0NEJCQTJBMjJBQkZCMA'
35-
}
36-
]
37-
};
38-
30+
it('getKeysInterceptor', async () => {
3931
const client = new jwksRsa.JwksClient({
4032
jwksUri: `${jwksHost}/.well-known/jwks.json`,
41-
getKeysInterceptor: () => Promise.resolve(keySetResponse.keys)
33+
getKeysInterceptor: () => Promise.resolve(x5cSingle.keys)
4234
});
4335

4436
const key = await client.getSigningKey('NkFCNEE1NDFDNTQ5RTQ5OTE1QzRBMjYyMzY0NEJCQTJBMjJBQkZCMA');
4537
expect(key.kid).to.equal('NkFCNEE1NDFDNTQ5RTQ5OTE1QzRBMjYyMzY0NEJCQTJBMjJBQkZCMA');
4638
});
39+
40+
it('Types-Only Validation with express-jwt', () => {
41+
expressjwt6({
42+
algorithms: ["RS256"],
43+
secret: jwksRsa.expressJwtSecret({
44+
cache: true,
45+
jwksUri: `https://my-authz-server/.well-known/jwks.json`
46+
})
47+
});
48+
49+
expressjwt7({
50+
algorithms: ['RS256'],
51+
secret: jwksRsa.expressJwtSecret({
52+
cache: true,
53+
jwksUri: `https://my-authz-server/.well-known/jwks.json`
54+
}) as GetVerificationKey
55+
});
56+
})
4757
});

0 commit comments

Comments
 (0)