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

sails-hook-jwt-auth

v1.0.3

Published

json web token authentication system

Downloads

7

Readme

sails-hook-jwt-auth

Hook that provides jwt authentication sails-compatible scheme, such as policies, routes, controllers, services.

Installation

Within a Sails App structure:

npm install --save sails-hook-jwt-auth

Service

This module globally expose a service which integrates with the jsonwebtoken (https://github.com/auth0/node-jsonwebtoken) and provide the interface to apply the jwt specification (http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html).

var jwt = require('jsonwebtoken');

module.exports.issueToken = function(payload, options) {
  var token = jwt.sign(payload, process.env.TOKEN_SECRET || sails.config.jwt.token_secret, options);
  return token;
};

module.exports.verifyToken = function(token, callback) {
  return jwt.verify(token, process.env.TOKEN_SECRET || sails.config.jwt.token_secret, {}, callback);
};

Policy

The authToken.js policy is just like any other Sails policy and can be applied as such. It's responsible for parsing the token from the incoming request and validating it's state.

module.exports = function(req, res, next) {
  var token;

  if ( req.headers && req.headers.authorization ) {
    var parts = req.headers.authorization.split(' ');
    if ( parts.length == 2 ) {
      var scheme = parts[0],
        credentials = parts[1];

      if ( /^Bearer$/i.test(scheme) ) {
        token = credentials;
      }
    } else {
      return res.json( 401, { err: { status: 'danger', message: res.i18n('auth.policy.wrongFormat') }});
    }
  } else if ( req.param('token') ) {
    token = req.param('token');
    // We delete the token from param to not mess with blueprints
    delete req.query.token;
  } else {
    return res.json( 401, { err: { status: 'danger', message: res.i18n('auth.policy.noAuthorizationHeaderFound') }});
  }

  TokenAuth.verifyToken(token, function(err, decodedToken) {
    if ( err ) return res.json( 401, { err: { status: 'danger', message: res.i18n('auth.policy.invalidToken'), detail: err }});

    req.token = decodedToken.sub;

    next();
  });
};

Use it as you would use any other sails policy to enable jwt authentication restriction to your Controllers/Actions:

module.exports.policies = {
	...
  'UserController': ['authToken'],
  ...
};

Model

This hook sets up a basic User model with some defaults attributes required to implement the jwt authentication scheme.

var bcrypt = require('bcrypt');
module.exports = {

  attributes: {
  	email: {
  		type: 'email',
  		required: true,
  		unique: true
  	},

  	password: {
  		type: 'string',
      required: true,
  	},

    active: {
      type: 'boolean',
      defaultsTo: true
    },

    isPasswordValid: function (password, cb) {
      bcrypt.compare(password, this.password, cb);
    }
  }
};

The User model can be extended with any property you want by defining it in your own Sails project.

Routes

These are the routes provided by this hook:

module.exports.routes = {
	'post /login' : 'AuthController.login',
	'post /signup' : 'AuthController.signup',
	'get /activate/:token' : 'AuthController.activate'
};

/login

The POST request to this route /login must be sent with these body parameters:

{
	email: '[email protected]',
	password: 'test123'
}

The POST response:

{ 
	user: user,
	token: jwt_token
}

Make sure that you provide the acquired token in every request made to the protected endpoints, as query parameter token or as an HTTP request Authorization header Bearer TOKEN_VALUE.

/signup

The POST request to this route /signup must be sent with these body parameters:

{
	email: '[email protected]',
	password: 'test123',
	confirmPassword: 'test123',
}

The POST response:

If account activation feature is disabled, the reponse will be the same as the POST /login. If it's enabled you will set the response as you want in the sendAccountActivationEmail function.

/activate/:token

Account Activation

This feature is off by default and to enable it you must override the requireAccountActivation configuration and implement the function sendAccountActivationEmail:

module.exports.jwt = {
	requireAccountActivation: true,
	sendAccountActivationEmail: function (res, user, link){
		sails.log.info('An email must be sent to this email: ', user.email, ' with this activation link: ', link);
		return res.json(200, { success: 'Email has been sent to user!' });
	}
}