Skip to content

Commit ed82293

Browse files
committed
remove default auth method
1 parent b18e1a2 commit ed82293

3 files changed

Lines changed: 39 additions & 233 deletions

File tree

src/auth.spec.ts

Lines changed: 0 additions & 139 deletions
This file was deleted.

src/index.ts

Lines changed: 25 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -8,74 +8,43 @@
88

99
import { Buffer } from 'node:buffer';
1010

11-
export = auth;
11+
/**
12+
* Object to represent user credentials.
13+
*/
14+
export interface Credentials {
15+
name: string;
16+
pass: string;
17+
}
1218

1319
/**
14-
* Parse the Authorization header field of a request
20+
* Parse basic auth to object.
21+
*
22+
* @param {string} string
23+
* @return {object}
1524
* @public
1625
*/
1726

18-
function auth(req: auth.IncomingMessageLike): auth.Credentials | undefined {
19-
if (!req) {
20-
throw new TypeError('argument req is required');
27+
export function parse(string: string): Credentials | undefined {
28+
if (typeof string !== 'string') {
29+
return undefined;
2130
}
2231

23-
if (typeof req !== 'object') {
24-
throw new TypeError('argument req is required to be an object');
25-
}
26-
27-
// get header
28-
const header = getAuthorization(req);
29-
3032
// parse header
31-
return auth.parse(header ?? '');
32-
}
33+
const match = CREDENTIALS_REGEXP.exec(string);
3334

34-
namespace auth {
35-
/**
36-
* Object to represent user credentials.
37-
*/
38-
export interface Credentials {
39-
name: string;
40-
pass: string;
35+
if (!match) {
36+
return undefined;
4137
}
4238

43-
export interface IncomingMessageLike {
44-
headers?: {
45-
authorization?: string;
46-
};
47-
}
48-
49-
/**
50-
* Parse basic auth to object.
51-
*
52-
* @param {string} string
53-
* @return {object}
54-
* @public
55-
*/
56-
57-
export function parse(string: string): auth.Credentials | undefined {
58-
if (typeof string !== 'string') {
59-
return undefined;
60-
}
39+
// decode user pass
40+
const userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1]));
6141

62-
// parse header
63-
const match = CREDENTIALS_REGEXP.exec(string);
64-
65-
if (!match) {
66-
return undefined;
67-
}
68-
69-
// decode user pass
70-
const userPass = USER_PASS_REGEXP.exec(decodeBase64(match[1]));
71-
72-
if (!userPass) {
73-
return undefined;
74-
}
75-
76-
// return credentials object
77-
return new CredentialsImpl(userPass[1], userPass[2]);
42+
if (!userPass) {
43+
return undefined;
7844
}
45+
46+
// return credentials object
47+
return new CredentialsImpl(userPass[1], userPass[2]);
7948
}
8049

8150
/**
@@ -110,20 +79,7 @@ function decodeBase64(str: string): string {
11079
return Buffer.from(str, 'base64').toString();
11180
}
11281

113-
/**
114-
* Get the Authorization header from request object.
115-
* @private
116-
*/
117-
118-
function getAuthorization(req: auth.IncomingMessageLike) {
119-
if (!req.headers || typeof req.headers !== 'object') {
120-
throw new TypeError('argument req is required to have headers property');
121-
}
122-
123-
return req.headers.authorization;
124-
}
125-
126-
class CredentialsImpl implements auth.Credentials {
82+
class CredentialsImpl implements Credentials {
12783
name: string;
12884
pass: string;
12985

src/parse.spec.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,90 @@
11
import { describe, it, assert } from 'vitest';
2-
import auth from './index';
2+
import { parse } from './index';
33

4-
function request(authorization: string) {
5-
return {
6-
headers: {
7-
authorization: authorization,
8-
},
9-
};
10-
}
11-
12-
describe('auth.parse(string)', function () {
4+
describe('parse(string)', function () {
135
describe('with undefined string', function () {
146
it('should return undefined', function () {
15-
assert.strictEqual((auth as any).parse(), undefined);
7+
assert.strictEqual((parse as any)(), undefined);
168
});
179
});
1810

1911
describe('with malformed string', function () {
2012
it('should return undefined', function () {
21-
assert.strictEqual(auth.parse('Something'), undefined);
13+
assert.strictEqual(parse('Something'), undefined);
2214
});
2315
});
2416

2517
describe('with malformed scheme', function () {
2618
it('should return undefined', function () {
27-
assert.strictEqual(auth.parse('basic_Zm9vOmJhcg=='), undefined);
19+
assert.strictEqual(parse('basic_Zm9vOmJhcg=='), undefined);
2820
});
2921
});
3022

3123
describe('with malformed credentials', function () {
3224
it('should return undefined', function () {
33-
assert.strictEqual(auth.parse('basic Zm9vcgo='), undefined);
25+
assert.strictEqual(parse('basic Zm9vcgo='), undefined);
3426
});
3527
});
3628

3729
describe('with valid credentials', function () {
3830
it('should return .name and .pass', function () {
39-
var creds = auth.parse('basic Zm9vOmJhcg==');
31+
var creds = parse('basic Zm9vOmJhcg==');
4032
assert.strictEqual(creds?.name, 'foo');
4133
assert.strictEqual(creds?.pass, 'bar');
4234
});
4335
});
4436

4537
describe('with empty password', function () {
4638
it('should return .name and .pass', function () {
47-
var creds = auth.parse('basic Zm9vOg==');
39+
var creds = parse('basic Zm9vOg==');
4840
assert.strictEqual(creds?.name, 'foo');
4941
assert.strictEqual(creds?.pass, '');
5042
});
5143
});
5244

5345
describe('with empty userid', function () {
5446
it('should return .name and .pass', function () {
55-
var creds = auth.parse('basic OnBhc3M=');
47+
var creds = parse('basic OnBhc3M=');
5648
assert.strictEqual(creds?.name, '');
5749
assert.strictEqual(creds?.pass, 'pass');
5850
});
5951
});
6052

6153
describe('with empty userid and pass', function () {
6254
it('should return .name and .pass', function () {
63-
var creds = auth.parse('basic Og==');
55+
var creds = parse('basic Og==');
6456
assert.strictEqual(creds?.name, '');
6557
assert.strictEqual(creds?.pass, '');
6658
});
6759
});
6860

6961
describe('with colon in pass', function () {
7062
it('should return .name and .pass', function () {
71-
var creds = auth.parse('basic Zm9vOnBhc3M6d29yZA==');
63+
var creds = parse('basic Zm9vOnBhc3M6d29yZA==');
7264
assert.strictEqual(creds?.name, 'foo');
7365
assert.strictEqual(creds?.pass, 'pass:word');
7466
});
7567
});
7668

7769
describe('with scheme "Basic"', function () {
7870
it('should return .name and .pass', function () {
79-
var req = request('Basic Zm9vOmJhcg==');
80-
var creds = auth(req);
71+
var creds = parse('Basic Zm9vOmJhcg==');
8172
assert.strictEqual(creds?.name, 'foo');
8273
assert.strictEqual(creds?.pass, 'bar');
8374
});
8475
});
8576

8677
describe('with scheme "BASIC"', function () {
8778
it('should return .name and .pass', function () {
88-
var req = request('BASIC Zm9vOmJhcg==');
89-
var creds = auth(req);
79+
var creds = parse('BASIC Zm9vOmJhcg==');
9080
assert.strictEqual(creds?.name, 'foo');
9181
assert.strictEqual(creds?.pass, 'bar');
9282
});
9383
});
9484

9585
describe('with scheme "BaSiC"', function () {
9686
it('should return .name and .pass', function () {
97-
var req = request('BaSiC Zm9vOmJhcg==');
98-
var creds = auth(req);
87+
var creds = parse('BaSiC Zm9vOmJhcg==');
9988
assert.strictEqual(creds?.name, 'foo');
10089
assert.strictEqual(creds?.pass, 'bar');
10190
});

0 commit comments

Comments
 (0)