# express-persona

> Mozilla Persona authentication for your Express application

Latest version **0.1.2** (published 2014-05-29) · BSD license · 0 weekly downloads

## Install

```sh
npm install express-persona
pnpm add express-persona
yarn add express-persona
bun add express-persona
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.2 |
| Published | 2014-05-29 |
| First published | 2012-09-20 |
| Weekly downloads | 0 |
| License | BSD |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 65 |
| Author | Jon Buckley |
| Maintainers | jbuck |
| Keywords | mozilla, persona, browserid, authentication, express |

## Links

- npm: https://www.npmjs.com/package/express-persona
- Repository: https://github.com/jbuck/express-persona
- Issues: https://github.com/jbuck/express-persona/issues
- npm.io page: https://npm.io/package/express-persona

## Dependencies (2)

- [connect](https://npm.io/package/connect.md) ~2.9.0
- [browserid-verify](https://npm.io/package/browserid-verify.md) ~0.1.2

## Alternatives

- [@pothos/plugin-authz](https://npm.io/package/@pothos/plugin-authz.md) — 12.4K weekly downloads
- [@bounded-sh/client](https://npm.io/package/@bounded-sh/client.md) — 3.2K weekly downloads
- [@luigi-project/plugin-auth-oauth2](https://npm.io/package/@luigi-project/plugin-auth-oauth2.md) — 2.3K weekly downloads
- [@nocobase/plugin-verification](https://npm.io/package/@nocobase/plugin-verification.md) — 2.0K weekly downloads
- [escher-request](https://npm.io/package/escher-request.md) — 545 weekly downloads

## Recent versions

- 0.1.2 (latest) — 2014-05-29
- 0.1.1 — 2013-12-02
- 0.1.0 — 2013-09-25
- 0.0.10 — 2013-09-10
- 0.0.9 — 2013-06-14
- 0.0.8 — 2013-06-03
- 0.0.7 — 2012-10-18
- 0.0.6 — 2012-10-03
- 0.0.5 — 2012-09-25
- 0.0.4 — 2012-09-25
- 0.0.3 — 2012-09-24
- 0.0.2 — 2012-09-24
- 0.0.1 — 2012-09-20

## README

express-persona [![Build Status](https://secure.travis-ci.org/jbuck/express-persona.png)](http://travis-ci.org/jbuck/express-persona)
===============

Mozilla Persona integration for Express. express-persona is designed to quickly get
Persona authentication working in your Express application, while following Persona
security best practices.

Quick start
-----------
Install using npm: `npm install express-persona`

Include the module inside your Express application:

```javascript
var express = require("express"),
    app = express();

app.use(express.json())
  .use(express.urlencoded())
  .use(express.cookieParser())
  .use(express.session({
  	secret: "mozillapersona"
  }));

require("express-persona")(app, {
  audience: "http://localhost:8888" // Must match your browser's address bar
});
```

Include the Persona library in your web page:

`<script src="https://login.persona.org/include.js"></script>`

Add login and logout functionality to your buttons:

```javascript
document.querySelector("#login").addEventListener("click", function() {
  navigator.id.request();
}, false);

document.querySelector("#logout").addEventListener("click", function() {
  navigator.id.logout();
}, false);
```

Watch for login and logout actions:

```javascript
navigator.id.watch({
  onlogin: function(assertion) {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/persona/verify", true);
    xhr.setRequestHeader("Content-Type", "application/json");
    xhr.addEventListener("loadend", function(e) {
      var data = JSON.parse(this.responseText);
      if (data && data.status === "okay") {
        console.log("You have been logged in as: " + data.email);
      }
    }, false);

    xhr.send(JSON.stringify({
      assertion: assertion
    }));
  },
  onlogout: function() {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", "/persona/logout", true);
    xhr.addEventListener("loadend", function(e) {
      console.log("You have been logged out");
    });
    xhr.send();
  }
});
```

By default, express-persona adds the users email address to `req.session.email` when their
email is validated.

This library will handle 3 of 4 essential practices for [Persona security considerations]
(https://developer.mozilla.org/en-US/docs/Persona/Security_Considerations) but you should
implement CSRF protection as well. I recommend the built-in express csrf middleware.

You can view and run complete examples in the
[examples directory](https://github.com/jbuck/express-persona/tree/master/examples).

Documentation
-------------

### API

* `require('express-persona')` returns `function(express, options)`
  * `express` is an instance of the express server that you want to add routes to
  * `options` is an object. It has one required parameter, `audience`.

### Required options

* `audience` - The URL of your express app when viewed in a browser. Must include the protocol, hostname, and port.
  * Example: `http://example.org:80`, `https://example.org:443`

### Optional options

* `verifyPath` - The URL that clients use to verify credentials.
  * Default: `/persona/verify`
  * Examples: `/browserid/verify`, `/api/verify`
* `logoutPath` - The URL that clients use to logout.
  * Default: `/persona/logout`
  * Examples: `/browserid/logout`, `/api/logout`
* `sessionKey` - The session key to store the validated email in.
  * Default: `email`
  * Example: `user`, `username`
* `verifierURI` - The URI of the Persona Remote Verification API
  * Default: `https://verifier.login.persona.org/verify`
  * You probably don't want to touch this unless you have a good reason, like testing.
* `verifyResponse(error, req, res, email)` - Function to generate response for verify route
  * Default: see _Verify route_, below, for successess and failure responses
  * `error` will be a string suitable for display (the "reason" attribute in the default implementation), if an error occurs
  * `req, res` are the request and response objects
  * `email` is a string containing the email, and will exist if there is not an error
* `logoutResponse(error, req, res)` - Function to generate response for logout route
  * Default: see _Logout route_, below, for response
  * `error` will be a string suitable for display, if an error occurs
  * `req, res` are the request and response objects
* `middleware(req, res, next)` - Custom middleware for logout/login routes
  * Default: none
  * `req, res` are the request and response objects
  * `next` points to the next middleware function in the chain

### Verify route

* On success:

```javascript
{
  "status": "okay"
  "email": "jon@example.org"
}
```

* On failure

```javascript
{
  "status": "failure"
  "reason": "request failed"
}
```

### Logout route

* Always returns:

```javascript
{
  "status": "okay"
}
```

Tests
-----

### Running Tests

Run tests using `npm test` from the root of the repository.

---
_Source: https://npm.io/package/express-persona · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
