Skip to content

Commit 0f40077

Browse files
fix(proxy): replace basic-auth-parser with built-in implementation (#418)
Remove the unmaintained `basic-auth-parser` dependency and replace it with a simple built-in parser. Removes a `require()` call and the `@ts-expect-error` suppression. Co-authored-by: clutz-bot[bot] <261721731+clutz-bot[bot]@users.noreply.github.com> Co-authored-by: jangolano <jangolano@users.noreply.github.com>
1 parent 31d7ef1 commit 0f40077

6 files changed

Lines changed: 50 additions & 11 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"proxy": patch
3+
---
4+
5+
Replace `basic-auth-parser` dependency with built-in implementation

packages/proxy/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
"license": "MIT",
4040
"dependencies": {
4141
"args": "^5.0.3",
42-
"basic-auth-parser": "0.0.2-1",
4342
"debug": "catalog:"
4443
},
4544
"devDependencies": {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export interface BasicAuthResult {
2+
scheme: string;
3+
username?: string;
4+
password?: string;
5+
}
6+
7+
export function basicAuthParser(auth: string): BasicAuthResult {
8+
const parts = auth.split(' ');
9+
const scheme = parts[0];
10+
if (scheme !== 'Basic') {
11+
return { scheme };
12+
}
13+
const decoded = Buffer.from(parts[1], 'base64').toString('utf-8');
14+
const colon = decoded.indexOf(':');
15+
const username = decoded.substring(0, colon);
16+
const password = decoded.substring(colon + 1);
17+
return { scheme, username, password };
18+
}

packages/proxy/src/bin/proxy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import args from 'args';
33
import createDebug from 'debug';
44
import { spawn } from 'child_process';
55
import { once } from 'events';
6-
// @ts-expect-error no types for "basic-auth-parser"
7-
import basicAuthParser from 'basic-auth-parser';
6+
import { basicAuthParser } from '../basic-auth-parser.js';
87
import { createProxy } from '../proxy.js';
98
//import pkg from '../pkg';
109

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { basicAuthParser } from '../src/basic-auth-parser';
2+
3+
describe('basicAuthParser', () => {
4+
it('should parse Basic auth header', () => {
5+
const result = basicAuthParser('Basic YWRtaW46cGFzc3dvcmQ=');
6+
expect(result).toEqual({
7+
scheme: 'Basic',
8+
username: 'admin',
9+
password: 'password',
10+
});
11+
});
12+
13+
it('should return only scheme for non-Basic auth', () => {
14+
const result = basicAuthParser('Digest DEADC0FFEE');
15+
expect(result).toEqual({ scheme: 'Digest' });
16+
});
17+
18+
it('should handle colons in password', () => {
19+
const result = basicAuthParser('Basic YWRtaW46cGFzczp3b3Jk');
20+
expect(result).toEqual({
21+
scheme: 'Basic',
22+
username: 'admin',
23+
password: 'pass:word',
24+
});
25+
});
26+
});

pnpm-lock.yaml

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)