[jsonwebtoken] Allow function as secretOrPublicKey argument for async verify

This commit is contained in:
Daniel Parker
2018-10-20 13:20:33 -05:00
parent 0e95a07e46
commit ddeae23cf4
2 changed files with 18 additions and 4 deletions
+5 -4
View File
@@ -3,7 +3,8 @@
// Definitions by: Maxime LUCE <https://github.com/SomaticIT>,
// Daniel Heim <https://github.com/danielheim>,
// Brice BERNARD <https://github.com/brikou>,
// Veli-Pekka Kestilä <https://github.com/vpk>
// Veli-Pekka Kestilä <https://github.com/vpk>,
// Daniel Parker <https://github.com/rlgod>
// 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;
+13
View File
@@ -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) {