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

travelogue-hapi4

v2.0.0

Published

hapi passport.js integration helper

Downloads

2

Readme

travelogue Logo

Passport.js integration for hapi

Build Status

Travelogue is a Hapi plugin that provides modular and unobtrusive authentication to Hapi through Passport. Travelogue supports almost every Passport strategy including Facebook OAuth, Google OpenID, and many others listed here.

Install

$ npm install travelogue yar passport

Usage

Travelogue has been designed to integrate consistently with Passport APIs - allowing developers who have used Passport before to use Travelogue with very few modifications. Thus, most Travelogue apps will follow the same general format outlined below:

var config = {
    hostname: 'localhost',
    port: 8000,
    urls: {
        failureRedirect: '/login',
        successRedirect: '/'
    },
    facebook: {
        clientID: "...",
        clientSecret: "...",
        callbackURL: "http://localhost:8000/auth/facebook/callback"
    }
};

var plugins = {
    yar: {
        cookieOptions: {
            password: 'worldofwalmart', // cookie secret
            isSecure: false // required for non-https applications
        }
    },
    travelogue: config
};

var server = new Hapi.Server(config.hostname, config.port);
server.pack.require(plugins, function (err) { 

    if (err) {
        throw err;
    }
});

server.auth.strategy('passport', 'passport');

var Passport = server.plugins.travelogue.passport;

// Follow normal Passport rules to add Strategies
Passport.use(/* Strategy Goes Here */);
Passport.serializeUser(ser);
Passport.deserializeUser(deser);

// Add your routes
server.route(routeOne);
// ... 

server.start(function () {

    console.log('server started on port: ', server.info.port);
});

Note: Be careful with setting strategy callbackURL's. If you are testing using localhost:{port}, make sure this matches the callbackURL exactly AND the designated callback URL in your third-party application settings.

Known Broken/Unsupported Strategies

Check the GitHub Issues area for exisiting issues and to report an issue.

Route Handling

There are three common categories of third-party authentication route handling:

  • External Authentication
  • Internal Authentication
  • Authorization

External Authentication allows the user to authenticate themselves via a third-party service like Facebook or Twitter.

Internal Authentication returns the credentials from External Authentication for verification on the server and ultimately "logs" the user on (saves their session for subsequent visits).

Authorization checks whether or not the user logged-on is allowed to access a specific route or resource.

External Authentication

For most third-party authentication schemes/strategies, External Authentication is done via a single route using the Passport.authenticate(__strategy__)(request) handler.

This handler typically will redirect the user to the third-party website, authenticate the user, and redirect to the callbackURL configured for that specific strategy (the Internal Authentication step).

server.route({
    method: 'GET',
    path: '/auth/facebook',
    config: {
        handler: function (request, reply) {
            Passport.authenticate('facebook')(request, reply);
        }
    }
});

Internal Authentication

For all authentication schemes and strategies, the External Authentication information must be sent back to the server. Thus, one route handler must be specifically catered to the task of retrieving this information and processing it appropriately (using the Passport.authenticate(strategy, options)(request, passthroughHandler) format).

server.route({
    method: 'GET',
    path: '/auth/facebook/callback',
    config: {
        handler: function (request, reply) {
            
            Passport.authenticate('facebook', {
                failureRedirect: config.urls.failureRedirect,
                successRedirect: config.urls.successRedirect,
                failureFlash: true
            })(request, reply, function (err) {

                if (err && err.isBoom) {
                    // This would be a good place to flash error message
                }
                return reply().redirect('/');
            });
        }
    }
});

Authorization

After verifying the identity of a user, the server will need to determine the permissions a user has for specific routes and resources.

In the typical case, once verified and logged-in, the user has full access to a new set of routes. Travelogue provides a shortcut to verify that a user is logged-on via the config.auth interface.

server.route({
    method: 'GET',
    path: '/home',
    config: { auth: 'passport' },
    handler: function (request, reply) {

        // If logged in already, redirect to /home
        // else to /login
        reply("ACCESS GRANTED");
    }
});

However, it may be the case that there may be several levels of user access permissions. Just like with Passport, a custom function can be created similar to Passport's typical ensureAuthenticated.

Conflicts with Other Hapi Auth Schemes

It is possible to use multiple Hapi authentication schemes simultaneously within one application. To prevent conflicts, set the config.auth to false in any handler that should NOT use any auth schemes. An example is shown below.

server.route({
    method: 'GET',
    path: '/',
    config: { 
        auth: false,
        handler: function (request, reply) {

            reply("Ohai");
        }
    },
});

API Reference

While, Travelogue only requires the use of a few functions to configure and set up Passport authentication, it may be important to customize Travelogue's behavior outside the defaults. Therefore, Travelogue exposes several low-level methods. This section contains documentation for all methods and variables (but may be incomplete for the time being).

Settings

Some settings must be passed into the Hapi plugins architecture.

  • yar - the options object passed to yar
    • cookieOptions
      • password - (Required) secret key used to hash cookie
      • isSecure - enables TLS/SSL cookies. Defaults to true. Disable for normal http.
  • travelogue - the options object passed to travelogue
    • urls - Urls used by Travelogue/Passport
      • failureRedirect - redirect to this relative URL on failed logins. Defaults to '/login'.
      • successRedirect - redirect to this relative URL on successful logins. Defaults to '/'.
    • excludePaths - array of string paths that will not employ travelogue authorization or authentication; these paths will be excluded from Travelogue.

Returns null.

API

Travelogue.passport

Returns an alternative reference to the passport module. Can also be accessed from server.plugins.travelogue.passport.

Travelogue.middleware

Provides direct access to the passport middlewares specifically created for Hapi.

Returns an object with the following interface:

  • authenticate - authentication related functions
  • initialize - initialization related functions

Those functions can be modified to add custom behavior with respect to Passport. Modify at your own risk.

Request-level

Request.session._isAuthenticated()

Returns true if the request is authenticated; false, otherwise.

Request.session._logIn(user, options, next)

Provides a direct interface to setting or modifying the user session data.

  • user - (Required) an object that contains user session data
  • options - the options object (currently unused)
  • next - (Required) callback function to execute on completion or failure

Returns null.

Request.session._logOut()

Permanently deletes the user session data ( thus unauthenticating the user and clearing out cookies).

Returns null.