|
| 1 | +// Copyright 2023-latest the httpland authors. All rights reserved. MIT license. |
| 2 | +// This module is browser compatible. |
| 3 | + |
| 4 | +import { isNullable, isString, toLowerCase } from "./deps.ts"; |
| 5 | +import { duplicate } from "./utils.ts"; |
| 6 | +import { Msg } from "./constants.ts"; |
| 7 | +import type { Authorization, AuthParams } from "./types.ts"; |
| 8 | + |
| 9 | +export interface AuthorizationLike |
| 10 | + extends |
| 11 | + Pick<Authorization, "authScheme">, |
| 12 | + Partial<Pick<Authorization, "params">> { |
| 13 | +} |
| 14 | + |
| 15 | +/** Serialize {@link AuthorizationLike} into string. |
| 16 | + * |
| 17 | + * @example |
| 18 | + * ```ts |
| 19 | + * import { stringifyAuthorization } from "https://deno.land/x/authorization_parser@$VERSION/stringify.ts"; |
| 20 | + * import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; |
| 21 | + * |
| 22 | + * assertEquals( |
| 23 | + * stringifyAuthorization({ authScheme: "Basic", params: "token68==" }), |
| 24 | + * "Basic token68", |
| 25 | + * ); |
| 26 | + * assertEquals( |
| 27 | + * stringifyAuthorization({ |
| 28 | + * authScheme: "Bearer", |
| 29 | + * params: { realm: `"Secure area"`, error: `"invalid_token"` }, |
| 30 | + * }), |
| 31 | + * `Bearer realm="Secure area", error="invalid_token"`, |
| 32 | + * ); |
| 33 | + * ``` |
| 34 | + * |
| 35 | + * @throws {Error} If the input is invalid {@link AuthorizationLike}. |
| 36 | + */ |
| 37 | +export function stringifyAuthorization(input: AuthorizationLike): string { |
| 38 | + assertToken(input.authScheme, `authScheme ${Msg.InvalidToken}`); |
| 39 | + |
| 40 | + if (isNullable(input.params)) return input.authScheme; |
| 41 | + |
| 42 | + const data = isString(input.params) |
| 43 | + ? (assertToken68(input.params, `token ${Msg.InvalidToken68}`), input.params) |
| 44 | + : stringifyAuthParams(input.params); |
| 45 | + |
| 46 | + return [input.authScheme, data].filter(Boolean).join(" "); |
| 47 | +} |
| 48 | + |
| 49 | +const reToken = /^[\w!#$%&'*+.^`|~-]+$/; |
| 50 | + |
| 51 | +export function assertToken( |
| 52 | + input: string, |
| 53 | + msg?: string, |
| 54 | + constructor: ErrorConstructor = Error, |
| 55 | +): asserts input { |
| 56 | + if (!isToken(input)) throw new constructor(msg); |
| 57 | +} |
| 58 | + |
| 59 | +export function isToken(input: string): boolean { |
| 60 | + return reToken.test(input); |
| 61 | +} |
| 62 | + |
| 63 | +const reToken68 = /^[A-Za-z\d+./_~-]+=*$/; |
| 64 | + |
| 65 | +export function isToken68(input: string): boolean { |
| 66 | + return reToken68.test(input); |
| 67 | +} |
| 68 | + |
| 69 | +export function assertToken68( |
| 70 | + input: string, |
| 71 | + msg?: string, |
| 72 | + constructor: ErrorConstructor = Error, |
| 73 | +): asserts input { |
| 74 | + if (!isToken68(input)) throw new constructor(msg); |
| 75 | +} |
| 76 | + |
| 77 | +const reQuotedString = |
| 78 | + /^"(?:\t| |!|[ \x23-\x5B\x5D-\x7E]|[\x80-\xFF]|\\(?:\t| |[\x21-\x7E])[\x80-\xFF])*"$/; |
| 79 | + |
| 80 | +export function isQuotedString(input: string): boolean { |
| 81 | + return reQuotedString.test(input); |
| 82 | +} |
| 83 | + |
| 84 | +function assertAuthParam(input: AuthParams): asserts input { |
| 85 | + for (const key in input) { |
| 86 | + assertToken(key, `token key ${Msg.InvalidToken}`); |
| 87 | + |
| 88 | + const value = input[key]!; |
| 89 | + |
| 90 | + if (isToken(value) || isQuotedString(value)) continue; |
| 91 | + |
| 92 | + throw Error(`token value should be <token> or <quoted-string> format`); |
| 93 | + } |
| 94 | + |
| 95 | + const duplicates = duplicate(Object.keys(input).map(toLowerCase)); |
| 96 | + |
| 97 | + if (duplicates.length) throw Error(Msg.DuplicatedKeys); |
| 98 | +} |
| 99 | + |
| 100 | +export function stringifyAuthParams(input: AuthParams): string { |
| 101 | + assertAuthParam(input); |
| 102 | + |
| 103 | + return Object |
| 104 | + .entries(input) |
| 105 | + .map(joinEntry) |
| 106 | + .join(", "); |
| 107 | +} |
| 108 | + |
| 109 | +function joinEntry( |
| 110 | + entry: readonly [string, string], |
| 111 | +): string { |
| 112 | + return entry[0] + "=" + entry[1]; |
| 113 | +} |
0 commit comments