0.2.0 • Published 2 years ago

koa-auth-parser v0.2.0

Weekly downloads
97
License
MIT
Repository
-
Last release
2 years ago

koa-auth-parser

A tiny Koa middleware for parsing Auth headers of various types and saving it to the Koa's context.

Installation

yarn add psylence303/koa-auth-parser

Example

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'
}

Configuration

It is possible to provide the additional configuration. Currently, the following properties are supported:

propName

Configures the nested property name inside ctx.auth to hold the parsing results:

app.use(authParser({
  propName: 'authentication'
});

Other API methods

splitAuthHeader(ctx)

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==');

Currently supported auth methods

TODO:

  • add more auth methods to parse
  • cover with tests

License

MIT