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

express-simple-auth-token

v1.2.3

Published

JSON web token middleware for express designed to work with Ember Simple Auth Token

Downloads

11

Readme

Express Simple Auth Token

JSON web token middleware for express designed to work with Ember Simple Auth Token.

Usage

var expressSimpleAuthToken = require('express-simple-auth-token');

var tokenMiddleware = expressSimpleAuthToken({
  secret: 'keyboard cat',
  lookup: function (user, callback) {
    users.findOne({ _id: user.id }, callback);
  },
  verify: function (password, user, callback) {
    verifyPassword(password, user.hash, callback);
  }
});

app.use(tokenMiddleware);

Settings

Required settings:

  • secret - for JSON Web Token, you must supply this.
  • lookup - function called to lookup a user when they log in, called with 2 arguments, username and callback. Username is based on identificationField option.
  • verify - function called to verify a user against the supplied password. Called with 3 arguments: password which is derived from the passwordField option, user which is the result of lookup, and a callback which must be called in the form callback(null, true) if verification was successful. This is where you run a key derivation function or similar.

Optional settings:

  • algorithm - for JWT (default: 'HS256')

  • serverTokenEndpoint - path of the endpoint for getting a token. (default: 'api-token-auth')

  • serverTokenRefreshEndpoint - path of the endpoint for getting a token (default: 'api-token-refresh')

  • passwordField - field in the body of the request posted to serverTokenEndpoint which contains the password (default: password)

  • identificationField - field in the body of the request posted to serverTokenEndpoint which contains the method if identifying the user (default: username)

  • tokenPropertyName - field name for the token for use in verification middleware, the request send to serverTokenRefreshEndpoint, and the response from both token endpoints (default: 'token')

  • refreshLeeway - in seconds, this option denotes if you should give a little leeway in the expiration time when refreshing the token. Useful if your client library uses the expiration date as the time it should send the request causing all token refreshes to fail due to the token having expired milliseconds previously. (default: 0)

  • tokenLife - how long should the token last, in minutes (default: 60)

  • authError - what to do when we can't validate a json web token (either because it isn't there or it is invalid). Defaults to

    function (req, res, next, error){
      return res.statusStatus(401);
    }
  • createTokenError - function called when error encountered in request to serverTokenEndpoint. Same default as authError.

  • refreshTokenError - function called when error encountered in request to serverTokenRefreshEndpoint. Same default as authError.

  • createToken - a function you can use if you want to modify the fields passed back in the token. Useful if you want to strip password hashes or lookup other related attributes. Called with 2 arguments: user which is the result of lookup, and callback. Defaults to

    function (user, callback) {
      process.nextTick(function () {
        callback(null, user);
      });
    }
  • refreshLookup - lookup function for use when the token is refreshed. Defaults to

    function (user, callback) {
      if (!user[opts.identificationField]) {
        return process.nextTick(function () {
          callback(new Error('no username in token'));
        });
      }
      var lookup = opts.lookup;
      lookup(user[opts.identificationField], callback);
    }

More Complete Example

var expressSimpleAuthToken = require('express-simple-auth-token');
var crypto = require('crypto');

var HASH_LEN = 64;

app.use(expressSimpleAuthToken({
  secret: crypto.randomBytes(64),
  lookup: function (user, callback) {
    lookupInDataBase(user, callback);
  },
  verify: function (password, user, callback) {
    var hash = user.hash;
    crypto.pbkdf2(password, user.salt, 10000, HASH_LEN, 'sha224', function (err, resp) {
      if (err) {
        return callback(err);
      }
      var hash = user.hash;
      var i = -1;
      var out = 0;
      while (++i < HASH_LEN) {
        out |= hash[i] ^ resp[i];
      }
      callback(null, out);
    });
  },
  createToken: function (user, callback) {
    delete user.hash;
    delete user.salt;
    callback(null, user);
  }
}));