Skip to content

Commit

Permalink
feat(jsonwebtoken): improve jsonwebtoken types (#2036)
Browse files Browse the repository at this point in the history
* Update jsonwebtoken

* Minor fixies

* Remove whitespace

* Expose sign options

* Minor improvements
  • Loading branch information
facundo-viale-olx authored and gantoine committed Apr 12, 2018
1 parent a802404 commit 10d0995
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 101 deletions.
222 changes: 128 additions & 94 deletions definitions/npm/jsonwebtoken_v8.2.x/flow_v0.56.x-/jsonwebtoken_v8.2.x.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,143 @@
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 class JsonWebTokenError extends Error {
name: string;
message: string;
inner: Error;
}
}

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 class TokenExpiredError extends Error {
name: string;
expiredAt: number;
inner: Error;
}

declare interface jwt$Decode {
(jwt: string): mixed;
declare class NotBeforeError extends Error {
name: string;
date: Date;
inner: Error;
}

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

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

declare type SigningOptionsWithAlgorithm<H> = SigningOptions<H> & { algorithm: Algorithm };

declare type VerifyCallback = (err: JsonWebTokenError | NotBeforeError | TokenExpiredError | null, decoded: Payload) => void;
declare type VerifyOptionsWithAlgorithm = VerifyOptions & { algorithms: Array<Algorithm> };
declare type VerifyOptions = $Shape<{
algorithms: Array<Algorithm>,
audience: string | string[],
issuer: string | string[],
ignoreExpiration: boolean,
ignoreNotBefore: boolean,
subject: string | string[],
clockTolerance: number,
maxAge: string | number,
clockTimestamp: number
}>;

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

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

<P: Encodable>
(payload: P, secretOrPrivateKey: string | Buffer, callback: SignCallback): string;

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

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

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

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

(jwt: string, options: jwt$DecodingOptions & { complete: true }): { header: Object, payload: mixed, signature: string };
}
declare type Payload = Object & {
jti?: string,
iss?: string,
sub?: string,
aud?: string | string[],
exp?: number,
iat?: number,
nbf?: number
}

declare interface jwt$Verify {
(jwt: string, secretOrPrivateKey: string | Buffer): mixed;
declare type Token = {
header: { typ: 'JWT', alg: Algorithm },
payload: Payload,
signature?: string,
}

(jwt: string, secretOrPrivateKey: string | Buffer, options: jwt$VerifyOptions | jwt$Callback): mixed;
declare interface Decode {
(jwt: string): Payload;

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

(jwt: string, secretOrPrivateKey: jwt$Key, options: jwt$VerifyOptionsWithAlgorithm): mixed;
(jwt: string, options: DecodingOptions & { complete: true }): Token;
}

(jwt: string, secretOrPrivateKey: jwt$Key, options: jwt$VerifyOptionsWithAlgorithm, callback: jwt$Callback): mixed;
}
declare interface Verify {
(jwt: string, secretOrPrivateKey: string | Buffer): Payload;

declare class jwt$TokenExpiredError extends Error {
}
(jwt: string, secretOrPrivateKey: string | Buffer, options: VerifyOptions | VerifyCallback): Payload;

declare class jwt$WebTokenError extends Error {
}
(jwt: string, secretOrPrivateKey: string | Buffer, options: VerifyOptions, callback: VerifyCallback): Payload;

(jwt: string, secretOrPrivateKey: Key, options: VerifyOptionsWithAlgorithm): Payload;

(jwt: string, secretOrPrivateKey: Key, options: VerifyOptionsWithAlgorithm, callback: VerifyCallback): Payload;
}

declare class TokenExpiredError extends Error {
}

declare class jwt$NotBeforeError extends Error {
declare class WebTokenError extends Error {
}

declare class NotBeforeError extends Error {
}

declare module.exports: {
sign: Sign,
decode: Decode,
verify: Verify,
JsonWebTokenError: Class<WebTokenError>,
NotBeforeError: Class<NotBeforeError>,
TokenExpiredError: Class<TokenExpiredError>
}
}
13 changes: 6 additions & 7 deletions definitions/npm/jsonwebtoken_v8.2.x/test_jsonwebtoken_v8.2.x.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

const jwt = require('jsonwebtoken');
import jwt from 'jsonwebtoken'

const payload = {
foo: 'bar'
Expand All @@ -12,9 +12,9 @@ jwt.sign(payload);
jwt.sign(payload, 'secret');

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

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

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

Expand All @@ -23,8 +23,7 @@ jwt.sign(payload, Buffer.from('secret'), {
});

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

// $ExpectError
Expand All @@ -47,11 +46,11 @@ 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.sign(payload, { key: 'foo', passphrase: 'bar' }, { algorithm: 'ES512' }, (err: Error) => undefined);

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

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

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

0 comments on commit 10d0995

Please sign in to comment.