diff --git a/types/jsonwebtoken/index.d.ts b/types/jsonwebtoken/index.d.ts index f54d3862c9..835164df0f 100644 --- a/types/jsonwebtoken/index.d.ts +++ b/types/jsonwebtoken/index.d.ts @@ -3,7 +3,8 @@ // Definitions by: Maxime LUCE , // Daniel Heim , // Brice BERNARD , -// Veli-Pekka Kestilä +// Veli-Pekka Kestilä , +// Daniel Parker // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 @@ -143,18 +144,18 @@ export declare function verify( /** * Asynchronously verify given token using a secret or a public key to get a decoded token * @param {String} token - JWT string to verify - * @param {String|Buffer} secretOrPublicKey - Either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA. + * @param {String|Buffer|Function} secretOrPublicKey - A string or buffer containing either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA. If jwt.verify is called asynchronous, secretOrPublicKey can be a function that should fetch the secret or public key * @param {VerifyOptions} [options] - Options for the verification * @param {Function} callback - Callback to get the decoded token on */ export declare function verify( token: string, - secretOrPublicKey: string | Buffer, + secretOrPublicKey: string | Buffer | Function, callback?: VerifyCallback, ): void; export declare function verify( token: string, - secretOrPublicKey: string | Buffer, + secretOrPublicKey: string | Buffer | Function, options?: VerifyOptions, callback?: VerifyCallback, ): void; diff --git a/types/jsonwebtoken/jsonwebtoken-tests.ts b/types/jsonwebtoken/jsonwebtoken-tests.ts index f20fbba5cc..6f9977995e 100644 --- a/types/jsonwebtoken/jsonwebtoken-tests.ts +++ b/types/jsonwebtoken/jsonwebtoken-tests.ts @@ -83,6 +83,19 @@ jwt.verify(token, cert, function(err, decoded) { console.log(result.foo); // bar }); +// verify a token assymetric with async key fetch function +function getKey(header: Object, callback: Function) { + cert = fs.readFileSync("public.pem"); + + callback(null, cert); +} + +jwt.verify(token, getKey, function(err, decoded) { + const result = decoded as ITestObject; + + console.log(result.foo); // bar +}); + // verify audience cert = fs.readFileSync("public.pem"); // get public key jwt.verify(token, cert, { audience: "urn:foo" }, function(err, decoded) {