Coder Social home page Coder Social logo

passports's Introduction

Passports

Multi-tenancy (read: virtual hosts) for Passport.JS

Overview

Passports makes things easier if you're running a multi-tenanted application with Passport.JS by abstracting away some of the book keeping involved with instantiating multiple passport instances and choosing which to use for a given request.

Installation

Available via npm:

$ npm install passports

Or via git:

$ npm install git://github.com/deoxxa/passports.git

Usage

Passports provides a framework for applications to define their own passport.js multi-tenanting implementations. It requires you to provide two functions: _getConfig and _createInstance. These functions are called when necessary to do exactly what they sound like they do, allowing you to completely control the configuration and instantiation of the passport objects managed by passports.

You can see an example of how this all fits together below, in the "example" section.

_getConfig

The _getConfig function is used to work out what the configuration parameters are for passport for a given request. It's provided with the request object from express, and is expected to call the callback function when it's done with an error (or null), an ID, and optionally some configuration parameters for _createInstance to use in instantiating the passport object.

passports._getConfig = function _getConfig(req, cb) {
  cb(null, req.hostname, {
    realm: "Please log in to " + req.hostname,
  });
};

_createInstance

The createInstance function is used to actually instantiate a passport object. It's only called when a cached object isn't already available, providing a means of lazy instantiation. It's given some configuration parameters (from _getConfig) and is expected to call the callback function with an error (or null) and the resultant passport instance.

passports._createInstance = function _createInstance(options, cb) {
  var instance = new Passport();

  instance.use("basic", new BasicStrategy(options, function(name, password, done) {
    return done(null, {name: name});
  }));

  instance.serializeUser(function(user, cb) {
    user.realm = options.realm;

    cb(null, JSON.stringify(user));
  });

  instance.deserializeUser(function(id, cb) {
    cb(null, JSON.parse(id));
  });

  cb(null, instance);
};

API

constructor

Constructs a new Passports object, optionally providing the _getConfig and _createInstance functions in an object.

new Passports([options]);
// basic instantiation
var passports = new Passports();

// instantiation with functions
var passports = new Passports({
  getConfig: myGetConfig,
  createInstance: myCreateInstance,
});

Arguments

  • options - an object specifying values for _getConfig and _createInstance

attach

Returns an express/connect-compatible middleware function that attaches the correct passport object to a request. You probably want this as the first passports-related piece of middleware in your application.

passports.attach();
app.use(passports.attach());

middleware

Wraps a passport middleware function so that it'll be called using the correct passport instance, optionally passing some arguments to it.

passports.middleware(name, [arg1, [arg2, ...]]);
app.use(passports.middleware("authenticate", "basic"));

Arguments

  • name - name of the passport middleware function.
  • argN - arguments to be passed to the middleware.

#added

added is an event that's fired with the id of a passport object after it's created and added to the passports collection.

passports.on("added", onAdded);
passports.on("added", function onAdded(id, instance) {
  console.log(id);
});

Parameters

  • id - id of the passport instance.
  • instance - the passport instance itself.

Example

Also see example.js.

// $ npm install express passports passport passport-http

var express = require("express"),
    Passports = require("passports"),
    Passport = require("passport").Passport,
    BasicStrategy = require("passport-http").BasicStrategy;

var passports = new Passports();

passports._getConfig = function _getConfig(req, cb) {
  return cb(null, req.hostname, {
    realm: req.hostname,
  });
};

passports._createInstance = function _createInstance(options, cb) {
  var instance = new Passport();

  instance.use("basic", new BasicStrategy(options, function(name, password, done) {
    return done(null, {name: name});
  }));

  instance.serializeUser(function(user, cb) {
    user.realm = options.realm;

    cb(null, JSON.stringify(user));
  });

  instance.deserializeUser(function(id, cb) {
    cb(null, JSON.parse(id));
  });

  cb(null, instance);
};

var app = express();

app.use(express.logger());
app.use(express.cookieParser());
app.use(express.session({secret: "keyboard cat"}));
app.use(passports.attach());
app.use(passports.middleware("initialize"));
app.use(passports.middleware("session"));
app.use(app.router);

app.get("/login", passports.middleware("authenticate", "basic", {
  successRedirect: "/",
}));

app.get("/", function(req, res, next) {
  if (!req.user) {
    return res.redirect("/login");
  }

  return res.send("hello, " + JSON.stringify(req.user));
});

app.listen(3000, function() {
  console.log("listening");
});

License

3-clause BSD. A copy is included with the source.

Contact

passports's People

Contributors

deoxxa avatar mneil avatar

Watchers

Auris avatar Courtney Couch avatar jlehtinen avatar James Cloos avatar Paul Hughes avatar  avatar Monika avatar

Forkers

cubedro

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.