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

passport-latch

v0.1.0

Published

Latch authentication strategy for Passport.

Downloads

2

Readme

Passport-latch

Passport strategy for latch.

This module lets you use using latch in your Node.js applications. By plugging into Passport, latch can be easily and unobtrusively integrated into any application or framework that supports Connect-style middleware, including Express.

Note that in contrast to most Passport strategies, latch usually requires that a user already be authenticated using an initial factor, although you can still use passport-latch as a normal Passport module to protect any operation. Requirements regarding when to require a second factor are a matter of application-level policy, and outside the scope of both Passport and this strategy.

Install

$ npm install passport-latch

Usage

Configure Strategy

The latch strategy authorizes a user using his accountId previously obtained when pairing his account with latch.

passport.use('latch_status', new LatchStrategy({appId: 'vxXXNTbQUnKXR9tTiZRe', secretKey: 'xA4xFx8pCx4iY4AyJzeFb4sqvNmjiiNT4kk22FNN'},
    function(user, done) {
        // setup function, supply key and period to done callback
        findAccountIdForUserId(user.id, function(err, obj) {
            if (err) { return done(err); }
            return done(null, obj.accountId);
        });
    }
));

Authenticate Requests

Use passport.authenticate(), specifying the 'latch' strategy, to authenticate requests.

For example, as route middleware in an Express application:

app.post('/latch-status', 
    passport.authenticate('latch_status', { operationId: 'vxXXNTbQUnKXR9tTiZRe',failureRedirect: '/latchblocked', failureFlash: true }),
    function(req, res) {
        res.redirect('/');
});

You can also use latch with any other Passport Strategy, just by quering the latch servers after a successful authentication:

passport.use('login_with_latch', new LocalStrategy(function(username, password, done) {
    process.nextTick(function () {
        // Find the user by username.  If there is no user with the given
        // username, or the password is not correct, set the user to `false` to
        // indicate failure and set a flash message.  Otherwise, return the
        // authenticated `user`.
        findByUsername(username, function(err, user) {
            if (err) { return done(err); }
            if (!user) { return done(null, false, { message: 'Invalid username or password' }); }
            if (user.password != password) { return done(null, false, { message: 'Invalid username or password' }); }
        
            // Once the authentication has been succesful, we look for his accountId by id
            var accountId = findAccountIdForUserId(user.id, function(err, obj) {
                if (err) { return false; }
                if (obj) {
                    return obj.accountId;
                } else {
                    return false;
                }
            });
        
            if (accountId) {
                var rv = latch.status(accountId, function(err, data) {
                    if ((err) || (!data)) {
                        return done(null, user);
                    } 
                    if (data['data']['operations']['vxXXNTbQUnKXR9tTiZRe']['status'] == "on") {
                        // If it is 'on', it means that the operation is not blocked
                        return done(null, user);
                    } else {
                        // The operation is blocked by latch
                        return done(null, false, { message: 'Latch is blocking your login' });
                    }
                });
            } else {
                return done(null, user);
            }
         })
     });
}));

And then as route middleware in an Express application:

app.post('/loginlatch', 
    passport.authenticate('login_with_latch', { failureRedirect: '/loginlatch', failureFlash: true }),
    function(req, res) {
        res.redirect('/');
});

Examples

For a complete, working example, refer to the example.

Tests

$ npm install
$ make test

Credits

License

LGPL License

Copyright (c) 2014 ElevenPaths <http://www.elevenpaths.com/>