Express Azure AD JWT Middleware
raw JSON →This package provides Express middleware specifically designed for authenticating HTTP requests using JSON Web Tokens (JWTs) issued by Azure Active Directory (AAD). It streamlines the process of validating incoming JWTs and, upon successful validation, decodes the token and attaches the resulting JSON object to `req.user` (or a configurable property like `req.auth`). This allows subsequent middleware or route handlers to easily access user information for authorization and access control. The current stable version, 0.2.2, was released several years ago, indicating the project is likely no longer actively maintained. Its primary differentiation lies in its explicit focus on Azure AD JWT validation within the Express framework, simplifying integration for applications within the Microsoft ecosystem.
Common errors
error Error: UnauthorizedError: No authorization token was found ↓
Authorization: Bearer <YOUR_JWT_TOKEN> for protected routes. error Error: UnauthorizedError: invalid token ↓
express-azure-jwt middleware options. Warnings
breaking The package is effectively abandoned. The last commit on GitHub was in January 2017, and the latest npm version (0.2.2) was published over 7 years ago. This means there will be no new features, bug fixes, or security patches, which is a significant risk for authentication middleware. ↓
gotcha This middleware is specifically designed for JWTs issued by Azure Active Directory. Attempting to use it with JWTs from other identity providers (e.g., Auth0, Firebase, custom JWTs) may lead to validation failures or unexpected behavior, as it relies on AAD-specific issuer and audience validation logic. ↓
gotcha Due to its age and CommonJS-first design, this package may have compatibility issues or require specific configuration (e.g., `esModuleInterop` in TypeScript) when used in modern Node.js environments that default to ES Modules (ESM). ↓
gotcha The default behavior on an invalid or missing token is to throw an `UnauthorizedError`. While the README provides an example of catching this, failure to implement a global error handler for `UnauthorizedError` will result in unhandled exceptions crashing the application or returning generic server errors. ↓
Install
npm install express-azure-jwt yarn add express-azure-jwt pnpm add express-azure-jwt Imports
- jwt
const jwt = require('express-azure-jwt'); - jwt wrong
import { jwt } from 'express-azure-jwt';correctimport jwt from 'express-azure-jwt';
Quickstart
import express from 'express';
import jwt from 'express-azure-jwt';
const app = express();
const PORT = process.env.PORT || 3000;
// Basic JWT middleware initialization. Replace with your actual audience.
// In a real application, configuration like audience should come from environment variables.
const azureJwtMiddleware = jwt({
audience: process.env.AZURE_AD_AUDIENCE || 'api://my-app-id'
});
// Error handling middleware for JWT validation failures
app.use((err: any, req: express.Request, res: express.Response, next: express.NextFunction) => {
if (err.name === 'UnauthorizedError') {
console.error('Unauthorized request:', err.message);
return res.status(401).send('Invalid or missing authentication token.');
}
next(err);
});
// Apply JWT authentication to most routes, but exclude '/public' and '/token'
app.use(azureJwtMiddleware.unless({ path: ['/public', '/token'] }));
// Public route, no authentication required
app.get('/public', (req, res) => {
res.status(200).send('This is a public endpoint.');
});
// Protected route - requires a valid Azure AD JWT
app.get('/protected', (req, res) => {
// req.user will contain the decoded JWT payload if authentication was successful
if (!req.user) {
return res.status(401).send('Authentication information not found.');
}
res.status(200).json({ message: 'Access granted to protected data!', user: req.user });
});
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log('Try accessing /public without a token.');
console.log('Try accessing /protected with a valid Azure AD JWT in Authorization: Bearer header.');
});