An abstract class implementing Passport's strategy API.
$ npm install passport-strategy
This module exports an abstract Strategy
class that is intended to be
subclassed when implementing concrete authentication strategies. Once
implemented, such strategies can be used by applications that utilize Passport
middleware for authentication.
Create a new CustomStrategy
constructor which inherits from Strategy
:
var util = require('util')
, Strategy = require('passport-strategy');
function CustomStrategy(...) {
Strategy.call(this);
}
util.inherits(CustomStrategy, Strategy);
Passport will identify mounted strategies by the instance's name
attribute,
so be sure to set one in the constructor:
var util = require('util')
, Strategy = require('passport-strategy');
function CustomStrategy(...) {
Strategy.call(this);
this.name = 'custom'; // set instance name
}
util.inherits(CustomStrategy, Strategy);
Later, when a user calls passport.authenticate
to acquire
the authentication middleware that employs this strategy, the value
of this name
attribute is what must be passed in as the first argument:
var authMiddleware = passport.authenticate('custom');
Implement the authenticate
method, performing the necessary operations required by the
authentication scheme or protocol being implemented.
CustomStrategy.prototype.authenticate = function(req, options) {
// TODO: authenticate request
}
Upon mounting a strategy instance on passport, it will be augmented with several methods.
These methods are essential to writing a working authenticate
method:
error(err)
, indicates to passport that an error occurred in authenticating the request.fail(options, statusCode)
, indicates to passport that the request failed authentication.success(userObj)
, indicates to passport that the request was successfully authenticated and provides the user object with which to associate the new authenticated session.
Since the strategy instance is augmented with these methods, they can be accessed as
properties of this
within the authenticate
method.
AlwaysSucceedStrategy.prototype.authenticate = function(req, options) {
this.success({
username : 'bogus',
userId : NaN
});
}
AlwaysFailStrategy.prototype.authenticate = function(req, options) {
this.fail({
message : "working as intended"
}, 200);
}
AlwaysErrorStrategy.prototype.authenticate = function(req, options) {
var err = new Error("Strategy: working as intended")
this.error(err);
}
CoinFlipStrategy.prototype.authenticate = function(req, options) {
if(Math.random() < 0.5) {
this.success({
username : "George Washington",
userId : 25
});
} else {
this.fail();
}
}
- chai-passport-strategy — helpers for testing strategies with the Chai assertion library
$ npm install
$ npm test
Copyright (c) 2011-2014 Jared Hanson <http://jaredhanson.net/>