-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BouncerClient.ts
44 lines (35 loc) · 1.43 KB
/
BouncerClient.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
36
37
38
39
40
41
42
43
44
import { CrowdSecClient } from './CrowdSecClient.js';
import { createDebugger } from '../utils.js';
import { IBouncerAuthentication, IBouncerClientOptions, ITLSAuthentication } from '../interfaces/index.js';
import { DecisionsBouncer } from '../Decisions/DecisionsBouncer.js';
import Validate from '../Validate.js';
import { CrowdsecClientError } from '../Errors/CrowdsecClientError.js';
export class BouncerClient extends CrowdSecClient {
protected debug = createDebugger('bouncerClient');
public Decisions: DecisionsBouncer;
#auth?: IBouncerAuthentication;
constructor(options: IBouncerClientOptions) {
super(options);
if (!options?.auth) {
throw new CrowdsecClientError('options.auth is needed when creating a bouncer client');
}
if (Validate.implementsTKeys<ITLSAuthentication>(options.auth, ['key', 'cert', 'ca'])) {
this.setAuthenticationByTLS(options.auth);
} else {
this.#auth = options.auth;
this.setAuthenticationHeaders({
'X-Api-Key': this.#auth.apiKey
});
}
this.Decisions = new DecisionsBouncer({ httpClient: this.http });
}
public async login(): Promise<void> {
return this.testConnection();
}
public async testConnection(): Promise<void> {
return this._testConnection('/v1/decisions');
}
public async stop() {
this.Decisions.stop();
}
}