-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
50 lines (44 loc) · 1.22 KB
/
index.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
45
46
47
48
49
50
import { ChannelCredentials, createChannel, createClientFactory } from 'nice-grpc';
import { AuthenticationInterceptor, AuthenticationManager } from './interceptor/Authentication';
import {
ApiClient,
ApiDefinition,
} from './lib/api';
export type IsAllowedRequest = {
principal: string
resourceKind: string
resourceValue: string
action: string
}
export class Client {
public stub: ApiClient;
constructor(address: string, clientId: string, clientSecret: string) {
const channel = createChannel(address, ChannelCredentials.createInsecure());
const authenticationManager = new AuthenticationManager(clientId, clientSecret, channel);
this.stub = createClientFactory()
.use(AuthenticationInterceptor(authenticationManager))
.create(ApiDefinition, channel);
}
public async isAllowed({
principal,
resourceKind,
resourceValue,
action,
}: IsAllowedRequest): Promise<boolean> {
try {
const response = await this.stub.check({
checks: [
{
principal,
resourceKind,
resourceValue,
action,
},
],
});
return response.checks[0].isAllowed;
} catch (error) {
return false;
}
}
}