Coder Social home page Coder Social logo

passport-strategy's Introduction

passport-strategy

Build Coverage Quality Dependencies Tips

An abstract class implementing Passport's strategy API.

Install

$ npm install passport-strategy

Usage

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.

Subclass Strategy

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);

Set instance attributes

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 Authentication

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.

Examples

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();
    }
}

Related Modules

Tests

$ npm install
$ npm test

Credits

License

The MIT License

Copyright (c) 2011-2014 Jared Hanson <http://jaredhanson.net/>

passport-strategy's People

Contributors

jaredhanson avatar srhoulam avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.