Socket
Book a DemoInstallSign in
Socket

passport-latch

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

passport-latch

Latch authentication strategy for Passport.

0.1.0
latest
Source
npmnpm
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

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

  • ElevenPaths

License

LGPL License

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

Keywords

passport

FAQs

Package last updated on 14 Jul 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.