1 | # Express Meshblu Authentication Middleware
|
2 |
|
3 | Express middleware to support all meshblu auth styles
|
4 |
|
5 | [![Build Status](https://travis-ci.org/octoblu/express-meshblu-auth.svg?branch=master)](https://travis-ci.org/octoblu/express-meshblu-auth) [![Code Climate](https://codeclimate.com/github/octoblu/express-meshblu-auth/badges/gpa.svg)](https://codeclimate.com/github/octoblu/express-meshblu-auth) [![Test Coverage](https://codeclimate.com/github/octoblu/express-meshblu-auth/badges/coverage.svg)](https://codeclimate.com/github/octoblu/express-meshblu-auth) [![npm version](https://badge.fury.io/js/express-meshblu-auth.svg)](http://badge.fury.io/js/express-meshblu-auth) [![Gitter](https://badges.gitter.im/octoblu/help.svg)](https://gitter.im/octoblu/help)
|
6 |
|
7 | ## Supported Auth Methods
|
8 |
|
9 | * cookies: `request.cookies.meshblu_auth_uuid` and `request.cookies.meshblu_auth_token`
|
10 | * headers: `request.cookies.meshblu_auth_uuid` and `request.cookies.meshblu_auth_token`
|
11 | * basic: `Authorization: Basic c3VwZXItcGluazpwaW5raXNoLXB1cnBsZWlzaAo=`
|
12 | * bearer: `Authorization: Bearer c3VwZXItcGluazpwaW5raXNoLXB1cnBsZWlzaAo=`
|
13 |
|
14 | ## Example:
|
15 |
|
16 | ```javascript
|
17 | var express = require('express');
|
18 | var MeshbluAuth = require('express-meshblu-auth');
|
19 | var meshbluAuth = new MeshbluAuth({
|
20 | protocol: 'https',
|
21 | server: 'meshblu.octoblu.com',
|
22 | port: 443
|
23 | });
|
24 |
|
25 | var app = express();
|
26 | // Retrieves the uuid & token from the request,
|
27 | // validate them, then add them to request.meshbluAuth
|
28 | app.use(meshbluAuth.auth());
|
29 |
|
30 | // Retrieves the uuid & token from the request,
|
31 | // validate them by retrieving the device, then:
|
32 | // add credentials to request.meshbluAuth
|
33 | // add device to request.meshbluDevice
|
34 | app.use(meshbluAuth.get());
|
35 |
|
36 | // Returns a 401 if no uuid & token were provided in the request
|
37 | // Returns a 403 if the uuid & token provided were invalid
|
38 | // calls next otherwise
|
39 | // meshbluAuth.auth or meshbluAuth.get MUST BE CALLED FIRST in the middleware chain
|
40 | app.use(meshbluAuth.gateway());
|
41 |
|
42 | // Can be used instead of gateway. Redirects user if uuid & token were not
|
43 | // provided or were not valid
|
44 | app.use(meshbluAuth.gatewayRedirect('/login'));
|
45 |
|
46 | app.use(function (request, response) {
|
47 | response.json({uuid: request.meshbluAuth.uuid, token: request.meshbluAuth.token});
|
48 | });
|
49 | app.listen(3333);
|
50 | ```
|