Skip to content

Commit 434e10e

Browse files
committed
Map warrants to correct params before making access check request
1 parent 5fe2620 commit 434e10e

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

src/modules/Authorization.ts

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import Feature from "./Feature";
22
import Permission from "./Permission";
3-
import Check, { AccessCheckRequest, CheckMany, CheckWarrantRequest, FeatureCheck, PermissionCheck } from "../types/Check";
3+
import Check, { AccessCheckRequest, CheckMany, CheckWarrant, FeatureCheck, PermissionCheck } from "../types/Check";
44
import Warrant, { isSubject, isWarrantObject } from "../types/Warrant";
55
import WarrantClient from "../WarrantClient";
66

77
export default class Authorization {
88
public static async check(check: Check): Promise<boolean> {
99
const accessCheckRequest: AccessCheckRequest = {
1010
warrants: [{
11-
objectType: isWarrantObject(check.object) ? check.object.getObjectType() : check.object.objectType,
12-
objectId: isWarrantObject(check.object) ? check.object.getObjectId() : check.object.objectId,
11+
object: check.object,
1312
relation: check.relation,
14-
subject: isSubject(check.subject) ? check.subject : { objectType: check.subject.getObjectType(), objectId: check.subject.getObjectId() },
13+
subject: check.subject,
1514
context: check.context
1615
}],
1716
debug: check.debug
@@ -24,12 +23,11 @@ export default class Authorization {
2423
}
2524

2625
public static async checkMany(check: CheckMany): Promise<boolean> {
27-
let warrants: CheckWarrantRequest[] = check.warrants.map((warrant) => {
26+
let warrants: CheckWarrant[] = check.warrants.map((warrant) => {
2827
return {
29-
objectType: isWarrantObject(warrant.object) ? warrant.object.getObjectType() : warrant.object.objectType,
30-
objectId: isWarrantObject(warrant.object) ? warrant.object.getObjectId() : warrant.object.objectId,
28+
object: warrant.object,
3129
relation: warrant.relation,
32-
subject: isSubject(warrant.subject) ? warrant.subject : { objectType: warrant.subject.getObjectType(), objectId: warrant.subject.getObjectId() },
30+
subject: warrant.subject,
3331
context: warrant.context
3432
}
3533
})
@@ -69,9 +67,13 @@ export default class Authorization {
6967
// Private methods
7068
private static async authorize(accessCheckRequest: AccessCheckRequest): Promise<boolean> {
7169
try {
70+
7271
const response = await WarrantClient.httpClient.post({
7372
url: "/v2/authorize",
74-
data: accessCheckRequest,
73+
data: {
74+
...accessCheckRequest,
75+
warrants: this.mapWarrantsForRequest(accessCheckRequest.warrants),
76+
},
7577
});
7678

7779
return response.code === 200;
@@ -80,21 +82,36 @@ export default class Authorization {
8082
}
8183
}
8284

83-
private static async edgeAuthorize(warrantCheck: AccessCheckRequest): Promise<boolean> {
85+
private static async edgeAuthorize(accessCheckRequest: AccessCheckRequest): Promise<boolean> {
8486
try {
8587
const response = await WarrantClient.httpClient.post({
8688
baseUrl: WarrantClient.config.authorizeEndpoint,
8789
url: "/v2/authorize",
88-
data: warrantCheck,
90+
data: {
91+
...accessCheckRequest,
92+
warrants: this.mapWarrantsForRequest(accessCheckRequest.warrants),
93+
},
8994
});
9095

9196
return response.code === 200;
9297
} catch (e) {
9398
if (e.code === "cache_not_ready") {
94-
return this.authorize(warrantCheck);
99+
return this.authorize(accessCheckRequest);
95100
}
96101

97102
throw e;
98103
}
99104
}
105+
106+
private static mapWarrantsForRequest(warrants: CheckWarrant[]): any[] {
107+
return warrants.map((warrant) => {
108+
return {
109+
objectType: isWarrantObject(warrant.object) ? warrant.object.getObjectType() : warrant.object.objectType,
110+
objectId: isWarrantObject(warrant.object) ? warrant.object.getObjectId() : warrant.object.objectId,
111+
relation: warrant.relation,
112+
subject: isSubject(warrant.subject) ? warrant.subject : { objectType: warrant.subject.getObjectType(), objectId: warrant.subject.getObjectId() },
113+
context: warrant.context
114+
}
115+
})
116+
}
100117
}

src/types/Check.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,8 @@ export interface PermissionCheck {
3636
debug?: boolean;
3737
}
3838

39-
export interface CheckWarrantRequest {
40-
objectType: string;
41-
objectId: string;
42-
relation: string;
43-
subject: WarrantObject | Subject;
44-
context?: PolicyContext;
45-
}
46-
4739
export interface AccessCheckRequest {
4840
op?: CheckOp;
49-
warrants: CheckWarrantRequest[];
41+
warrants: CheckWarrant[];
5042
debug?: boolean;
5143
}

0 commit comments

Comments
 (0)