Skip to main content
Deno 2 is finally here 🎉️
Learn more

authorization-parser

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM

HTTP Authorization field parser and serializer.

Compliant with RFC 9110, 11.6.2. Authorization.

Parsing

Parse string into Authorization.

import { parseAuthorization } from "https://deno.land/x/authorization_parser@$VERSION/parse.ts";
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";

const result = parseAuthorization("Basic token68");

assertEquals(parseAuthorization("Basic token68"), {
  authScheme: "Basic",
  token: "token68",
});
assertEquals(
  parseAuthorization(`Bearer realm="example", error="invalid_token"`),
  {
    authScheme: "Bearer",
    token: {
      realm: `"example"`,
      error: `"invalid_token"`,
    },
  },
);

Throwing error

In the following cases, throws an error.

  • Syntax error
  • Semantic error

Syntax error

If field value has an invalid syntax, it may throw a SyntaxError.

The syntax follows Authorization ABNF.

import { parseAuthorization } from "https://deno.land/x/authorization_parser@$VERSION/parse.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

assertThrows(() => parseAuthorization("<invalid>"));

Semantic error

In case of semantic errors, throw an Error.

  • If there is a duplicate key(case insensitive) in auth-param
import { parseAuthorization } from "https://deno.land/x/authorization_parser@$VERSION/parse.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

assertThrows(() =>
  parseAuthorization("scheme duplicate=value, Duplicate=value")
);

Authorization

Authorization is following structure:

Name Type Description
authScheme string Authentication scheme.
token Token68 | AuthParam | null token68 or auth-param.

Token68

It is the same as string.

The token68 syntax allows the 66 unreserved URI characters, plus a few others, so that it can hold a base64, base64url (URL and filename safe alphabet), base32, or base16 (hex) encoding, with or without padding, but excluding whitespace.

AuthParam

It is name/value pairs.

interface AuthParam {
  readonly [k: string]: string;
}

API

All APIs can be found in the deno doc.

License

Copyright © 2023-present httpland.

Released under the MIT license