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-ethereum-siwe

v0.1.0

Published

Ethereum authentication strategy for Passport.

Downloads

633

Readme

passport-ethereum-siwe

Passport strategy for authenticating with Sign-In with Ethereum.

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

Install

$ npm install passport-ethereum-siwe

Usage

The Ethereum authentication strategy authenticates users using an Ethereum wallet.

The strategy takes a verify function as an argument, which accepts address as an argument. address is the user's Ethereum address. When authenticating a user, this strategy obtains this information from a message signed by the user's wallet.

The verify function is responsible for determining the user to which the address belongs. In cases where the account is logging in for the first time, a new user record is typically created automatically. On subsequent logins, the existing user record will be found via its relation to the address.

Because the verify function is supplied by the application, the app is free to use any database of its choosing. The example below illustrates usage of a SQL database.

var EthereumStrategy = require('passport-ethereum-siwe');
var SessionNonceStore = require('passport-ethereum-siwe').SessionNonceStore;

var store = new SessionChallengeStore();

passport.use(new EthereumStrategy({ store: store },
  function verify(address, cb) {
    db.get('SELECT * FROM blockchain_credentials WHERE chain = ? AND address = ?', [
      'eip155:1',
      address
    ], function(err, row) {
      if (err) { return cb(err); }
      if (!row) {
        db.run('INSERT INTO users (username) VALUES (?)', [
          address
        ], function(err) {
          if (err) { return cb(err); }
          var id = this.lastID;
          db.run('INSERT INTO blockchain_credentials (user_id, chain, address) VALUES (?, ?, ?)', [
            id,
            'eip155:1',
            address
          ], function(err) {
            if (err) { return cb(err); }
            var user = {
              id: id,
              username: address
            };
            return cb(null, user);
          });
        });
      } else {
        db.get('SELECT rowid AS id, * FROM users WHERE rowid = ?', [ row.user_id ], function(err, row) {
          if (err) { return cb(err); }
          if (!row) { return cb(null, false); }
          return cb(null, row);
        });
      }
    });
  }
));

Define Routes

Two routes are needed in order to allow users to log in with their Ethereum wallet.

The first route generates a randomized nonce, saves it in the NonceStore, and sends it to the client-side JavaScript for it to be included in the signed message. This is necessary in order to protect against replay attacks.

router.post('/login/ethereum/challenge', function(req, res, next) {
  store.challenge(req, function(err, nonce) {
    if (err) { return next(err); }
    res.json({ nonce: nonce });
  });
});

The second route authenticates the signed message and logs the user in.

router.post('/login/ethereum',
  passport.authenticate('ethereum', { failWithError: true }),
  function(req, res, next) {
    res.json({ ok: true });
  },
  function(err, req, res, next) {
    res.json({ ok: false });
  });

Examples

License

The MIT License

Copyright (c) 2022 Jared Hanson <https://www.jaredhanson.me/>