Express Meshblu Authentication Middleware

9.2.1 · active · verified Wed Apr 22

express-meshblu-auth is an Express.js middleware library designed to handle all authentication styles for the Meshblu IoT platform. It simplifies the process of integrating Meshblu device authentication into Express applications by supporting various methods including cookies (meshblu_auth_uuid, meshblu_auth_token), HTTP headers (using the same cookie names), Basic authentication, and Bearer tokens. The library provides middleware functions like `auth()`, `get()`, `gateway()`, `gatewayDevice()`, and `gatewayRedirect()` to retrieve, validate, and attach Meshblu credentials and device information to the request object, or enforce access control. The current stable version is 9.2.1. While there isn't a strict release cadence, the project has seen several updates within major version 9 and a jump from v8 to v9, indicating active maintenance. Its primary differentiation lies in its specific integration with the Meshblu ecosystem, offering out-of-the-box support for its authentication paradigms.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to initialize the MeshbluAuth middleware and apply its core authentication (`auth()`), device retrieval (`get()`), and access control (`gateway()`, `gatewayDevice()`, `gatewayRedirect()`) functions to an Express application. It showcases how to access authenticated Meshblu UUID and token from the request object.

const express = require('express');
const MeshbluAuth = require('express-meshblu-auth');

const meshbluAuth = new MeshbluAuth({
  protocol: 'https',
  server: 'meshblu.octoblu.com',
  port: 443
});

const app = express();

// Retrieves the uuid & token from the request,
// validate them, then add them to request.meshbluAuth
app.use(meshbluAuth.auth());

// Retrieves the uuid & token from the request,
// validate them by retrieving the device, then:
// add credentials to request.meshbluAuth
// add device to request.meshbluDevice
app.use(meshbluAuth.get());

// Returns a 401 if no uuid & token were provided in the request
// Returns a 403 if the uuid & token provided were invalid
// calls next otherwise
// meshbluAuth.auth or meshbluAuth.get MUST BE CALLED FIRST in the middleware chain
app.use(meshbluAuth.gateway());

// Returns a 401 if no uuid & token were provided in the request
// Returns a 403 if the uuid & token provided were invalid
// Returns a 403 if the uuid given does not match the authorized uuid
// calls next otherwise
// meshbluAuth.auth or meshbluAuth.get MUST BE CALLED FIRST in the middleware chain
app.use(meshbluAuth.gatewayDevice('uuid'));

// Can be used instead of gateway. Redirects user if uuid & token were not
// provided or were not valid
app.use(meshbluAuth.gatewayRedirect('/login'));

app.use(function (request, response) {
  response.json({uuid: request.meshbluAuth.uuid, token: request.meshbluAuth.token});
});

app.listen(3333, () => {
  console.log('Meshblu auth example app listening on port 3333');
});

view raw JSON →