|
| 1 | +# authorization-parser |
| 2 | + |
| 3 | +[](https://deno.land/x/authorization_parser) |
| 4 | +[](https://doc.deno.land/https/deno.land/x/authorization_parser/mod.ts) |
| 5 | +[](https://github.com/httpland/authorization-parser/releases) |
| 6 | +[](https://codecov.io/gh/httpland/authorization-parser) |
| 7 | +[](https://github.com/httpland/authorization-parser/blob/main/LICENSE) |
| 8 | + |
| 9 | +[](https://github.com/httpland/authorization-parser/actions/workflows/test.yaml) |
| 10 | +[](https://nodei.co/npm/@httpland/authorization-parser/) |
| 11 | + |
| 12 | +HTTP `Authorization` field parser and serializer. |
| 13 | + |
| 14 | +Compliant with |
| 15 | +[RFC 9110, 11.6.2. Authorization](https://www.rfc-editor.org/rfc/rfc9110.html#section-11.6.2). |
| 16 | + |
| 17 | +## Parsing |
| 18 | + |
| 19 | +Parse string into [Authorization](#authorization). |
| 20 | + |
| 21 | +```ts |
| 22 | +import { parseAuthorization } from "https://deno.land/x/authorization_parser@$VERSION/parse.ts"; |
| 23 | +import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; |
| 24 | + |
| 25 | +const result = parseAuthorization("Basic token68"); |
| 26 | + |
| 27 | +assertEquals(parseAuthorization("Basic token68"), { |
| 28 | + authScheme: "Basic", |
| 29 | + token: "token68", |
| 30 | +}); |
| 31 | +assertEquals( |
| 32 | + parseAuthorization(`Bearer realm="example", error="invalid_token"`), |
| 33 | + { |
| 34 | + authScheme: "Bearer", |
| 35 | + token: { |
| 36 | + realm: `"example"`, |
| 37 | + error: `"invalid_token"`, |
| 38 | + }, |
| 39 | + }, |
| 40 | +); |
| 41 | +``` |
| 42 | + |
| 43 | +### Throwing error |
| 44 | + |
| 45 | +In the following cases, throws an error. |
| 46 | + |
| 47 | +- Syntax error |
| 48 | +- Semantic error |
| 49 | + |
| 50 | +#### Syntax error |
| 51 | + |
| 52 | +If field value has an invalid syntax, it may throw a `SyntaxError`. |
| 53 | + |
| 54 | +The syntax follows |
| 55 | +[Authorization ABNF](https://www.rfc-editor.org/rfc/rfc9110.html#section-11.6.2-2). |
| 56 | + |
| 57 | +```ts |
| 58 | +import { parseAuthorization } from "https://deno.land/x/authorization_parser@$VERSION/parse.ts"; |
| 59 | +import { assertThrows } from "https://deno.land/std/testing/asserts.ts"; |
| 60 | + |
| 61 | +assertThrows(() => parseAuthorization("<invalid>")); |
| 62 | +``` |
| 63 | + |
| 64 | +#### Semantic error |
| 65 | + |
| 66 | +In case of semantic errors, throw an `Error`. |
| 67 | + |
| 68 | +- If there is a duplicate key(case insensitive) in |
| 69 | + [auth-param](https://www.rfc-editor.org/rfc/rfc9110.html#section-11.2-5) |
| 70 | + |
| 71 | +```ts |
| 72 | +import { parseAuthorization } from "https://deno.land/x/authorization_parser@$VERSION/parse.ts"; |
| 73 | +import { assertThrows } from "https://deno.land/std/testing/asserts.ts"; |
| 74 | + |
| 75 | +assertThrows(() => |
| 76 | + parseAuthorization("scheme duplicate=value, Duplicate=value") |
| 77 | +); |
| 78 | +``` |
| 79 | + |
| 80 | +## Authorization |
| 81 | + |
| 82 | +`Authorization` is following structure: |
| 83 | + |
| 84 | +| Name | Type | Description | |
| 85 | +| ---------- | ------------------------------------------ | ---------------------- | |
| 86 | +| authScheme | `string` | Authentication scheme. | |
| 87 | +| token | `Token68` | `AuthParam` | `null` | token68 or auth-param. | |
| 88 | + |
| 89 | +### Token68 |
| 90 | + |
| 91 | +It is the same as `string`. |
| 92 | + |
| 93 | +The token68 syntax allows the 66 unreserved URI characters, plus a few others, |
| 94 | +so that it can hold a base64, base64url (URL and filename safe alphabet), |
| 95 | +base32, or base16 (hex) encoding, with or without padding, but excluding |
| 96 | +whitespace. |
| 97 | + |
| 98 | +### AuthParam |
| 99 | + |
| 100 | +It is name/value pairs. |
| 101 | + |
| 102 | +```ts |
| 103 | +interface AuthParam { |
| 104 | + readonly [k: string]: string; |
| 105 | +} |
| 106 | +``` |
| 107 | + |
| 108 | +## API |
| 109 | + |
| 110 | +All APIs can be found in the |
| 111 | +[deno doc](https://doc.deno.land/https/deno.land/x/authorization_parser/mod.ts). |
| 112 | + |
| 113 | +## License |
| 114 | + |
| 115 | +Copyright © 2023-present [httpland](https://github.com/httpland). |
| 116 | + |
| 117 | +Released under the [MIT](./LICENSE) license |
0 commit comments