Passwordless is an authentication middleware for Node.js that improves security for your users while being fast and easy to deploy.
Most websites are, however, still stuck with the same authentication mechanism as from the earliest days of the web: username and password.
While username and password have their place, we should be much more challenging if they are the right solution for our projects. We know that most people use the same password on all the sites they visit. For projects without dedicated security experts, should we really open up our users to the risk that a breach of our site also compromises their Amazon account? Also, the classic mechanism has by default at least two attack vectors: the login page and the password recovery page. Especially the latter is often implemented hurried and hence inherently more risky.
Unfortunately—depending on your technology stack—there a few to none ready-made solutions out there. Passwordless changes this for Node.js.
Getting started on Node.js & Express
Getting started with Passwordless is straight-forward and you’ll be able to deploy a fully fledged and secure authentication solution for a small project within two hours:
$ npm install passwordless --save
$ npm install passwordless-mongostore --save
To deliver the tokens to the users, email would be the most common option (but text message is also feasible) and you’re free to pick any of the existing email frameworks such as:
$ npm install emailjs --save
Setting up the basics
Let’s require all of the above mentioned modules in the same file that you use to initialise Express:
If you’ve chosen emailjs for delivery that would also be a great moment to connect it to your email account (e.g. a Gmail account):
The final preliminary step would be to tell Passwordless which storage interface you’ve chosen above and to initialise it:
Delivering a token
passwordless.addDelivery(deliver) adds a new delivery mechanism.
deliver is called whenever a token has to be sent. By default, the mechanism you choose should provide the user with a link in the following format:
deliver will be called with all the needed details. Hence, the delivery of the token (in this case with emailjs) can be as easy as:
Initialising the Express middleware
sessionSupport() makes the login persistent, so the user will stay logged in while browsing your site. Please make sure that you’ve already prepared your session middleware (such as express-session) beforehand.
acceptToken() will intercept any incoming tokens, authenticate users, and redirect them to the correct page. While the option
successRedirect is not strictly needed, it is strongly recommended to use it to avoid leaking valid tokens via the referrer header of outgoing HTTP links on your site.
Routing & Authenticating
The following takes for granted that you’ve already setup your router
var router = express.Router(); as explained in the express docs
You will need at least two URLs to:
- Display a page asking for the user’s email
- Accept the form details (via POST)
What happens here?
passwordless.requestToken(getUserId) has two tasks: Making sure the email address exists and transforming it into a unique user ID that can be sent out via email and can be used for identifying users later on. Usually, you’ll already have a model that is taking care of storing your user details and you can simply interact with it as shown in the example above.
In some cases (think of a blog edited by just a couple of users) you can also skip the user model entirely and just hardwire valid email addresses with their respective IDs:
All it needs is a simple HTML form capturing the user’s email address. By default, Passwordless will look for an input field called
Protecting your pages
Passwordless offers middleware to ensure only authenticated users get to see certain pages:
Who is logged in?
By default, Passwordless makes the user ID available through the request object:
req.user. To display or reuse the ID it to pull further details from the database you can do the following:
Or, more generally, you can add another middleware that pulls the whole user record from your model and makes it available to any route on your site:
That’s all it takes to let your users authenticate securely and easily. For more details you should check out the deep dive which explains all the options and the example that will show you how to integrate all of the things above into a working solution.
As mentioned earlier, all authentication systems have their tradeoffs and you should pick the right system for your needs. Token-based channels share one risk with the majority of other solutions incl. the classic username/password scheme: If the user’s email account is compromised and/or the channel between your SMTP server and the user’s, their account on your site will be compromised as well. Two default options help mitigate (but not entirely eliminate!) this risk: Short-lived tokens and automatic invalidation of tokens after they’ve been used once.
For most sites token-based authentication represents a step up in security: users don’t have to think of new passwords (which are usually too simple) and there is no risk of them reusing passwords. For us as developers, Passwordless offers a solution that has only one (and simple!) path of authentication that is easier to understand and hence to protect. Also, we don’t have to touch any user passwords.
Another point is usability. We should consider both, the first time usage of your site and the following logons. For first-time users, token-based authentication couldn’t be more straight-forward: They will still have to validate their email address as they have to with classic login mechanisms, but in the best-case scenario there will be no additional details required. No creativity needed to come up with a password that fulfils all restrictions and nothing to memorise. If the user logins again, the experience depends on the specific use case. Most websites have relatively long session timeouts and logins are relatively rare. Or, people’s visits to the website are actually so infrequent that they will have difficulties recounting if they already had an account and if so what the password could have been. In those cases Passwordless presents a clear advantage in terms of usability. Also, there are few steps to take and those can be explained very clearly along the process. Websites that users visit frequently and/or that have conditioned people to login several times a week (think of Amazon) might however benefit from a classic (or even better: two-factor) approach as people will likely be aware of their passwords and there might be more opportunity to convince users about the importance of good passwords.
This blog post has been published on Mozilla Hacks.