Giter Club home page Giter Club logo

passport-securelogin's Introduction

Passport SecureLogin

Travis Coveralls Release Downloads License

A Passport strategy and Express middleware for SecureLogin authentication.

What Is SecureLogin?

SecureLogin is a decentralized authentication protocol for websites and apps. Classic passwords/2FA are poorly designed, hard to backup and inconvenient to use. SecureLogin is an all-in-one solution that creates a cryptographic private key from your email and master password to sign in everywhere and helps you forget about passwords.

Learn more about SecureLogin here.

This module provides a set of useful tools to develop Node.js apps that use the SecureLogin protocol. You can easily provide authentication by using the Passport strategy, and confirm important actions using the included Express middleware. See usage below.

Usage

Authentication with Passport

Use the SecureLogin strategy as you would any other Passport strategy. It's super easy. Here's how you would get it set up:

passport.use(new SecureLogin.Strategy({ domains: 'http://c.dev:3001' }));

app.post('/login', passport.authenticate('securelogin', { session: true }),
    (req, res) => res.sendStatus(200));

You can also check to make sure you want to let a user login. This could be useful if you only want to allow only certain people to authenticate (e.g. a private blog or beta website).

passport.use(new SecureLogin.Strategy({ domains: 'http://c.dev:3001' },
    (user, done) => {
        // Do some verification here, then call `done(err, user, info)`
        if (user.authkeys.public === 'WfgIE2wK/9N3PQE5KpZOCwNEPVAFV3c8T6NweX+dSos=') {
            done(null, user);
        } else {
            done(null, false, 'not allowed to authenticate');
        }
    }));

app.post('/login', passport.authenticate('securelogin', { session: true }),
    (req, res) => res.sendStatus(200));

Action Confirmation

Important actions should be verified to make sure they are being performed by the person who they claim to be. This can be easily done using this module by using something like the following Express route:

app.post('/sendmoney', SecureLogin.ScopeMiddleware({ domains: 'http://c.dev:3001' }),
    (req, res) => {
        console.log(`${req.user.authkeys.public} -> $${req.securelogin.scope.amount} -> ${req.securelogin.scope.address}`);
        res.json(req.securelogin.scope);
    });

When SecureLogin responds to the /sendmoney callback URL (which we define in the client-side JavaScript), the middleware will verify the response and set req.securelogin.scope to the verified scope if the verification is successful. Otherwise, req.securelogin.errors will contain the errors.

Profile Change

An important part of the SecureLogin protocol is the ability to change profiles. This could include a user changing their email and/or password, which will also change their identifying public key. Here's how you would implement this endpoint in passport-securelogin:

app.post('/securelogin', SecureLogin.SLMiddleware({ domains: DOMAINS },
    (err, newUser, oldPublicKey) => {
        if (err) {
            console.log(err);
        } else {
            // Update user's database entry with the new profile info
        }
    }));

The SecureLogin.SLMiddleware requires a callback with an err, a newUser object and the user's oldPublicKey. Assuming you're using the user's public key as a unique identifier in your database (you should be doing this), you can use oldPublicKey to find the user, and update their profile according to the newUser object.

Example

An example application can be found in the /example directory. To run the example, cd into the directory, npm install the dependencies, then run node server. You should be able to visit http://localhost:3001 to see the app. This example is also running at https://passport-securelogin.herokuapp.com if you would like to take a look.

passport-securelogin's People

Contributors

andrewda avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

passport-securelogin's Issues

Refactor module usage

For example, usage should look something like this:

const sl = new SecureLogin({
  origins: ['localhost:3001', 'http://c.dev'],
  // automatically creates `/securelogin` path based on changeCredentials function
  changeCredentials: (err, newUser, oldPublicKey) => {
    if (err) {
      console.log(err);
    } else {
      // do database stuff to update the user
    }
  }
});

// now you can access all SecureLogin methods through `sl`

passport.use(new sl.Strategy({ domains: 'http://c.dev:3001' }));

app.post('/login', passport.authenticate('securelogin', { session: true }),
  (req, res) => res.sendStatus(200));

app.post('/sendmoney', sl.ScopeMiddleware({ domains: 'http://c.dev:3001' }),
  (req, res) => {
    console.log(`${req.user.authkeys.public} -> $${req.securelogin.scope.amount} -> ${req.securelogin.scope.address}`);
    res.json(req.securelogin.scope);
  });

Add tests

It's important that this module is always working properly. Tests are super important.

Add /securelogin path

Users should be able to do:

app.use(SecureLogin.Express())

or something similar to that in order to create a /securelogin path with the necessary features.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.