Add Buffer type to property 'key' in Secret (#38360)

This commit is contained in:
Robert "RunAge" Gajda 2019-09-18 17:13:57 +00:00 committed by Daniel Rosenwasser
parent 9ea7f6359d
commit 4878bcc79f
2 changed files with 13 additions and 5 deletions

View File

@ -5,7 +5,8 @@
// Brice BERNARD <https://github.com/brikou>,
// Veli-Pekka Kestilä <https://github.com/vpk>,
// Daniel Parker <https://github.com/rlgod>,
// Kjell Dießel <https://github.com/kettil>
// Kjell Dießel <https://github.com/kettil>,
// Robert Gajda <https://github.com/RunAge>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
@ -82,7 +83,10 @@ export interface DecodeOptions {
complete?: boolean;
json?: boolean;
}
export type VerifyErrors= JsonWebTokenError | NotBeforeError | TokenExpiredError;
export type VerifyErrors =
| JsonWebTokenError
| NotBeforeError
| TokenExpiredError;
export type VerifyCallback = (
err: VerifyErrors,
decoded: object | string,
@ -111,7 +115,10 @@ export type GetPublicKeyOrSecret = (
callback: SigningKeyCallback
) => void;
export type Secret = string | Buffer | { key: string; passphrase: string };
export type Secret =
| string
| Buffer
| { key: string | Buffer; passphrase: string };
/**
* Synchronously sign the given payload into a JSON Web Token string

View File

@ -42,6 +42,7 @@ token = jwt.sign(testObject, cert, { algorithm: "RS256" });
const privKey: Buffer = fs.readFileSync("encrypted_private.key"); // get private key
const secret = { key: privKey.toString(), passphrase: "keypwd" };
token = jwt.sign(testObject, secret, { algorithm: "RS256" }); // the algorithm option is mandatory in this case
token = jwt.sign(testObject, { key: privKey, passphrase: 'keypwd' }, { algorithm: "RS256" });
// sign asynchronously
jwt.sign(testObject, cert, { algorithm: "RS256" }, (
@ -64,9 +65,9 @@ jwt.verify(token, "shhhhh", (err, decoded) => {
// use external time for verifying
jwt.verify(token, 'shhhhh', { clockTimestamp: 1 }, (err, decoded) => {
const result = decoded as TestObject;
const result = decoded as TestObject;
console.log(result.foo); // bar
console.log(result.foo); // bar
});
// invalid token