File tree Expand file tree Collapse file tree 2 files changed +32
-21
lines changed
Expand file tree Collapse file tree 2 files changed +32
-21
lines changed Original file line number Diff line number Diff line change @@ -61,20 +61,18 @@ namespace auth {
6161
6262 // parse header
6363 const match = CREDENTIALS_REGEXP . exec ( string ) ;
64-
65- if ( ! match ) {
66- return undefined ;
67- }
64+ if ( ! match ) return undefined ;
6865
6966 // decode user pass
70- const userPass = USER_PASS_REGEXP . exec ( decodeBase64 ( match [ 1 ] ) ) ;
71-
72- if ( ! userPass ) {
73- return undefined ;
74- }
67+ const userPass = decodeBase64 ( match [ 1 ] ) ;
68+ const colonIndex = userPass . indexOf ( ':' ) ;
69+ if ( colonIndex === - 1 ) return undefined ;
7570
7671 // return credentials object
77- return new CredentialsImpl ( userPass [ 1 ] , userPass [ 2 ] ) ;
72+ return new CredentialsImpl (
73+ userPass . slice ( 0 , colonIndex ) ,
74+ userPass . slice ( colonIndex + 1 ) ,
75+ ) ;
7876 }
7977}
8078
@@ -90,17 +88,6 @@ namespace auth {
9088const CREDENTIALS_REGEXP =
9189 / ^ * (?: [ B b ] [ A a ] [ S s ] [ I i ] [ C c ] ) + ( [ A - Z a - z 0 - 9 . _ ~ + / - ] + = * ) * $ / ;
9290
93- /**
94- * RegExp for basic auth user/pass
95- *
96- * user-pass = userid ":" password
97- * userid = *<TEXT excluding ":">
98- * password = *TEXT
99- * @private
100- */
101-
102- const USER_PASS_REGEXP = / ^ ( [ ^ : ] * ) : ( .* ) $ / ;
103-
10491/**
10592 * Decode base64 string.
10693 * @private
Original file line number Diff line number Diff line change 1+ import { describe , bench } from 'vitest' ;
2+ import { parse } from './index' ;
3+
4+ describe ( 'parse' , ( ) => {
5+ bench ( 'basic auth header' , ( ) => {
6+ const header = 'Basic dGVzdOnBhc3N3b3Jk' ; // "test:password" in base64
7+ parse ( header ) ;
8+ } ) ;
9+
10+ bench ( 'basic auth header with extra whitespace' , ( ) => {
11+ const header = ' Basic dGVzdOnBhc3N3b3Jk ' ; // "test:password" in base64 with extra whitespace
12+ parse ( header ) ;
13+ } ) ;
14+
15+ bench ( 'invalid basic auth header' , ( ) => {
16+ const header = 'Basic invalidbase64' ; // Invalid base64 string
17+ parse ( header ) ;
18+ } ) ;
19+
20+ bench ( 'non-basic auth header' , ( ) => {
21+ const header = 'Bearer sometoken' ; // Not a basic auth header
22+ parse ( header ) ;
23+ } ) ;
24+ } ) ;
You can’t perform that action at this time.
0 commit comments