diff --git a/types/jwt-then/index.d.ts b/types/jwt-then/index.d.ts new file mode 100644 index 0000000000..9a957ff4d2 --- /dev/null +++ b/types/jwt-then/index.d.ts @@ -0,0 +1,63 @@ +// Type definitions for jwt-then 1.0 +// Project: https://github.com/fl0w/jwt-then#readme +// Definitions by: Definitions by: Max Uetrecht +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// + +export type Secret = string | Buffer | { key: string; passphrase: string }; + +export interface SignOptions { + /** + * Signature algorithm. Could be one of these values : + * - HS256: HMAC using SHA-256 hash algorithm (default) + * - HS384: HMAC using SHA-384 hash algorithm + * - HS512: HMAC using SHA-512 hash algorithm + * - RS256: RSASSA using SHA-256 hash algorithm + * - RS384: RSASSA using SHA-384 hash algorithm + * - RS512: RSASSA using SHA-512 hash algorithm + * - ES256: ECDSA using P-256 curve and SHA-256 hash algorithm + * - ES384: ECDSA using P-384 curve and SHA-384 hash algorithm + * - ES512: ECDSA using P-521 curve and SHA-512 hash algorithm + * - none: No digital signature or MAC value included + */ + algorithm?: string; + keyid?: string; + /** expressed in seconds or a string describing a time span [zeit/ms](https://github.com/zeit/ms.js). Eg: 60, "2 days", "10h", "7d" */ + expiresIn?: string | number; + /** expressed in seconds or a string describing a time span [zeit/ms](https://github.com/zeit/ms.js). Eg: 60, "2 days", "10h", "7d" */ + notBefore?: string | number; + audience?: string | string[]; + subject?: string; + issuer?: string; + jwtid?: string; + noTimestamp?: boolean; + header?: object; + encoding?: string; +} + +/** + * Sign the given payload into a JSON Web Token string + * @param payload - Payload to sign, could be an literal, buffer or string + * @param secretOrPrivateKey - Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA. + * @param [options] - Options for the signature + * @returns A proiose containing the JSON Web Token string + */ +export function sign( + payload: string | Buffer | object, + secretOrPrivateKey: Secret, + options?: SignOptions, +): Promise; + +/** + * Verify given token using a secret or a public key to get a decoded token + * @param token - JWT string to verify + * @param secretOrPublicKey - Either the secret for HMAC algorithms, or the PEM encoded public key for RSA and ECDSA. + * @param [options] - Options for the verification + * @returns A promise containing either an error or the decoded JSON Web Token string + */ +export function verify( + token: string, + secretOrPublicKey: string | Buffer, +): Promise; diff --git a/types/jwt-then/jwt-then-tests.ts b/types/jwt-then/jwt-then-tests.ts new file mode 100644 index 0000000000..d9ad3812ae --- /dev/null +++ b/types/jwt-then/jwt-then-tests.ts @@ -0,0 +1,11 @@ +import jwt = require("jwt-then"); +import fs = require("fs"); + +const testObject = { foo: "bar" }; + +(async () => { + const token = await jwt.sign(testObject, "foobar"); + const verified = await jwt.verify(token, "foobar"); + + console.log(token, verified); +})(); diff --git a/types/jwt-then/tsconfig.json b/types/jwt-then/tsconfig.json new file mode 100644 index 0000000000..aa5eb794cf --- /dev/null +++ b/types/jwt-then/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "jwt-then-tests.ts" + ] +} diff --git a/types/jwt-then/tslint.json b/types/jwt-then/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/jwt-then/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }