npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

passpack

v0.1.1

Published

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

Downloads

25

Readme

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