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.


$ npm install passport-latch


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(, 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:'/latch-status', 
    passport.authenticate('latch_status', { operationId: 'vxXXNTbQUnKXR9tTiZRe',failureRedirect: '/latchblocked', failureFlash: true }),
    function(req, res) {

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(, 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:'/loginlatch', 
    passport.authenticate('login_with_latch', { failureRedirect: '/loginlatch', failureFlash: true }),
    function(req, res) {


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


$ npm install
$ make test



LGPL License

Copyright (c) 2014 ElevenPaths <>