mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 06:47:03 +00:00
Fix interface exports and add more tests
This commit is contained in:
Vendored
+135
-135
@@ -9,150 +9,147 @@
|
||||
import express = require("express");
|
||||
import jsonwebtoken = require("jsonwebtoken");
|
||||
|
||||
declare global {
|
||||
namespace Express {
|
||||
interface Request {
|
||||
jwt: JWT;
|
||||
}
|
||||
export interface JWT {
|
||||
/**
|
||||
* Indicates if the JWT is expired. valid will always be false if this is true.
|
||||
*/
|
||||
expired: boolean;
|
||||
|
||||
interface JWT {
|
||||
/**
|
||||
* Indicates if the JWT is expired. valid will always be false if this is true.
|
||||
*/
|
||||
expired: boolean;
|
||||
/**
|
||||
* The jwt-express options.
|
||||
*/
|
||||
options: JWTExpressOptions;
|
||||
|
||||
/**
|
||||
* The jwt-express options.
|
||||
*/
|
||||
options: JWTExpressOptions;
|
||||
/**
|
||||
* The payload of the JWT (must be an object). jwt-express will add a stales key-value pair to the payload for stale.
|
||||
*/
|
||||
payload: any;
|
||||
|
||||
/**
|
||||
* The payload of the JWT (must be an object). jwt-express will add a stales key-value pair to the payload for stale.
|
||||
*/
|
||||
payload: any;
|
||||
/**
|
||||
* The secret used to sign /verify the JWT.
|
||||
*/
|
||||
secret: string;
|
||||
|
||||
/**
|
||||
* The secret used to sign /verify the JWT.
|
||||
*/
|
||||
secret: string;
|
||||
/**
|
||||
* Indicates if the JWT is stale. The default timeout before a JWT is considered stale is 15 minutes.
|
||||
*/
|
||||
stale: boolean;
|
||||
|
||||
/**
|
||||
* Indicates if the JWT is stale. The default timeout before a JWT is considered stale is 15 minutes.
|
||||
*/
|
||||
stale: boolean;
|
||||
/**
|
||||
* The signed token of the JWT.
|
||||
*/
|
||||
token: string;
|
||||
|
||||
/**
|
||||
* The signed token of the JWT.
|
||||
*/
|
||||
token: string;
|
||||
/**
|
||||
* Indicates if this JWT is valid. This means that the payload hasn't been tampered with and that the JWT hasn't expired yet.
|
||||
*/
|
||||
valid: boolean;
|
||||
|
||||
/**
|
||||
* Indicates if this JWT is valid. This means that the payload hasn't been tampered with and that the JWT hasn't expired yet.
|
||||
*/
|
||||
valid: boolean;
|
||||
/**
|
||||
* Resigns this JWT Objects's payload.
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
resign(): JWT;
|
||||
|
||||
/**
|
||||
* Resigns this JWT Objects's payload.
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
resign(): JWT;
|
||||
/**
|
||||
* Calls the revoke function defined in the jwt.init() options with this JWT Object as the first parameter.
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
revoke(): JWT;
|
||||
|
||||
/**
|
||||
* Calls the revoke function defined in the jwt.init() options with this JWT Object as the first parameter.
|
||||
*
|
||||
* @return this
|
||||
*/
|
||||
revoke(): JWT;
|
||||
/**
|
||||
* Generates a signed token from the payload.
|
||||
*
|
||||
* @param payload The payload of the JWT (must be an object).
|
||||
* @return this
|
||||
*/
|
||||
sign(payload: any): JWT;
|
||||
|
||||
/**
|
||||
* Generates a signed token from the payload.
|
||||
*
|
||||
* @param payload The payload of the JWT (must be an object).
|
||||
* @return this
|
||||
*/
|
||||
sign(payload: any): JWT;
|
||||
/**
|
||||
* Stores this JWT in the cookie (if configured to use cookies).
|
||||
*
|
||||
* @param res The response in which to store the JWT.
|
||||
* @return this
|
||||
*/
|
||||
store(res: express.Response): JWT;
|
||||
|
||||
/**
|
||||
* Stores this JWT in the cookie (if configured to use cookies).
|
||||
*
|
||||
* @param res The response in which to store the JWT.
|
||||
* @return this
|
||||
*/
|
||||
store(res: express.Response): JWT;
|
||||
/**
|
||||
* This function is called when the jwt is passed through JSON.stringify.
|
||||
* We don't want the secret or options to be stringified.
|
||||
*
|
||||
* @return The JSON form of the object.
|
||||
*/
|
||||
toJSON(): any;
|
||||
|
||||
/**
|
||||
* This function is called when the jwt is passed through JSON.stringify.
|
||||
* We don't want the secret or options to be stringified.
|
||||
*
|
||||
* @return The JSON form of the object.
|
||||
*/
|
||||
toJSON(): any;
|
||||
|
||||
/**
|
||||
* Verify the token and load the info into this JWT.
|
||||
*
|
||||
* @param token The signed token to verify.
|
||||
* @return this
|
||||
*/
|
||||
verify(token: string): JWT;
|
||||
}
|
||||
|
||||
interface JWTExpressOptions {
|
||||
/**
|
||||
* The name of the cookie (default: 'jwt-express')
|
||||
*/
|
||||
cookie?: string;
|
||||
|
||||
/**
|
||||
* Options to use when storing the cookie (default: {httpOnly: true})
|
||||
*/
|
||||
cookieOptions?: express.CookieOptions;
|
||||
|
||||
/**
|
||||
* If true, will use cookies, otherwise will use the Authorization header (default: true)
|
||||
*/
|
||||
cookies?: boolean;
|
||||
|
||||
/**
|
||||
* Indicates if the JWT should be refreshed and stored every request (default: true)
|
||||
*/
|
||||
refresh?: boolean;
|
||||
|
||||
/**
|
||||
* The property of req to populate (default: 'jwt')
|
||||
*/
|
||||
reqProperty?: string;
|
||||
|
||||
/**
|
||||
* jwt.revoke() will call this function (default: function(jwt) {})
|
||||
*/
|
||||
revoke?: (jwt: JWT) => void;
|
||||
|
||||
/**
|
||||
* Options to use when signing the JWT (default: {})
|
||||
*/
|
||||
signOptions?: jsonwebtoken.SignOptions;
|
||||
|
||||
/**
|
||||
* Milliseconds when the jwt will go stale (default: 900000 (15 minutes))
|
||||
*/
|
||||
stales?: number;
|
||||
|
||||
/**
|
||||
* Additional verification. Must return a boolean (default: function(jwt) {return true})
|
||||
*/
|
||||
verify?: (jwt: JWT) => boolean;
|
||||
|
||||
/**
|
||||
* Options to use when verifying the JWT (default: {})
|
||||
*/
|
||||
verifyOptions?: jsonwebtoken.VerifyOptions;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Verify the token and load the info into this JWT.
|
||||
*
|
||||
* @param token The signed token to verify.
|
||||
* @return this
|
||||
*/
|
||||
verify(token: string): JWT;
|
||||
}
|
||||
|
||||
export let options: Express.JWTExpressOptions;
|
||||
export interface JWTExpressOptions {
|
||||
/**
|
||||
* The name of the cookie (default: 'jwt-express')
|
||||
*/
|
||||
cookie?: string;
|
||||
|
||||
/**
|
||||
* Options to use when storing the cookie (default: {httpOnly: true})
|
||||
*/
|
||||
cookieOptions?: express.CookieOptions;
|
||||
|
||||
/**
|
||||
* If true, will use cookies, otherwise will use the Authorization header (default: true)
|
||||
*/
|
||||
cookies?: boolean;
|
||||
|
||||
/**
|
||||
* Indicates if the JWT should be refreshed and stored every request (default: true)
|
||||
*/
|
||||
refresh?: boolean;
|
||||
|
||||
/**
|
||||
* The property of req to populate (default: 'jwt')
|
||||
*/
|
||||
reqProperty?: string;
|
||||
|
||||
/**
|
||||
* jwt.revoke() will call this function (default: function(jwt) {})
|
||||
*/
|
||||
revoke?: (jwt: JWT) => void;
|
||||
|
||||
/**
|
||||
* Options to use when signing the JWT (default: {})
|
||||
*/
|
||||
signOptions?: jsonwebtoken.SignOptions;
|
||||
|
||||
/**
|
||||
* Milliseconds when the jwt will go stale (default: 900000 (15 minutes))
|
||||
*/
|
||||
stales?: number;
|
||||
|
||||
/**
|
||||
* Additional verification. Must return a boolean (default: function(jwt) {return true})
|
||||
*/
|
||||
verify?: (jwt: JWT) => boolean;
|
||||
|
||||
/**
|
||||
* Options to use when verifying the JWT (default: {})
|
||||
*/
|
||||
verifyOptions?: jsonwebtoken.VerifyOptions;
|
||||
}
|
||||
|
||||
export interface JWTExpressError extends Error {
|
||||
name: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export let options: JWTExpressOptions;
|
||||
|
||||
/**
|
||||
* Returns a middleware function that ensures a JWT is valid and fresh. Useful to protect
|
||||
@@ -179,7 +176,7 @@ export function clear(): express.RequestHandler;
|
||||
* @param payload The payload of the JWT.
|
||||
* @return JWT
|
||||
*/
|
||||
export function create(secret: string | ((payload: any) => string), payload: any): Express.JWT;
|
||||
export function create(secret: string | ((payload: any) => string), payload: any): JWT;
|
||||
|
||||
/**
|
||||
* The jwt.init() function returns a middleware function for Express so it must be called inside app.use().
|
||||
@@ -194,7 +191,7 @@ export function create(secret: string | ((payload: any) => string), payload: any
|
||||
* @param options The options of jwt-express.
|
||||
* @return Express middleware
|
||||
*/
|
||||
export function init(secret: string | ((req: express.Request) => string), options?: Express.JWTExpressOptions): express.RequestHandler;
|
||||
export function init(secret: string | ((req: express.Request) => string), options?: JWTExpressOptions): express.RequestHandler;
|
||||
|
||||
/**
|
||||
* Returns a middleware function that requires the payload to contain / match certain data.
|
||||
@@ -216,7 +213,10 @@ export function require(key: string, operator?: string, value?: any): express.Re
|
||||
*/
|
||||
export function valid(): express.RequestHandler;
|
||||
|
||||
export interface JWTExpressError extends Error {
|
||||
name: string;
|
||||
message: string;
|
||||
declare global {
|
||||
namespace Express {
|
||||
interface Request {
|
||||
jwt: JWT;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import express = require("express");
|
||||
import exp = require("express");
|
||||
import jwt = require("jwt-express");
|
||||
|
||||
const app = express();
|
||||
const app = exp();
|
||||
|
||||
app.use(jwt.active());
|
||||
app.use(jwt.clear());
|
||||
@@ -17,12 +17,12 @@ app.use(jwt.init("Secret", {
|
||||
cookies: true,
|
||||
refresh: true,
|
||||
reqProperty: "jwt",
|
||||
revoke: (revokeJWT) => { jwtObj = revokeJWT; },
|
||||
revoke: (revokeJWT: jwt.JWT) => { jwtObj = revokeJWT; },
|
||||
signOptions: {
|
||||
expiresIn: "1h"
|
||||
},
|
||||
stales: 1000,
|
||||
verify: (verifyJWT) => { jwtObj = verifyJWT; return true; },
|
||||
verify: (verifyJWT: jwt.JWT) => { jwtObj = verifyJWT; return true; },
|
||||
verifyOptions: {
|
||||
ignoreExpiration: true
|
||||
}
|
||||
@@ -31,11 +31,14 @@ app.use(jwt.require("key"));
|
||||
app.use(jwt.require("key", "==", "value"));
|
||||
app.use(jwt.valid());
|
||||
|
||||
let resObj: express.Response = <any> {};
|
||||
app.use((err: jwt.JWTExpressError, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
let resObj: exp.Response = <any> {};
|
||||
app.use((err: jwt.JWTExpressError, req: exp.Request, res: exp.Response, next: exp.NextFunction) => {
|
||||
err.message.startsWith("");
|
||||
err.name.startsWith("");
|
||||
resObj = res;
|
||||
jwtObj = req.jwt;
|
||||
req.jwt.payload;
|
||||
req.hostname.startsWith("");
|
||||
});
|
||||
|
||||
jwtObj.expired.valueOf();
|
||||
@@ -52,3 +55,9 @@ jwtObj = jwtObj.sign(jwtObj.payload);
|
||||
jwtObj = jwtObj.store(resObj);
|
||||
jwtObj = jwtObj.verify(jwtObj.token);
|
||||
jwtObj.toJSON();
|
||||
|
||||
app.get("", (req: exp.Request, res: exp.Response, next: exp.NextFunction) => {
|
||||
jwtObj = req.jwt;
|
||||
req.jwt.payload;
|
||||
req.hostname.startsWith("");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user