Koa Bearer Token Middleware
raw JSON →koa-bearer-token is a middleware for Koa.js that parses bearer tokens from incoming requests, adhering to RFC6750. It extracts tokens from the `Authorization: Bearer <token>` header, `access_token` query parameter, or `access_token` in the request body. Since version 2.0.0, it also supports extracting tokens from signed or unsigned cookies. The current stable version is 2.0.2, released in August 2021, suggesting a maintenance or slow-cadence release schedule. Key differentiators include its strict RFC6750 compliance, extensive configurability for token keys and locations, and built-in TypeScript support. It integrates seamlessly with Koa applications, making it straightforward to secure API endpoints with OAuth2 bearer tokens. It requires Node.js version 12 or higher.
Common errors
error TypeError: (0, _koaBearertoken.default) is not a function ↓
import bearerToken from 'koa-bearer-token'; to import { bearerToken } from 'koa-bearer-token';. error TypeError: bearerToken is not a function ↓
const bearerToken = require('koa-bearer-token'); to const { bearerToken } = require('koa-bearer-token');. error Error: You must pass secret option in order to sign/unsign cookie ↓
cookie.secret option: bearerToken({ cookie: { signed: true, secret: 'YOUR_APP_SECRET' } }). error Property 'token' does not exist on type 'Request'. ↓
Request interface: declare module 'koa' { interface Request { [myToken]?: string; } }. Warnings
breaking Version 2.0.0 introduced a breaking change by switching from default export to named export. Code using `require('koa-bearer-token')` or `import bearerToken from 'koa-bearer-token'` will fail. ↓
breaking Version 2.0.0 raised the minimum Node.js requirement to version 12. Applications running on older Node.js versions will encounter compatibility issues. ↓
gotcha When extracting tokens from cookies, failing to pass `{ signed: true }` makes your application vulnerable to cookie spoofing, as it will accept non-signed cookies. ↓
gotcha If a token is found in more than one location (e.g., header and query), the middleware will abort the request with a 400 Bad Request status code, per RFC6750. ↓
Install
npm install koa-bearer-token yarn add koa-bearer-token pnpm add koa-bearer-token Imports
- bearerToken wrong
import bearerToken from 'koa-bearer-token';correctimport { bearerToken } from 'koa-bearer-token'; - bearerToken wrong
const bearerToken = require('koa-bearer-token');correctconst { bearerToken } = require('koa-bearer-token'); - Request
declare module 'koa' { interface Request { myToken?: string; } }
Quickstart
import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import { bearerToken } from 'koa-bearer-token';
const app = new Koa();
app.use(bodyParser());
app.use(bearerToken({
cookie: {
signed: false, // Set to true if using signed cookies and provide a secret
secret: process.env.COOKIE_SECRET ?? '', // Required if signed is true
key: 'auth_token', // Custom cookie key
},
reqKey: 'myCustomToken',
}));
app.use((ctx) => {
if (ctx.request.myCustomToken) {
ctx.body = `Token found: ${ctx.request.myCustomToken}`;
} else {
ctx.status = 401;
ctx.body = 'Authentication required';
}
});
app.listen(3000, () => {
console.log('Koa app listening on port 3000');
});