A tiny Koa middleware for parsing Auth headers of various types and saving it to the Koa's context.
yarn add psylence303/koa-auth-parser
Using the middleware:
import Koa from 'koa';
import authParser from 'koa-auth-parser';
const app = new Koa();
app.use(async (ctx, next) => {
await next();
console.log(ctx.state.auth);
});
app.use(authParser());
app.use(ctx => {
ctx.body = 'Hello Koa';
ctx.status = 200;
});
app.listen(3000);
Sample request:
curl --location --request POST 'localhost:3000' --header 'Authorization: Basic c29tZV9hZG1pbjpwYXNzd29yZA=='
Output of the ctx.state.auth:
{
digest: 'c29tZV9hZG1pbjpwYXNzd29yZA==',
scheme: 'Basic',
username: 'some_admin',
password: 'password'
}
It is possible to provide the additional configuration. Currently, the following properties are supported:
Configures the nested property name inside ctx.auth
to hold the parsing results:
app.use(authParser({
propName: 'authentication'
});
If, for some reason, you have to split the authorization header without using the full middleware you can import the corresponding method:
import {splitAuthHeader} from 'koa-auth-parser';
const result = splitAuthHeader('Basic c29tZV9hZG1pbjpwYXNzd29yZA==');
assert.equal(result.scheme, 'Basic');
assert.equal(result.digest, 'c29tZV9hZG1pbjpwYXNzd29yZA==');
- add more auth methods to parse
- cover with tests
MIT