Passwordless
Passwordless is a modern node.js module for Express that allows authentication and authorization without passwords by simply sending one-time password (OTPW) tokens via email or other means. It utilizes a very similar mechanism as the reset password feature of classic websites. The module was inspired by Justin Balthrop's article "Passwords are Obsolete"
Check out a demo and further documentation on https://passwordless.net or have a look at an example.
Token-based authentication is...
- Faster to implement compared to typical user auth systems (you only need one form)
- Better for your users as they get started with your app quickly and don't have to remember passwords
- More secure for your users avoiding the risks of reused passwords
Getting you started
The following should provide a quick-start in using Passwordless. If you need more details check out the example, the deep dive, or the documentation. Also, don't hesitate to raise comments and questions on GitHub.
1. Install the module:
$ npm install passwordless --save
You'll also want to install a TokenStore such as MongoStore and something to deliver the tokens (be it email, SMS or any other means). For example:
$ npm install passwordless-mongostore --save
$ npm install emailjs --save
If you need to store your tokens differently consider developing a new TokenStore and let us know.
2. Require the needed modules
You will need:
- Passwordless
- A TokenStore to store the tokens such as MongoStore
- Something to deliver the tokens such as emailjs for email or twilio for text messages / SMS
var passwordless = ;var MongoStore = ;var email = ;
3. Setup your delivery
This is very much depending on how you want to deliver your tokens, but if you use emailjs this could look like this:
var smtpServer = emailserver;
4. Initialize Passwordless
passwordless.init()
will take your TokenStore, which will store the generated tokens as shown below for a MongoStore:
// Your MongoDB TokenStorevar pathToMongoDb = 'mongodb://localhost/passwordless-simple-mail';passwordless;
5. Tell Passwordless how to deliver 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:
http://www.example.com/?token={TOKEN}&uid={UID}
That's how you could do this with emailjs:
// Set up a delivery servicepasswordless;
6. Setup the middleware for express
app;app;
sessionSupport()
makes the login persistent, so the user will stay logged in while browsing your site. Make sure to have added your session middleware before this line. Have a look at express-session how to setup sessions if you are unsure. Please be aware: If you decide to use cookie-session rather than e.g. express-session as your middleware you have to set passwordless.init(tokenStore, {skipForceSessionSave:true})
acceptToken()
will accept incoming tokens and authenticate the user (see the URL in step 5). 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. When provided, the user will be forwarded to the given URL as soon as she has been authenticated.
Instead of accepting tokens on any URL as done above you can also restrict the acceptance of tokens to certain URLs:
// Accept tokens only on /logged_in (be sure to change the// URL you deliver in step 5)router;
7. The router
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 URLs to:
- Display a page asking for the user's email (or phone number, ...)
- Receive these details (via POST) and identify the user
For example like this:
/* GET login screen. */router; /* POST login details. */router;
What happens here? passwordless.requestToken(getUserId)
has two tasks: Making sure the email address exists and transforming it into a proper user ID that will become the identifier from now on. For example user@example.com becomes 123 or 'u1002'. You call callback(null, ID)
if all is good, callback(null, null)
if you don't know this email address, and callback('error', null)
if something went wrong. At this stage, please make sure that you've added middleware to parse POST data (such as body-parser).
Most likely, you want a user registration page where you take an email address and any other user details and generate an ID. However, you can also simply accept any email address by skipping the lookup and just calling callback(null, user)
.
In an even simpler scenario and if you just have a fixed list of users do the following:
// GET login as above var users = id: 1 email: 'marc@example.com' id: 2 email: 'alice@example.com' ; /* POST login details. */router;
8. Login page
All you need is a form where users enter their email address, for example:
Login Email:
By default, Passwordless will look for a field called user
submitted via POST.
9. Protect your pages
You can protect all pages that should only be accessed by authenticated users by using the passwordless.restricted()
middleware, for example:
/* GET restricted site. */router;
You can also protect a full path, by adding:
router;
10. Who is logged in?
Passwordless stores the user ID in req.user (this can be changed via configuration). So, if you want to display the user's details or use them for further requests, do something like:
router;
You could also create a middleware that is adding the user to any request and enriching it with all user details. Make sure, though, that you are adding this middleware after acceptToken()
and sessionSupport()
:
app
Common options
Logout
Just call passwordless.logout()
as in:
router;
Redirects
Redirect non-authorised users who try to access protected resources with failureRedirect
(default is a 401 error page):
router
Redirect unsuccessful login attempts with failureRedirect
(default is a 401 or 400 error page):
router;
After the successful authentication through acceptToken()
, you can redirect the user to a specific URL with successRedirect
:
app;
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. When provided, the user will be forwarded to the given URL as soon as she has been authenticated. If not provided, Passwordless will simply call the next middleware.
Error flashes
Error flashes are session-based error messages that are pushed to the user with the next request. For example, you might want to show a certain message when the user authentication was not successful or when a user was redirected after accessing a resource she should not have access to. To make this work, you need to have sessions enabled and a flash middleware such as connect-flash installed.
Error flashes are supported in any middleware of Passwordless that supports failureRedirect
(see above) but only(!) if failureRedirect
is also supplied:
restricted()
when the user is not authorized to access the resourcerequestToken()
when the supplied user details are unknown
As an example:
router;
The error flashes are pushed onto the passwordless
array of your flash middleware. Check out the connect-flash docs how to pull the error messages, but a typical scenario should look like this:
router;
Success flashes
Similar to error flashes success flashes are session-based messages that are pushed to the user with the next request. For example, you might want to show a certain message when the user has clicked on the token URL and the token was accepted by the system. To make this work, you need to have sessions enabled and a flash middleware such as connect-flash installed.
Success flashes are supported by the following middleware of Passwordless:
acceptToken()
when the token was successfully validatedlogout()
when the user was logged in and was successfully logged outrequestToken()
when the token was successfully stored and send out to the user
Consider the following example:
router;
The messages are pushed onto the passwordless-success
array of your flash middleware. Check out the connect-flash docs how to pull the messages, but a typical scenario should look like this:
router;
2-step authentication (e.g. for SMS)
For some token-delivery channels you want to have the shortest possible token (e.g. for text messages). One way to do so is to remove the user ID from the token URL and to only keep the token for itself. The user ID is then kept in the session. In practice his could look like this: A user types in his phone number, hits submit, is redirected to another page where she has to type in the token received per SMS, and then hit submit another time.
To achieve this, requestToken stores the requested UID in req.passwordless.uidToAuth
. Putting it all together, take the following steps:
1: Read out req.passwordless.uidToAuth
// Display a new form after the user has submitted the phone numberrouter
2: Display another form to submit the token submitting the UID in a hidden input
Login You should have received a token via SMS. Type it in below: Token:
3: Allow POST to accept tokens
router;
Successful login and redirect to origin
Passwordless supports the redirect of users to the login page, remembering the original URL, and then redirecting them again to the originally requested page as soon as the token has been accepted. Due to the many steps involved, several modifications have to be undertaken:
1: Set originField
and failureRedirect
for passwordless.restricted()
Doing this will call /login
with /login?origin=/admin
to allow later reuse
router;
2: Display origin
as hidden field on the login page
Be sure to pass origin
to the page renderer.
Token:
3: Let requestToken()
accept origin
This will store the original URL next to the token in the TokenStore.
app;
4: Reconfigure acceptToken()
middleware
app;
Several delivery strategies
In case you want to use several ways to send out tokens you have to add several delivery strategies to Passwordless as shown below:
passwordless;passwordless;
To simplify your code, provide the field delivery
to your HTML page which submits the recipient details. Afterwards, requestToken()
will allow you to distinguish between the different methods:
router;
Modify lifetime of a token
This is particularly useful if you use shorter tokens than the default to keep security on a high level:
// Lifetime in ms for the specific delivery strategypasswordless;
Allow token reuse
By default, all tokens are invalidated after they have been used by the user. Should a user try to use the same token again and is not yet logged in, she will not be authenticated. In some cases (e.g. stateless operation or increased convenience) you might want to allow the reuse of tokens. Please be aware that this might open up your users to the risk of valid tokens being used by third parties without the user being aware of it.
To enable the reuse of tokens call init()
with the option allowTokenReuse: true
, as shown here:
passwordless;
Different tokens
You can generate your own tokens. This is not recommended except you face delivery constraints such as SMS-based authentication. If you reduce the complexity of the token, please consider reducing as well the lifetime of the token (see above):
passwordless;
Stateless operation
Just remove the app.use(passwordless.sessionSupport());
middleware. Every request for a restricted resource has then to be combined with a token and uid. You should consider the following points:
- By default, tokens are invalidated after their first use. For stateless operations you should call
passwordless.init()
with the following option:passwordless.init(tokenStore, {allowTokenReuse:true})
(for details see above) - Tokens have a limited lifetime. Consider extending it (for details see above), but be aware about the involved security risks
- Consider switching off redirects such as
successRedirect
on theacceptToken()
middleware
The tokens and security
By default, tokens are generated using 16 Bytes of pseudo-random data as produced by the cryptographically strong crypto library of Node.js. This can be considered strong enough to withstand brute force attacks especially when combined with a finite time-to-live (set by default to 1h). In addition, it is absolutely mandatory to store the tokens securely by hashing and salting them (done by default with TokenStores such as MongoStore). Security can be further enhanced by limiting the number of tries per user ID before locking that user out from the service for a certain amount of time.
Further documentation
Tests
Download the whole repository and call:
$ npm test
License
Author
Florian Heinemann @thesumofall