mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-08 08:04:19 +08:00
Merge pull request #2821 from SomaticIT/add-jsonwebtoken
Add jsonwebtoken node module definitions
This commit is contained in:
@@ -213,6 +213,7 @@ All definitions files include a header with the author and editors, so at some p
|
||||
* [JSDeferred](http://cho45.stfuawsc.com/jsdeferred/) (by [Daisuke Mino](https://github.com/minodisk))
|
||||
* [JSONEditorOnline](https://github.com/josdejong/jsoneditoronline) (by [Vincent Bortone](https://github.com/vbortone/))
|
||||
* [JSON-Pointer](https://www.npmjs.org/package/json-pointer) (by [Bart van der Schoor](https://github.com/Bartvds))
|
||||
* [jsonwebtoken](https://github.com/auth0/node-jsonwebtoken) (by [Maxime LUCE](https://github.com/SomaticIT))
|
||||
* [JsRender](http://www.jsviews.com/#jsrender) (by [Kensuke MATSUZAKI](https://github.com/zakki))
|
||||
* [jStorage](http://www.jstorage.info/) (by [Danil Flores](https://github.com/dflor003/))
|
||||
* [jsTree](http://www.jstree.com/) (by [Adam Pluciński](https://github.com/adaskothebeast))
|
||||
|
||||
64
jsonwebtoken/jsonwebtoken-tests.ts
Normal file
64
jsonwebtoken/jsonwebtoken-tests.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Test suite created by Maxime LUCE <https://github.com/SomaticIT>
|
||||
*
|
||||
* Created by using code samples from https://github.com/auth0/node-jsonwebtoken.
|
||||
*/
|
||||
|
||||
/// <reference path="../node/node.d.ts" />
|
||||
/// <reference path="jsonwebtoken.d.ts" />
|
||||
|
||||
import jwt = require("jsonwebtoken");
|
||||
import fs = require("fs");
|
||||
|
||||
var token: string;
|
||||
var cert: Buffer;
|
||||
|
||||
/**
|
||||
* jwt.sign
|
||||
* https://github.com/auth0/node-jsonwebtoken#usage
|
||||
*/
|
||||
// sign with default (HMAC SHA256)
|
||||
token = jwt.sign({ foo: 'bar' }, 'shhhhh');
|
||||
|
||||
// sign with RSA SHA256
|
||||
cert = fs.readFileSync('private.key'); // get private key
|
||||
token = jwt.sign({ foo: 'bar' }, cert, { algorithm: 'RS256'});
|
||||
|
||||
/**
|
||||
* jwt.verify
|
||||
* https://github.com/auth0/node-jsonwebtoken#jwtverifytoken-secretorpublickey-options-callback
|
||||
*/
|
||||
// verify a token symmetric
|
||||
jwt.verify(token, 'shhhhh', function(err, decoded) {
|
||||
console.log(decoded.foo) // bar
|
||||
});
|
||||
|
||||
// invalid token
|
||||
jwt.verify(token, 'wrong-secret', function(err, decoded) {
|
||||
// err
|
||||
// decoded undefined
|
||||
});
|
||||
|
||||
// verify a token asymmetric
|
||||
cert = fs.readFileSync('public.pem'); // get public key
|
||||
jwt.verify(token, cert, function(err, decoded) {
|
||||
console.log(decoded.foo) // bar
|
||||
});
|
||||
|
||||
// verify audience
|
||||
cert = fs.readFileSync('public.pem'); // get public key
|
||||
jwt.verify(token, cert, { audience: 'urn:foo' }, function(err, decoded) {
|
||||
// if audience mismatch, err == invalid audience
|
||||
});
|
||||
|
||||
// verify issuer
|
||||
cert = fs.readFileSync('public.pem'); // get public key
|
||||
jwt.verify(token, cert, { audience: 'urn:foo', issuer: 'urn:issuer' }, function(err, decoded) {
|
||||
// if issuer mismatch, err == invalid issuer
|
||||
});
|
||||
|
||||
/**
|
||||
* jwt.decode
|
||||
* https://github.com/auth0/node-jsonwebtoken#jwtdecodetoken
|
||||
*/
|
||||
var decoded = jwt.decode(token);
|
||||
79
jsonwebtoken/jsonwebtoken.d.ts
vendored
Normal file
79
jsonwebtoken/jsonwebtoken.d.ts
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
// Type definitions for jsonwebtoken 0.4.0
|
||||
// Project: https://github.com/auth0/node-jsonwebtoken
|
||||
// Definitions by: Maxime LUCE <https://github.com/SomaticIT>
|
||||
// Definitions: https://github.com/borisyankov/DefinitelyTyped
|
||||
|
||||
/// <reference path="../node/node.d.ts" />
|
||||
|
||||
declare module "jsonwebtoken" {
|
||||
|
||||
export interface SignOptions {
|
||||
/**
|
||||
* Signature algorithm. Could be one of these values :
|
||||
* - HS256: HMAC using SHA-256 hash algorithm
|
||||
* - 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;
|
||||
/** @member {number} - Lifetime for the token in minutes */
|
||||
expiresInMinutes?: number;
|
||||
audience?: string;
|
||||
subject?: string;
|
||||
issuer?: string;
|
||||
}
|
||||
|
||||
export interface VerifyOptions {
|
||||
audience?: string;
|
||||
issuer?: string;
|
||||
}
|
||||
|
||||
export interface VerifyCallbak {
|
||||
(err: Error, decoded: any): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sign the given payload into a JSON Web Token string
|
||||
* @param {String|Object|Buffer} payload - Payload to sign, could be an literal, buffer or string
|
||||
* @param {String|Buffer} secretOrPrivateKey - Either the secret for HMAC algorithms, or the PEM encoded private key for RSA and ECDSA.
|
||||
* @param {SignOptions} [options] - Options for the signature
|
||||
* @returns {String} The JSON Web Token string
|
||||
*/
|
||||
export function sign(payload: string, secretOrPrivateKey: string): string;
|
||||
export function sign(payload: string, secretOrPrivateKey: Buffer): string;
|
||||
export function sign(payload: Buffer, secretOrPrivateKey: string): string;
|
||||
export function sign(payload: Buffer, secretOrPrivateKey: Buffer): string;
|
||||
export function sign(payload: Object, secretOrPrivateKey: string): string;
|
||||
export function sign(payload: Object, secretOrPrivateKey: Buffer): string;
|
||||
export function sign(payload: string, secretOrPrivateKey: string, options: SignOptions): string;
|
||||
export function sign(payload: string, secretOrPrivateKey: Buffer, options: SignOptions): string;
|
||||
export function sign(payload: Buffer, secretOrPrivateKey: string, options: SignOptions): string;
|
||||
export function sign(payload: Buffer, secretOrPrivateKey: Buffer, options: SignOptions): string;
|
||||
export function sign(payload: Object, secretOrPrivateKey: string, options: SignOptions): string;
|
||||
export function sign(payload: Object, secretOrPrivateKey: Buffer, options: SignOptions): string;
|
||||
|
||||
/**
|
||||
* 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 {VerifyOptions} [options] - Options for the verification
|
||||
* @param {Function} callback - Callback to get the decoded token on
|
||||
*/
|
||||
function verify(token: string, secretOrPublicKey: string, callback: VerifyCallbak): void;
|
||||
function verify(token: string, secretOrPublicKey: Buffer, callback: VerifyCallbak): void;
|
||||
function verify(token: string, secretOrPublicKey: string, options: VerifyOptions, callback: VerifyCallbak): void;
|
||||
function verify(token: string, secretOrPublicKey: Buffer, options: VerifyOptions, callback: VerifyCallbak): void;
|
||||
|
||||
/**
|
||||
* Returns the decoded payload without verifying if the signature is valid.
|
||||
* @param {String} token - JWT string to decode
|
||||
* @returns {Object} The decoded Token
|
||||
*/
|
||||
function decode(token: string): any;
|
||||
}
|
||||
Reference in New Issue
Block a user