-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathrbac-api.ts
More file actions
135 lines (114 loc) · 3.89 KB
/
rbac-api.ts
File metadata and controls
135 lines (114 loc) · 3.89 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import {
APIRequestContext,
APIResponse,
Page,
request,
} from "@playwright/test";
import playwrightConfig from "../../../playwright.config";
import { Policy, Role } from "./rbac-api-structures";
import { RhdhAuthApiHack } from "./rhdh-auth-api-hack";
export default class RhdhRbacApi {
private readonly apiUrl = playwrightConfig.use.baseURL + "/api/permission/";
private readonly authHeader: {
Accept: "application/json";
Authorization: string;
};
private myContext: APIRequestContext;
private readonly roleRegex = /^[a-zA-Z0-9_-]+\/[a-zA-Z0-9_-]+$/;
private constructor(private readonly token: string) {
this.authHeader = {
Accept: "application/json",
Authorization: `Bearer ${this.token}`,
};
}
public static async build(token: string): Promise<RhdhRbacApi> {
const instance = new RhdhRbacApi(token);
instance.myContext = await request.newContext({
baseURL: instance.apiUrl,
extraHTTPHeaders: instance.authHeader,
});
return instance;
}
//Roles:
public async getRoles(): Promise<APIResponse> {
return await this.myContext.get("roles");
}
public async getRole(role: string): Promise<APIResponse> {
return await this.myContext.get(`roles/role/${role}`);
}
public async updateRole(
role: string /* shall be like: default/admin */,
oldRole: Role,
newRole: Role,
): Promise<APIResponse> {
this.checkRoleFormat(role);
return await this.myContext.put(`roles/role/${role}`, {
data: { oldRole, newRole },
});
}
public async createRoles(role: Role): Promise<APIResponse> {
return await this.myContext.post("roles", { data: role });
}
public async deleteRole(role: string): Promise<APIResponse> {
return await this.myContext.delete(`roles/role/${role}`);
}
//Policies:
public async getPolicies(): Promise<APIResponse> {
return await this.myContext.get("policies");
}
public async getPoliciesByRole(policy: string): Promise<APIResponse> {
return await this.myContext.get(`policies/role/${policy}`);
}
public async getPoliciesByQuery(
params: string | { [key: string]: string | number | boolean },
): Promise<APIResponse> {
return await this.myContext.get("policies", { params });
}
public async createPolicies(policy: Policy[]): Promise<APIResponse> {
return await this.myContext.post("policies", { data: policy });
}
public async updatePolicy(
role: string /* shall be like: default/admin */,
oldPolicy: Policy[],
newPolicy: Policy[],
): Promise<APIResponse> {
this.checkRoleFormat(role);
return await this.myContext.put(`policies/role/${role}`, {
data: { oldPolicy, newPolicy },
});
}
public async deletePolicy(policy: string, policies: Policy[]) {
this.checkRoleFormat(policy);
return await this.myContext.delete(`policies/role/${policy}`, {
data: policies,
});
}
// Conditions
public async getConditions(): Promise<APIResponse> {
return await this.myContext.get("roles/conditions");
}
public async getConditionByQuery(
params: string | { [key: string]: string | number | boolean },
): Promise<APIResponse> {
return await this.myContext.get("roles/conditions", { params });
}
public async getConditionById(id: number): Promise<APIResponse> {
return await this.myContext.get(`roles/conditions/${id}`);
}
public async deleteConditionById(id: number): Promise<APIResponse> {
return await this.myContext.delete(`roles/conditions/${id}`);
}
public async dispose(): Promise<void> {
await this.myContext.dispose();
}
private checkRoleFormat(role: string) {
if (!this.roleRegex.test(role))
throw Error(
"roles passed to the Rbac api must have format like: default/admin",
);
}
public static async buildRbacApi(page: Page): Promise<RhdhRbacApi> {
const token = await RhdhAuthApiHack.getToken(page);
return RhdhRbacApi.build(token);
}
}