From f67852cbc81823e9d0178e9cdc89aea4a5e3c40c Mon Sep 17 00:00:00 2001 From: Cyril Schumacher Date: Mon, 7 Dec 2015 18:29:53 +0100 Subject: [PATCH 1/7] Update validator: add "isMACAddress" function. --- validator/validator-tests.ts | 2 ++ validator/validator.d.ts | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/validator/validator-tests.ts b/validator/validator-tests.ts index b7f45d427..c5a55f59c 100644 --- a/validator/validator-tests.ts +++ b/validator/validator-tests.ts @@ -19,6 +19,8 @@ validator.isURL("sample"); validator.isFQDN("sample"); +validator.isMACAddress("sample"); + validator.isIP("sample"); validator.isAlpha("sample"); diff --git a/validator/validator.d.ts b/validator/validator.d.ts index 05a391fa4..2b29efac0 100644 --- a/validator/validator.d.ts +++ b/validator/validator.d.ts @@ -22,7 +22,7 @@ interface IEmailoptions { lowercase?: boolean } -// callback type for #extend +// callback type for #extend interface IExtendCallback { (argv: string): any } @@ -54,6 +54,9 @@ interface IValidatorStatic { // check if the string is a fully qualified domain name (e.g. domain.com). isFQDN(str: string, options?: IFQDNoptions): boolean; + // check if the string is a MAC address. + isMACAddress(str: string): boolean; + // check if the string is an IP (version 4 or 6). isIP(str: string, version?: number): boolean; @@ -177,7 +180,7 @@ interface IValidatorStatic { // remove characters that do not appear in the whitelist. whitelist(input: string, chars: string): string; - // remove characters that appear in the blacklist. + // remove characters that appear in the blacklist. blacklist(input: string, chars: string): string; // canonicalize an email address. From c6a1eb87530f8bbe638b121f4099b33f00dd3bd2 Mon Sep 17 00:00:00 2001 From: Cyril Schumacher Date: Mon, 7 Dec 2015 18:46:54 +0100 Subject: [PATCH 2/7] Add definition "express-brute". --- express-brute/express-brute-tests.ts | 16 ++++ express-brute/express-brute.d.ts | 129 +++++++++++++++++++++++++++ 2 files changed, 145 insertions(+) create mode 100644 express-brute/express-brute-tests.ts create mode 100644 express-brute/express-brute.d.ts diff --git a/express-brute/express-brute-tests.ts b/express-brute/express-brute-tests.ts new file mode 100644 index 000000000..ea0f2f5b4 --- /dev/null +++ b/express-brute/express-brute-tests.ts @@ -0,0 +1,16 @@ +/// + +import express = require("express"); +import ExpressBrute = require("express-brute"); + +var store = new ExpressBrute.MemoryStore(); +store = new ExpressBrute.MemoryStore({ prefix: "prefix" }); +store.set("key", "value", 0, (error: any) => { }); +store.get("key", (error: any, data: Object) => { }); +store.reset("key", (error: any) => { }); + +var app = express(); +var bruteforce = new ExpressBrute(store); +app.post("/auth", bruteforce.prevent, (req, res, next) => { + res.send("Success!"); +}); diff --git a/express-brute/express-brute.d.ts b/express-brute/express-brute.d.ts new file mode 100644 index 000000000..377efcdce --- /dev/null +++ b/express-brute/express-brute.d.ts @@ -0,0 +1,129 @@ +// Type definitions for express-validator 2.9.0 +// Project: https://github.com/AdamPflug/express-brute +// Definitions by: Cyril Schumacher +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "express-brute" { + import express = require("express"); + + /** + * @summary Options for {@link MemoryStore} class. + * @interface + */ + interface MemoryStoreOptions { + /** + * @summary Key prefix. + * @type {string} + */ + prefix: string; + } + + /** + * @summary Options for {@link ExpressBrute#getMiddleware} class. + * @interface + */ + interface ExpressBruteMiddleware { + /** + * @summary Allows you to override the value of failCallback for this middleware. + * @type {Function} + */ + failCallback: Function; + + /** + * @summary Disregard IP address when matching requests if set to true. Defaults to false. + * @type {boolean} + */ + ignoreIP: boolean; + + /** + * @summary Key. + * @type {any} + */ + key: any; + } + + /** + * @summary Middleware. + * @class + */ + class ExpressBrute { + /** + * @summary Constructor. + * @constructor + * @param {any} store The store. + */ + constructor(store: any); + + /** + * @summary Generates middleware that will bounce requests with the same key and IP address that happen faster than the current wait time by calling failCallback. + * @param {Object} options The options. + */ + getMiddleware(options: ExpressBruteMiddleware): express.RequestHandler; + + /** + * @summary Uses the current proxy trust settings to get the current IP from a request object. + * @param {Request} request The HTTP request. + * @return {RequestHandler} The Request handler. + */ + getIPFromRequest(request: express.Request): express.RequestHandler; + + /** + * @summary Middleware that will bounce requests that happen faster than the current wait time by calling failCallback. + * @param {Request} request The HTTP request. + * @param {Response} response The HTTP response. + * @param {Function} next The next middleware. + * @return {RequestHandler} The Request handler. + */ + prevent(request: express.Request, response: express.Response, next: Function): express.RequestHandler; + + /** + * @summary Resets the wait time between requests back to its initial value. + * @param {string} ip The IP address. + * @param {string} key The key. response. + * @param {Function} next The next middleware. + * @return {RequestHandler} The Request handler. + */ + reset(ip: string, key: string, next: Function): express.RequestHandler; + } + + module ExpressBrute { + /** + * @summary In-memory store. + * @class + */ + export class MemoryStore { + /** + * @summary Constructor. + * @constructor + * @param {Object} options The options. + */ + constructor(options?: MemoryStoreOptions); + /** + * @summary Gets key value. + * @param {string} key The key name. + * @param {Function} callbck The callback. + */ + get(key: string, callback: (error: any, data: Object) => void): void; + + /** + * @summary Sets the key value. + * @param {string} key The name. + * @param {string} value The value. + * @param {number} lifetime The lifetime. + * @param {Function} callback The callback. + */ + set(key: string, value: any, lifetime: number, callback: (error: any) => void): void; + + /** + * @summary Deletes the key. + * @param {string} key The name. + * @param {Function} callback The callback. + */ + reset(key: string, callback: (error: any) => void): void; + } + } + + export = ExpressBrute; +} From 41ecb256fe6fe1c59c537d158721ba68d783e16b Mon Sep 17 00:00:00 2001 From: Cyril Schumacher Date: Mon, 7 Dec 2015 19:19:49 +0100 Subject: [PATCH 3/7] Add definition for "express-brute-mongo". --- .../express-brute-mongo-tests.ts | 27 +++++++++++++++++++ express-brute-mongo/express-brute-mongo.d.ts | 22 +++++++++++++++ express-brute/express-brute.d.ts | 2 +- 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 express-brute-mongo/express-brute-mongo-tests.ts create mode 100644 express-brute-mongo/express-brute-mongo.d.ts diff --git a/express-brute-mongo/express-brute-mongo-tests.ts b/express-brute-mongo/express-brute-mongo-tests.ts new file mode 100644 index 000000000..a4512782b --- /dev/null +++ b/express-brute-mongo/express-brute-mongo-tests.ts @@ -0,0 +1,27 @@ +/// +/// +/// + +import express = require("express"); +import ExpressBrute = require("express-brute"); +import MongoStore = require("express-brute-mongo"); +import mongodb = require("mongodb"); +var MongoClient = mongodb.MongoClient; + +var store = new MongoStore(ready => { + MongoClient.connect("mongodb://127.0.0.1:27017/test", (err, db) => { + if (err) { + throw err; + } + + var collection = db.collection("bruteforce-store"); + ready(collection); + }); +}); + +var app = express(); +var bruteforce = new ExpressBrute(store); + +app.post("/auth", bruteforce.prevent, (req, res, next) => { + res.send("Success!"); +}); diff --git a/express-brute-mongo/express-brute-mongo.d.ts b/express-brute-mongo/express-brute-mongo.d.ts new file mode 100644 index 000000000..bc4d5e43d --- /dev/null +++ b/express-brute-mongo/express-brute-mongo.d.ts @@ -0,0 +1,22 @@ +// Type definitions for express-brute-mongo +// Project: https://github.com/auth0/express-brute-mongo +// Definitions by: Cyril Schumacher +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module "express-brute-mongo" { + /** + * @summary MongoDB store adapter. + * @class + */ + export = class MongoStore { + /** + * @summary Constructor. + * @constructor + * @param {Function} getCollection The collection. + * @param {Object} options The otpions. + */ + constructor(getCollection: (collection: any) => void, options?: Object); + } +} diff --git a/express-brute/express-brute.d.ts b/express-brute/express-brute.d.ts index 377efcdce..7242d44dc 100644 --- a/express-brute/express-brute.d.ts +++ b/express-brute/express-brute.d.ts @@ -1,4 +1,4 @@ -// Type definitions for express-validator 2.9.0 +// Type definitions for express-brute // Project: https://github.com/AdamPflug/express-brute // Definitions by: Cyril Schumacher // Definitions: https://github.com/borisyankov/DefinitelyTyped From a961bfca179fd8d16dcdc166bc4c026d11e3fec6 Mon Sep 17 00:00:00 2001 From: Cyril Schumacher Date: Mon, 7 Dec 2015 19:39:31 +0100 Subject: [PATCH 4/7] Update definition for "nodemailer". --- nodemailer/nodemailer-tests.ts | 16 ++++++++++++++-- nodemailer/nodemailer.d.ts | 6 +++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/nodemailer/nodemailer-tests.ts b/nodemailer/nodemailer-tests.ts index 1d99046c0..a991096d5 100644 --- a/nodemailer/nodemailer-tests.ts +++ b/nodemailer/nodemailer-tests.ts @@ -11,6 +11,20 @@ var transporter: nodemailer.Transporter = nodemailer.createTransport({ } }); +// create reusable transporter object using SMTP transport and set default values for mail options. +transporter = nodemailer.createTransport({ + service: 'Gmail', + auth: { + user: 'gmail.user@gmail.com', + pass: 'userpass' + } +}, { + from: 'sender@address', + headers: { + 'My-Awesome-Header': '123' + } +}); + // setup e-mail data with unicode symbols var mailOptions: nodemailer.SendMailOptions = { from: 'Fred Foo ✔ ', // sender address @@ -24,5 +38,3 @@ var mailOptions: nodemailer.SendMailOptions = { transporter.sendMail(mailOptions, (error: Error, info: nodemailer.SentMessageInfo): void => { // nothing }); - - diff --git a/nodemailer/nodemailer.d.ts b/nodemailer/nodemailer.d.ts index e0d1300b0..e7d09f54f 100644 --- a/nodemailer/nodemailer.d.ts +++ b/nodemailer/nodemailer.d.ts @@ -51,13 +51,13 @@ declare module "nodemailer" { /** * Create a direct transporter */ - export function createTransport(options?: directTransport.DirectOptions): Transporter; + export function createTransport(options?: directTransport.DirectOptions, defaults?: Object): Transporter; /** * Create an SMTP transporter */ - export function createTransport(options?: smtpTransport.SmtpOptions): Transporter; + export function createTransport(options?: smtpTransport.SmtpOptions, defaults?: Object): Transporter; /** * Create a transporter from a given implementation */ - export function createTransport(transport: Transport): Transporter; + export function createTransport(transport: Transport, defaults?: Object): Transporter; } From 5111e014788097f739548ef63025bc22371a5ae9 Mon Sep 17 00:00:00 2001 From: Cyril Schumacher Date: Tue, 8 Dec 2015 10:24:22 +0100 Subject: [PATCH 5/7] Add definition for "connect-timeout". --- connect-timeout/connect-timeout-tests.ts | 28 ++++++++++++++++++ connect-timeout/connect-timeout.d.ts | 36 ++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 connect-timeout/connect-timeout-tests.ts create mode 100644 connect-timeout/connect-timeout.d.ts diff --git a/connect-timeout/connect-timeout-tests.ts b/connect-timeout/connect-timeout-tests.ts new file mode 100644 index 000000000..283ee2679 --- /dev/null +++ b/connect-timeout/connect-timeout-tests.ts @@ -0,0 +1,28 @@ +/// +/// +/// +/// + +import express = require("express"); +import timeout = require("connect-timeout"); +import bodyParser = require("body-parser"); +import cookieParser = require("cookie-parser"); + +// example of using this top-level; note the use of haltOnTimedout +// after every middleware; it will stop the request flow on a timeout +var app = express(); +app.use(timeout("5s", { respond: false })); +app.use(bodyParser()); +app.use(haltOnTimedout); +app.use(cookieParser()); +app.use(haltOnTimedout); + +// Add your routes here, etc. + +function haltOnTimedout(req, res, next) { + if (!req.timedout) { + next(); + } +} + +app.listen(3000); diff --git a/connect-timeout/connect-timeout.d.ts b/connect-timeout/connect-timeout.d.ts new file mode 100644 index 000000000..8b7ff2879 --- /dev/null +++ b/connect-timeout/connect-timeout.d.ts @@ -0,0 +1,36 @@ +// Type definitions for connect-timeout +// Project: https://github.com/expressjs/timeout +// Definitions by: Cyril Schumacher +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + +declare module Express { + export interface Request { + /** + * @summary Clears the timeout on the request. + */ + clearTimeout(): void; + + /** + * + * @return {boolean} true if timeout fired; false otherwise. + */ + timedout(event: string, message: string): boolean; + } +} + +declare module "connect-timeout" { + import express = require("express"); + + interface TimeoutOptions extends Object { + /** + * @summary Controls if this module will "respond" in the form of forwarding an error. + * @type {boolean} + */ + respond: boolean; + } + + function timeout(timeout: string, options?: TimeoutOptions): express.RequestHandler; + export = timeout; +} From 784857f638e949d67c80fcd7d7f5ab5de53fb808 Mon Sep 17 00:00:00 2001 From: Cyril Schumacher Date: Tue, 8 Dec 2015 10:26:56 +0100 Subject: [PATCH 6/7] Fix errors. --- connect-timeout/connect-timeout-tests.ts | 2 +- connect-timeout/connect-timeout.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/connect-timeout/connect-timeout-tests.ts b/connect-timeout/connect-timeout-tests.ts index 283ee2679..920c7fdc6 100644 --- a/connect-timeout/connect-timeout-tests.ts +++ b/connect-timeout/connect-timeout-tests.ts @@ -19,7 +19,7 @@ app.use(haltOnTimedout); // Add your routes here, etc. -function haltOnTimedout(req, res, next) { +function haltOnTimedout(req: express.Request, res: express.Response, next: Function) { if (!req.timedout) { next(); } diff --git a/connect-timeout/connect-timeout.d.ts b/connect-timeout/connect-timeout.d.ts index 8b7ff2879..8494a3afb 100644 --- a/connect-timeout/connect-timeout.d.ts +++ b/connect-timeout/connect-timeout.d.ts @@ -1,6 +1,6 @@ // Type definitions for connect-timeout // Project: https://github.com/expressjs/timeout -// Definitions by: Cyril Schumacher +// Definitions by: Cyril Schumacher // Definitions: https://github.com/borisyankov/DefinitelyTyped /// From b8c618001c9769b653da68e2f8b7d279f12b0366 Mon Sep 17 00:00:00 2001 From: Cyril Schumacher Date: Wed, 9 Dec 2015 10:42:31 +0100 Subject: [PATCH 7/7] Update definition for "express-validator": add "isMACAddress" function. --- express-validator/express-validator.d.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/express-validator/express-validator.d.ts b/express-validator/express-validator.d.ts index 428c90afd..78073231b 100644 --- a/express-validator/express-validator.d.ts +++ b/express-validator/express-validator.d.ts @@ -66,12 +66,14 @@ declare module ExpressValidator { * Accepts http, https, ftp */ isUrl(): Validator; + /** * Combines isIPv4 and isIPv6 */ isIP(): Validator; isIPv4(): Validator; isIPv6(): Validator; + isMACAddress(): Validator; isAlpha(): Validator; isAlphanumeric(): Validator; isNumeric(): Validator;