passpack

0.1.1 • Public • Published

Passpack

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

Overview

Passpack 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 passpack

Or via git:

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

Usage

Passpack 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 passpack.

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.

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

_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.

passpack._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 Passpack object, optionally providing the _getConfig and _createInstance functions in an object.

new Passpack([options]);
// basic instantiation
var passpack = new Passpack();
 
// instantiation with functions
var passpack = new Passpack({
  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 passpack-related piece of middleware in your application.

passpack.attach();
app.use(passpack.attach());

middleware

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

passpack.middleware(name, [arg1, [arg2, ...]]);
app.use(passpack.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 passpack collection.

passpack.on("added", onAdded);
passpack.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 passpack passport passport-http
 
var express = require("express"),
    Passpack = require("passpack"),
    Passport = require("passport").Passport,
    BasicStrategy = require("passport-http").BasicStrategy;
 
var passpack = new Passpack();
 
passpack._getConfig = function _getConfig(req, cb) {
  return cb(null, req.host, {
    realm: req.host,
  });
};
 
passpack._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(passpack.attach());
app.use(passpack.middleware("initialize"));
app.use(passpack.middleware("session"));
app.use(app.router);
 
app.get("/login", passpack.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

Readme

Keywords

none

Package Sidebar

Install

npm i passpack

Weekly Downloads

4

Version

0.1.1

License

BSD

Last publish

Collaborators

  • deoxxa