11// Copyright 2023-latest the httpland authors. All rights reserved. MIT license.
22// This module is browser compatible.
33
4- import { duplicate } from "./utils.ts" ;
5- import { head , isString , parseListFields , toLowerCase } from "./deps.ts" ;
4+ import { divideWhile , duplicate , isToken68 , trimStartBy } from "./utils.ts" ;
5+ import {
6+ head ,
7+ isString ,
8+ isToken ,
9+ parseListFields ,
10+ toLowerCase ,
11+ } from "./deps.ts" ;
612import { Msg } from "./constants.ts" ;
713import type { Authorization , AuthParams } from "./types.ts" ;
814
9- /** Generate from _abnf.ts. */
10- const reAuthorization =
11- / ^ (?< authScheme > (? = ( [ \w ! # $ % & ' * + . ^ ` | ~ - ] + ) ) \2) (?: (? = ( + ) ) \3(?: (?< token68 > (? = ( (?: [ A - Z a - z ] | \d | [ + . / _ ~ - ] ) + ) ) \5(? = ( = * ) ) \6) | (?< authParam > (? = ( .* ) ) \8) ) ) ? $ / ;
12-
1315/** Parse string into {@link Authorization}.
1416 *
1517 * @example
@@ -39,24 +41,28 @@ const reAuthorization =
3941 * @throws {Error } If the auth param key is duplicated.
4042 */
4143export function parseAuthorization ( input : string ) : Authorization {
42- const result = reAuthorization . exec ( input ) ;
44+ const result = divideWhile ( input , isToken ) ;
4345
44- if ( ! result || ! result . groups ) throw SyntaxError ( Msg . InvalidSyntax ) ;
46+ if ( ! result ) throw new SyntaxError ( Msg . InvalidSyntax ) ;
4547
46- const groups = result . groups as ParsedGroups ;
47- const { authScheme } = groups ;
48- const params = isString ( groups . authParam )
49- ? parseAuthParams ( groups . authParam )
50- : groups . token68 ;
48+ const [ authScheme , rest ] = result ;
5149
52- return { authScheme, params : params ?? null } ;
53- }
50+ // challenge = auth-scheme
51+ if ( ! rest ) return { authScheme, params : null } ;
52+ if ( ! rest . startsWith ( " " ) ) throw new SyntaxError ( Msg . InvalidSyntax ) ;
53+
54+ const maybeToken68OrAuthParam = trimStartBy ( rest , " " ) ;
5455
55- type ParsedGroups = {
56- readonly authScheme : string ;
57- readonly token68 : string | undefined ;
58- readonly authParam : string | undefined ;
59- } ;
56+ // challenge = auth-scheme [ 1*SP ( token68 ) ]
57+ if ( isToken68 ( maybeToken68OrAuthParam ) ) {
58+ return { authScheme, params : maybeToken68OrAuthParam } ;
59+ }
60+
61+ // challenge = auth-scheme [ 1*SP ( #auth-param ) ]
62+ const params = parseAuthParams ( maybeToken68OrAuthParam ) ;
63+
64+ return { authScheme, params } ;
65+ }
6066
6167/** Generate from _abnf.ts. */
6268const reAuthParam =
@@ -79,7 +85,7 @@ export function parseAuthParams(input: string): AuthParams {
7985 const entries = list . map ( ( el ) => {
8086 const result = reAuthParam . exec ( el ) ;
8187
82- if ( ! result || ! result . groups ) throw SyntaxError ( Msg . InvalidSyntax ) ;
88+ if ( ! result || ! result . groups ) throw new SyntaxError ( Msg . InvalidSyntax ) ;
8389
8490 const groups = result . groups as AuthParamGroups ;
8591 const value = isString ( groups . token )
0 commit comments