Koa Escher Authentication Middleware

4.0.0 · active · verified Wed Apr 22

koa-escher-auth is a Koa middleware designed to integrate Escher authentication into Node.js applications. It restricts access to routes by verifying incoming HTTP requests using Escher signatures and a configurable key pool. The package is currently stable at version 4.0.0, released in January 2023, with updates occurring on an irregular basis, typically for dependency upgrades or minor feature enhancements. Key differentiators include its tight integration with the Koa framework and its reliance on the `escher-keypool` for managing authentication credentials, ensuring secure, signed request processing. It is explicitly designed to work downstream of a body-parser middleware to correctly process request bodies for authentication. Escher itself is a stateless API authentication protocol based on AWS Signature Version 4.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a Koa application with Escher authentication, including the necessary body parser middleware and how to access the authenticated user's access key ID from the Koa context.

import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import { authenticator } from 'koa-escher-auth';

// Load Escher configuration from environment variables or provide directly
const escherConfig = {
  credentialScope: process.env.SUITE_ESCHER_CREDENTIAL_SCOPE ?? 'eu/app-id/ems_request',
  keyPool: process.env.SUITE_ESCHER_KEY_POOL ?? JSON.stringify([
    { 'keyId': 'app-id_suite_v1', 'secret': 'app-id-secret', 'acceptOnly': 0 }
  ])
};

const app = new Koa();

// IMPORTANT: koa-bodyparser must be used before koa-escher-auth
app.use(bodyParser());

// Apply the Escher authenticator middleware
app.use(authenticator(escherConfig));

// Define a protected route handler
app.use(async (ctx) => {
  // If authentication passes, the access key ID is available on ctx.escherAccessKeyId
  ctx.body = `Hello world, ${ctx.escherAccessKeyId}! Request authenticated successfully.`;
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Server listening on http://localhost:${port}`);
});

view raw JSON →