Skip to content

Commit

Permalink
jsonwebtoken 8.2
Browse files Browse the repository at this point in the history
  • Loading branch information
marudor committed Apr 9, 2018
1 parent 88a77ba commit 8caaec1
Show file tree
Hide file tree
Showing 2 changed files with 180 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
declare module "jsonwebtoken" {
declare module.exports: {
sign: jwt$Sign,
decode: jwt$Decode,
verify: jwt$Verify,
JsonWebTokenError: Class<jwt$WebTokenError>,
NotBeforeError: Class<jwt$NotBeforeError>,
TokenExpiredError: Class<jwt$TokenExpiredError>
}
}

declare type jwt$Encodable = String | Buffer | Object;
declare type jwt$Key = { key: string | Buffer, passphrase: string | Buffer };
declare type jwt$Algorithm =
'RS256'
| 'RS384'
| 'RS512'
| 'ES256'
| 'ES384'
| 'ES512'
| 'HS256'
| 'HS384'
| 'HS512'
| 'none';

declare type jwt$Callback = (tokenOrError: Error | string) => void;
declare type jwt$SigningOptions<Headers> = $Shape<{
algorithm: jwt$Algorithm,
expiresIn: number | string,
notBefore: number | string,
audience: string | string[],
issuer: string,
jwtid: string,
subject: string,
noTimestamp: boolean,
header: Headers,
keyid: string,
mutatePayload: boolean,
}>;

declare type jwt$SigningOptionsWithAlgorithm<H> = jwt$SigningOptions<H> & { algorithm: jwt$Algorithm };
declare type jwt$VerifyOptionsWithAlgorithm = jwt$VerifyOptions & { algorithms: Array<jwt$Algorithm> };

declare type jwt$VerifyOptions = $Shape<{
algorithms: Array<jwt$Algorithm>,
audience: string,
issuer: string | string[],
ignoreExpiration: boolean,
ignoreNotBefore: boolean,
subject: string | string[],
clockTolerance: number,
maxAge: string | number,
clockTimestamp: number
}>;

declare type jwt$DecodingOptions = $Shape<{
complete: boolean,
json: boolean,
encoding: string,
}>;

declare interface jwt$Sign {
<P: jwt$Encodable>
(payload: P, secretOrPrivateKey: string | Buffer): string;

<P: jwt$Encodable>
(payload: P, secretOrPrivateKey: string | Buffer, callback: jwt$Callback): string;

<P: jwt$Encodable, H>
(payload: P, secretOrPrivateKey: jwt$Key, options: jwt$SigningOptionsWithAlgorithm<H>): string;

<P: jwt$Encodable, H>
(payload: P, secretOrPrivateKey: string | Buffer, options: $Shape<jwt$SigningOptions<H>>): string;

<P: jwt$Encodable, H>
(payload: P, secretOrPrivateKey: string | Buffer, options: $Shape<jwt$SigningOptions<H>>, callback: jwt$Callback): string;

<P: jwt$Encodable, H>
(payload: P, secretOrPrivateKey: jwt$Key, options: jwt$SigningOptionsWithAlgorithm<H>, callback: jwt$Callback): string;
}

declare interface jwt$Decode {
(jwt: string): mixed;

(jwt: string, options: jwt$DecodingOptions): mixed;

(jwt: string, options: jwt$DecodingOptions & { complete: true }): { header: Object, payload: mixed, signature: string };
}

declare interface jwt$Verify {
(jwt: string, secretOrPrivateKey: string | Buffer): mixed;

(jwt: string, secretOrPrivateKey: string | Buffer, options: jwt$VerifyOptions | jwt$Callback): mixed;

(jwt: string, secretOrPrivateKey: string | Buffer, options: jwt$VerifyOptions, callback: jwt$Callback): mixed;

(jwt: string, secretOrPrivateKey: jwt$Key, options: jwt$VerifyOptionsWithAlgorithm): mixed;

(jwt: string, secretOrPrivateKey: jwt$Key, options: jwt$VerifyOptionsWithAlgorithm, callback: jwt$Callback): mixed;
}

declare class jwt$TokenExpiredError extends Error {
}

declare class jwt$WebTokenError extends Error {
}

declare class jwt$NotBeforeError extends Error {
}
71 changes: 71 additions & 0 deletions definitions/npm/jsonwebtoken_v8.2.x/test_jsonwebtoken_v8.2.x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// @flow

const jwt = require('jsonwebtoken');

const payload = {
foo: 'bar'
};

// $ExpectError
jwt.sign(payload);

jwt.sign(payload, 'secret');

// $ExpectError
jwt.sign(payload, 'secret', (token: number) => undefined);

jwt.sign(payload, 'secret', (token: Error | string) => undefined);

jwt.sign(payload, Buffer.from('secret'));

jwt.sign(payload, Buffer.from('secret'), {
algorithm: 'RS256'
});

jwt.sign(payload, Buffer.from('secret'), {
algorithm: 'RS256',
mutatePayload: true,
});

// $ExpectError
jwt.sign(payload, Buffer.from('secret'), {
algorithm: 'RS256',
mutatePayload: 1,
});

// $ExpectError
jwt.sign(payload, Buffer.from('secret'), {
algorithm: 'PEMDAS'
});

// $ExpectError
jwt.sign(payload, { key: 'foo', passphrase: 'bar' });

// $ExpectError
jwt.sign(payload, { key: 'foo', passphrase: 'bar' }, { subject: 'missing algo' });

// $ExpectError
jwt.sign(payload, { key: 'foo', passphrase: 'bar' }, (foo) => undefined);

jwt.sign(payload, { key: 'foo', passphrase: 'bar' }, { algorithm: 'ES512' }, (foo) => undefined);

jwt.verify('token', 'secret');

jwt.verify('token', 'secret', (foo) => undefined);

// $ExpectError
jwt.verify('token', { key: 'secret', passphrase: 'foo' });

// $ExpectError
jwt.verify('token', { key: 'secret', passphrase: 'foo' }, { algorithms: ['not-real'] });

jwt.verify('token', { key: 'secret', passphrase: 'foo' }, { algorithms: ['ES512'] });

jwt.decode('foo');

jwt.decode('foo', {
json: true
});

// $ExpectError
jwt.decode(1234);

0 comments on commit 8caaec1

Please sign in to comment.