From 994c3dcd66bd8ccbcb8b04fe7aa065fad43e05ce Mon Sep 17 00:00:00 2001 From: Lior Mualem Date: Wed, 9 Jul 2014 10:23:20 +0300 Subject: [PATCH 1/4] Added definition for passport-strategy module. --- CONTRIBUTORS.md | 1 + passport-strategy/passport-strategy.d.ts | 98 ++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 passport-strategy/passport-strategy.d.ts diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 723bb1d2b8..917645686d 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -253,6 +253,7 @@ All definitions files include a header with the author and editors, so at some p * [OpenLayers](https://github.com/openlayers/openlayers) (by [Ilya Bolkhovsky](https://github.com/bolhovsky/)) * [Optimist](https://github.com/substack/node-optimist) (by [Carlos Ballesteros Velasco](https://github.com/soywiz)) * [Passport](http://passportjs.org/) (by [Hiroki Horiuchi](https://github.com/horiuchi/)) +* [passport-strategy](http://passportjs.org/) (by [Lior Mualem](https://github.com/liorm)) * [pathwatcher](http://atom.github.io/node-pathwatcher/) (by [vvakame](https://github.com/vvakame)) * [Parallel.js](https://github.com/adambom/parallel.js) (by [Josh Baldwin](https://github.com/jbaldwin)) * [Parsimmon](https://github.com/jayferd/parsimmon) (by [Bart van der Schoor](https://github.com/Bartvds)) diff --git a/passport-strategy/passport-strategy.d.ts b/passport-strategy/passport-strategy.d.ts new file mode 100644 index 0000000000..da8b9db8e1 --- /dev/null +++ b/passport-strategy/passport-strategy.d.ts @@ -0,0 +1,98 @@ +// Type definitions for Passport v0.2.0 Strategy module. +// Project: http://passportjs.org +// Definitions by: Lior Mualem +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// +/// + +/** + * Using this module, one can easily implement a strategy using typescript by + * inheriting the 'Strategy' class and reimplementing the 'authenticate' method. + */ + +declare module 'passport-strategy' { + + import passport = require('passport'); + import express = require('express'); + + class Strategy implements passport.Strategy { + /** + * Performs authentication for the request. + * Note: Virtual function - re-implement in the strategy. + * @param req The request to authenticate. + * @param options Options passed to the strategy. + */ + authenticate(req: express.Request, options?: any): void; + + // + // Augmented strategy functions. + // These are available only from the 'authenticate' function. + // They are added manually by the passport framework. + // + + /** + * Authenticate `user`, with optional `info`. + * + * Strategies should call this function to successfully authenticate a + * user. `user` should be an object supplied by the application after it + * has been given an opportunity to verify credentials. `info` is an + * optional argument containing additional user information. This is + * useful for third-party authentication strategies to pass profile + * details. + * + * @param {Object} user + * @param {Object} info + * @api public + */ + success(user: any, info: any): void; + + /** + * Fail authentication, with optional `challenge` and `status`, defaulting + * to 401. + * + * Strategies should call this function to fail an authentication attempt. + * + * @param {String} challenge (Can also be an object with 'message' and 'type' fields). + * @param {Number} status + * @api public + */ + fail(challenge: any, status: number): void; + fail(status: number): void; + + /** + * Redirect to `url` with optional `status`, defaulting to 302. + * + * Strategies should call this function to redirect the user (via their + * user agent) to a third-party website for authentication. + * + * @param {String} url + * @param {Number} status + * @api public + */ + redirect(url: string, status?: number): void; + + /** + * Pass without making a success or fail decision. + * + * Under most circumstances, Strategies should not need to call this + * function. It exists primarily to allow previous authentication state + * to be restored, for example from an HTTP session. + * + * @api public + */ + pass(): void; + + /** + * Internal error while performing authentication. + * + * Strategies should call this function when an internal error occurs + * during the process of performing authentication; for example, if the + * user directory is not available. + * + * @param {Error} err + * @api public + */ + error(err: Error): void; + } +} From 2845495b23ba7b33c434cd61a13678c2709022dc Mon Sep 17 00:00:00 2001 From: Lior Mualem Date: Wed, 9 Jul 2014 10:23:35 +0300 Subject: [PATCH 2/4] Added tests for passport-strategy module. --- passport-strategy/passport-strategy-test.ts | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 passport-strategy/passport-strategy-test.ts diff --git a/passport-strategy/passport-strategy-test.ts b/passport-strategy/passport-strategy-test.ts new file mode 100644 index 0000000000..e9c6fd604f --- /dev/null +++ b/passport-strategy/passport-strategy-test.ts @@ -0,0 +1,48 @@ + +import express = require('express'); +import passport = require('passport-strategy'); + + +export class Strategy extends passport.Strategy { + constructor(options: any, verify?: Function) { + if (typeof options == 'function') { + verify = options; + options = {}; + } + if (!verify) { + throw new TypeError('DummyStrategy requires a verify callback'); + } + + super(); + + this.name = 'dummy'; + this._verify = verify; + this._passReqToCallback = options.passReqToCallback; + } + + name: string; + + _verify: Function; + _passReqToCallback: boolean; + + authenticate(req: express.Request, options?: any): void { + options = options || {}; + + // Test fail method. + this.fail({ message: options.missingTokenMessage || 'Missing token' }, 400); + + var self = this; + + function verified(err: Error, user: any, info: any) { + if (err) { + return self.error(err); + } + if (!user) { + return self.fail(info); + } + self.success(user, info); + } + + verified(null, {}, {}); + } +} From 18f6be190d00cd4dbe60b48d81a226de5c69fee9 Mon Sep 17 00:00:00 2001 From: Lior Mualem Date: Wed, 9 Jul 2014 10:31:28 +0300 Subject: [PATCH 3/4] Fixed test syntax errors. --- passport-strategy/passport-strategy.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/passport-strategy/passport-strategy.d.ts b/passport-strategy/passport-strategy.d.ts index da8b9db8e1..2e5d0e3099 100644 --- a/passport-strategy/passport-strategy.d.ts +++ b/passport-strategy/passport-strategy.d.ts @@ -1,4 +1,4 @@ -// Type definitions for Passport v0.2.0 Strategy module. +// Type definitions for Passport Strategy module v0.2.0 // Project: http://passportjs.org // Definitions by: Lior Mualem // Definitions: https://github.com/borisyankov/DefinitelyTyped From fd0af903c6bffde5a69b07376e9d3fc9de67f84e Mon Sep 17 00:00:00 2001 From: Lior Mualem Date: Thu, 10 Jul 2014 09:16:22 +0300 Subject: [PATCH 4/4] Updated passport-strategy website. --- CONTRIBUTORS.md | 2 +- passport-strategy/passport-strategy.d.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 917645686d..10134afd5e 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -253,7 +253,7 @@ All definitions files include a header with the author and editors, so at some p * [OpenLayers](https://github.com/openlayers/openlayers) (by [Ilya Bolkhovsky](https://github.com/bolhovsky/)) * [Optimist](https://github.com/substack/node-optimist) (by [Carlos Ballesteros Velasco](https://github.com/soywiz)) * [Passport](http://passportjs.org/) (by [Hiroki Horiuchi](https://github.com/horiuchi/)) -* [passport-strategy](http://passportjs.org/) (by [Lior Mualem](https://github.com/liorm)) +* [passport-strategy](https://github.com/jaredhanson/passport-strategy) (by [Lior Mualem](https://github.com/liorm)) * [pathwatcher](http://atom.github.io/node-pathwatcher/) (by [vvakame](https://github.com/vvakame)) * [Parallel.js](https://github.com/adambom/parallel.js) (by [Josh Baldwin](https://github.com/jbaldwin)) * [Parsimmon](https://github.com/jayferd/parsimmon) (by [Bart van der Schoor](https://github.com/Bartvds)) diff --git a/passport-strategy/passport-strategy.d.ts b/passport-strategy/passport-strategy.d.ts index 2e5d0e3099..36036f54b2 100644 --- a/passport-strategy/passport-strategy.d.ts +++ b/passport-strategy/passport-strategy.d.ts @@ -1,5 +1,5 @@ // Type definitions for Passport Strategy module v0.2.0 -// Project: http://passportjs.org +// Project: https://github.com/jaredhanson/passport-strategy // Definitions by: Lior Mualem // Definitions: https://github.com/borisyankov/DefinitelyTyped