-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathHTTPAuth.ts
More file actions
36 lines (28 loc) · 773 Bytes
/
HTTPAuth.ts
File metadata and controls
36 lines (28 loc) · 773 Bytes
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
36
import fetch from 'node-fetch';
import { IAuthentication } from '../interfaces/Authentication.js';
import { Logger } from '../logger/Logger.js';
interface IHTTPAuthOptions {
url: string;
}
export class HTTPAuth implements IAuthentication {
private logger = new Logger('HTTPAuth');
private url: string;
constructor(config: IHTTPAuthOptions) {
this.url = config.url;
}
async authenticate(username: string, password: string) {
const result = await fetch(this.url, {
method: 'post',
body: JSON.stringify({
username,
password,
}),
headers: { 'Content-Type': 'application/json' },
});
if (result.status === 200) {
return true;
}
this.logger.log(`HTTP authentication failed, response code: ${result.status}`);
return false;
}
}