0.1.0 • Published 10 years ago

passport-latch v0.1.0

Weekly downloads
1
License
-
Repository
github
Last release
10 years ago

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/](http://www.elevenpaths.com/)