Skip to content

Commit 4a21d9a

Browse files
authored
Merge pull request #37 from warrant-dev/feat/AddPolicySupport
Add policy support for creating / deleting warrants
2 parents 139a153 + 434e10e commit 4a21d9a

File tree

12 files changed

+115
-47
lines changed

12 files changed

+115
-47
lines changed

src/modules/Authorization.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
import Feature from "./Feature";
22
import Permission from "./Permission";
3-
import Check, { AccessCheckRequest, CheckMany, 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
}],
17-
consistentRead: check.consistentRead,
1816
debug: check.debug
1917
}
2018
if (WarrantClient.config.authorizeEndpoint) {
@@ -25,19 +23,17 @@ export default class Authorization {
2523
}
2624

2725
public static async checkMany(check: CheckMany): Promise<boolean> {
28-
let warrants: Warrant[] = check.warrants.map((warrant) => {
26+
let warrants: CheckWarrant[] = check.warrants.map((warrant) => {
2927
return {
30-
objectType: isWarrantObject(warrant.object) ? warrant.object.getObjectType() : warrant.object.objectType,
31-
objectId: isWarrantObject(warrant.object) ? warrant.object.getObjectId() : warrant.object.objectId,
28+
object: warrant.object,
3229
relation: warrant.relation,
33-
subject: isSubject(warrant.subject) ? warrant.subject : { objectType: warrant.subject.getObjectType(), objectId: warrant.subject.getObjectId() },
30+
subject: warrant.subject,
3431
context: warrant.context
3532
}
3633
})
3734
const accessCheckRequest: AccessCheckRequest = {
3835
op: check.op,
3936
warrants: warrants,
40-
consistentRead: check.consistentRead,
4137
debug: check.debug
4238
}
4339

@@ -54,7 +50,6 @@ export default class Authorization {
5450
relation: "member",
5551
subject: featureCheck.subject,
5652
context: featureCheck.context,
57-
consistentRead: featureCheck.consistentRead,
5853
debug: featureCheck.debug
5954
})
6055
}
@@ -65,17 +60,20 @@ export default class Authorization {
6560
relation: "member",
6661
subject: permissionCheck.subject,
6762
context: permissionCheck.context,
68-
consistentRead: permissionCheck.consistentRead,
6963
debug: permissionCheck.debug
7064
})
7165
}
7266

7367
// Private methods
7468
private static async authorize(accessCheckRequest: AccessCheckRequest): Promise<boolean> {
7569
try {
70+
7671
const response = await WarrantClient.httpClient.post({
7772
url: "/v2/authorize",
78-
data: accessCheckRequest,
73+
data: {
74+
...accessCheckRequest,
75+
warrants: this.mapWarrantsForRequest(accessCheckRequest.warrants),
76+
},
7977
});
8078

8179
return response.code === 200;
@@ -84,21 +82,36 @@ export default class Authorization {
8482
}
8583
}
8684

87-
private static async edgeAuthorize(warrantCheck: AccessCheckRequest): Promise<boolean> {
85+
private static async edgeAuthorize(accessCheckRequest: AccessCheckRequest): Promise<boolean> {
8886
try {
8987
const response = await WarrantClient.httpClient.post({
9088
baseUrl: WarrantClient.config.authorizeEndpoint,
9189
url: "/v2/authorize",
92-
data: warrantCheck,
90+
data: {
91+
...accessCheckRequest,
92+
warrants: this.mapWarrantsForRequest(accessCheckRequest.warrants),
93+
},
9394
});
9495

9596
return response.code === 200;
9697
} catch (e) {
9798
if (e.code === "cache_not_ready") {
98-
return this.authorize(warrantCheck);
99+
return this.authorize(accessCheckRequest);
99100
}
100101

101102
throw e;
102103
}
103104
}
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+
}
104117
}

src/modules/PricingTier.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import WarrantClient from "../WarrantClient";
55
import { ListFeatureOptions } from "../types/Feature";
66
import { ObjectType } from "../types/ObjectType";
77
import { CreatePricingTierParams, ListPricingTierOptions } from "../types/PricingTier";
8-
import Warrant, { Context, WarrantObject } from "../types/Warrant";
8+
import Warrant, { PolicyContext, WarrantObject } from "../types/Warrant";
99

