-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPlainHttpAuthentication.ts
35 lines (29 loc) · 1.09 KB
/
PlainHttpAuthentication.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import IAuthentication from "../contracts/IAuthentication";
import ITransport from "../contracts/ITransport";
import { AuthOptions } from '../types/AuthOptions';
type HttpAuthOptions = AuthOptions & {
headers?: object
};
export default class PlainHttpAuthentication implements IAuthentication {
private username: string;
private password: string;
private headers: object;
constructor(options: HttpAuthOptions) {
this.username = options?.username || 'anonymous';
this.password = options?.password !== undefined ? options?.password : 'anonymous';
this.headers = options?.headers || {};
}
authenticate(transport: ITransport): Promise<ITransport> {
transport.setOptions('headers', {
...(this.headers),
Authorization: this.getToken(
this.username,
this.password,
)
});
return Promise.resolve(transport);
}
private getToken(username: string, password: string): string {
return 'Basic ' + Buffer.from(`${username}:${password}`).toString('base64');
}
}