1010
export default class PricingTier implements WarrantObject {
1111
pricingTierId: string;
@@ -160,7 +160,7 @@ export default class PricingTier implements WarrantObject {
160160
return Feature.removeFeatureFromPricingTier(this.pricingTierId, featureId);
161161
}
162162

163-
public async hasFeature(featureId: string, context: Context = {}): Promise<boolean> {
163+
public async hasFeature(featureId: string, context: PolicyContext = {}): Promise<boolean> {
164164
return Authorization.hasFeature({ featureId: featureId, subject: { objectType: ObjectType.PricingTier, objectId: this.pricingTierId }, context: context });
165165
}
166166

src/modules/Role.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import WarrantClient from "../WarrantClient";
55
import { ObjectType } from "../types/ObjectType";
66
import { ListPermissionOptions } from "../types/Permission";
77
import { CreateRoleParams, ListRoleOptions, UpdateRoleParams } from "../types/Role";
8-
import Warrant, { Context, WarrantObject } from "../types/Warrant";
8+
import Warrant, { PolicyContext, WarrantObject } from "../types/Warrant";
99

1010
export default class Role implements WarrantObject {
1111
roleId: string;
@@ -136,7 +136,7 @@ export default class Role implements WarrantObject {
136136
return Permission.removePermissionFromRole(this.roleId, permissionId);
137137
}
138138

139-
public async hasPermission(permissionId: string, context: Context = {}): Promise<boolean> {
139+
public async hasPermission(permissionId: string, context: PolicyContext = {}): Promise<boolean> {
140140
return Authorization.hasPermission({ permissionId: permissionId, subject: { objectType: ObjectType.Role, objectId: this.roleId }, context: context });
141141
}
142142

src/modules/Tenant.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { ObjectType } from "../types/ObjectType";
99
import { ListPricingTierOptions } from "../types/PricingTier";
1010
import { CreateTenantParams, ListTenantOptions, UpdateTenantParams } from "../types/Tenant";
1111
import { ListUserOptions } from "../types/User";
12-
import { Context, WarrantObject } from "../types/Warrant";
12+
import { PolicyContext, WarrantObject } from "../types/Warrant";
1313

1414
export default class Tenant implements WarrantObject {
1515
// Tenant properties
@@ -150,7 +150,7 @@ export default class Tenant implements WarrantObject {
150150
return Feature.removeFeatureFromTenant(this.tenantId, featureId);
151151
}
152152

153-
public async hasFeature(featureId: string, context: Context = {}): Promise<boolean> {
153+
public async hasFeature(featureId: string, context: PolicyContext = {}): Promise<boolean> {
154154
return Authorization.hasFeature({ featureId: featureId, subject: { objectType: ObjectType.Tenant, objectId: this.tenantId }, context: context });
155155
}
156156

src/modules/User.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { ListPricingTierOptions } from "../types/PricingTier";
1313
import { ListRoleOptions } from "../types/Role";
1414
import { CreateUserParams, ListUserOptions, UpdateUserParams } from "../types/User";
1515
import { ListTenantOptions } from "../types/Tenant";
16-
import { Context, WarrantObject } from "../types/Warrant";
16+
import { PolicyContext, WarrantObject } from "../types/Warrant";
1717
import WarrantModule from "./WarrantModule";
1818

1919
export default class User implements WarrantObject {
@@ -174,7 +174,7 @@ export default class User implements WarrantObject {
174174
return Permission.removePermissionFromUser(this.userId, permissionId);
175175
}
176176

177-
public async hasPermission(permissionId: string, context: Context = {}): Promise<boolean> {
177+
public async hasPermission(permissionId: string, context: PolicyContext = {}): Promise<boolean> {
178178
return Authorization.hasPermission({ permissionId: permissionId, subject: { objectType: ObjectType.User, objectId: this.userId }, context: context });
179179
}
180180

@@ -202,7 +202,7 @@ export default class User implements WarrantObject {
202202
return Feature.removeFeatureFromUser(this.userId, featureId);
203203
}
204204

205-
public async hasFeature(featureId: string, context: Context = {}): Promise<boolean> {
205+
public async hasFeature(featureId: string, context: PolicyContext = {}): Promise<boolean> {
206206
return Authorization.hasFeature({ featureId: featureId, subject: { objectType: ObjectType.User, objectId: this.userId }, context: context });
207207
}
208208

src/modules/WarrantModule.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default class WarrantModule {
1212
objectId: isWarrantObject(warrant.object) ? warrant.object.getObjectId() : warrant.object.objectId,
1313
relation: warrant.relation,
1414
subject: isSubject(warrant.subject) ? warrant.subject : { objectType: warrant.subject.getObjectType(), objectId: warrant.subject.getObjectId() },
15-
context: warrant.context
15+
policy: warrant.policy
1616
},
1717
});
1818
} catch (e) {
@@ -29,7 +29,7 @@ export default class WarrantModule {
2929
objectId: isWarrantObject(warrant.object) ? warrant.object.getObjectId() : warrant.object.objectId,
3030
relation: warrant.relation,
3131
subject: isSubject(warrant.subject) ? warrant.subject : { objectType: warrant.subject.getObjectType(), objectId: warrant.subject.getObjectId() },
32-
context: warrant.context
32+
policy: warrant.policy
3333
},
3434
});
3535
} catch (e) {

src/types/Check.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Warrant, { Context, Subject, WarrantObject, WarrantObjectLiteral } from "./Warrant";
1+
import Warrant, { PolicyContext, Subject, WarrantObject, WarrantObjectLiteral } from "./Warrant";
22

33
export enum CheckOp {
44
AllOf = "allOf",
@@ -9,40 +9,35 @@ export interface CheckWarrant {
99
object: WarrantObject | WarrantObjectLiteral;
1010
relation: string;
1111
subject: WarrantObject | Subject;
12-
context?: Context;
12+
context?: PolicyContext;
1313
}
1414

1515
export default interface Check extends CheckWarrant {
16-
consistentRead?: boolean;
1716
debug?: boolean;
1817
}
1918

2019
export interface CheckMany {
2120
op?: CheckOp;
2221
warrants: CheckWarrant[];
23-
consistentRead?: boolean;
2422
debug?: boolean;
2523
}
2624

2725
export interface FeatureCheck {
2826
featureId: string;
2927
subject: WarrantObject | Subject;
30-
context?: Context;
31-
consistentRead?: boolean;
28+
context?: PolicyContext;
3229
debug?: boolean;
3330
}
3431

3532
export interface PermissionCheck {
3633
permissionId: string;
3734
subject: WarrantObject | Subject;
38-
context?: Context;
39-
consistentRead?: boolean;
35+
context?: PolicyContext;
4036
debug?: boolean;
4137
}
4238

4339
export interface AccessCheckRequest {
4440
op?: CheckOp;
45-
warrants: Warrant[];
46-
consistentRead?: boolean;
41+
warrants: CheckWarrant[];
4742
debug?: boolean;
4843
}

src/types/Query.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { Context, isSubject, isWarrantObject, Subject, WarrantObject, WarrantObjectLiteral } from "./Warrant";
1+
import { isSubject, isWarrantObject, PolicyContext, Subject, WarrantObject, WarrantObjectLiteral } from "./Warrant";
22

33
export interface ForClause {
44
object?: WarrantObject | WarrantObjectLiteral;
55
relation?: string;
66
subject?: Subject | WarrantObject;
7-
context?: Context;
7+
context?: PolicyContext;
88
}
99

1010
export interface WhereClause {
1111
object?: WarrantObject | WarrantObjectLiteral;
1212
relation?: string;
1313
subject?: Subject | WarrantObject;
14-
context?: Context;
14+
context?: PolicyContext;
1515
}
1616

1717
export default class Query {

src/types/Session.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Context } from "./Warrant";
1+
import { PolicyContext } from "./Warrant";
22

33
export interface SessionParams {
44
userId: string;
55
ttl?: number;
6-
context?: Context;
6+
context?: PolicyContext;
77
}
88

99
export interface SelfServiceSessionParams extends SessionParams {

src/types/Warrant.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export interface ListWarrantOptions extends ListOptions {
77
userId?: string;
88
}
99

10-
export interface Context {
11-
[key: string]: string;
10+
export interface PolicyContext {
11+
[key: string]: any;
1212
}
1313

1414
export interface Subject {
@@ -27,7 +27,7 @@ export default interface Warrant {
2727
objectId: string;
2828
relation: string;
2929
subject: Subject;
30-
context?: Context;
30+
policy?: string;
3131
}
3232

3333
export interface WarrantObject {
@@ -48,5 +48,5 @@ export interface WarrantParams {
4848
object: WarrantObject | WarrantObjectLiteral;
4949
relation: string;
5050
subject: WarrantObject | Subject;
51-
context?: Context;
51+
policy?: string;
5252
}

0 commit comments

Comments
 (0)