forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorization.guard.ts
More file actions
313 lines (283 loc) · 7.78 KB
/
Copy pathauthorization.guard.ts
File metadata and controls
313 lines (283 loc) · 7.78 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import {
Injectable,
CanActivate,
ExecutionContext,
ForbiddenException,
} from "@nestjs/common";
import { UnauthorizedException } from "@nestjs/common";
import { Reflector } from "@nestjs/core";
import { AuthorizationService } from "../services/authorization.service";
import { ReceiptPolicyService } from "../../receipts/receipt-policy.service";
import {
CHECK_PERMISSIONS_KEY,
Permission,
} from "../decorators/permissions.decorator";
@Injectable()
export class AuthorizationGuard implements CanActivate {
constructor(
private readonly reflector: Reflector,
private readonly authorizationService: AuthorizationService,
private readonly receiptPolicyService: ReceiptPolicyService,
) {}
async canActivate(context: ExecutionContext): Promise<boolean> {
const requiredPermissions = this.reflector.getAllAndOverride<Permission[]>(
CHECK_PERMISSIONS_KEY,
[context.getHandler(), context.getClass()],
);
if (!requiredPermissions) {
return true; // No specific permissions required
}
const request = context.switchToHttp().getRequest();
const user = request.user;
if (!user || !user.id) {
throw new UnauthorizedException("User not authenticated");
}
// Extract resource IDs from request parameters
const params = request.params;
const body = request.body;
for (const permission of requiredPermissions) {
const hasPermission = await this.checkPermission(
user.id,
permission,
params,
body,
request,
);
if (!hasPermission) {
throw new ForbiddenException(
`Insufficient permissions for ${permission.action} on ${permission.resource}`,
);
}
}
return true;
}
private async checkPermission(
userId: string,
permission: Permission,
params: any,
body: any,
request: any,
): Promise<boolean> {
const { resource, action } = permission;
switch (resource) {
case "split":
return this.checkSplitPermission(userId, action, params, body, request);
case "receipt":
return this.checkReceiptPermission(
userId,
action,
params,
body,
request,
);
case "payment":
return this.checkPaymentPermission(
userId,
action,
params,
body,
request,
);
case "export":
return this.checkExportPermission(
userId,
action,
params,
body,
request,
);
case "invitation":
return this.checkInvitationPermission(
userId,
action,
params,
body,
request,
);
case "dispute":
return this.checkDisputePermission(
userId,
action,
params,
body,
request,
);
case "group":
return this.checkGroupPermission(userId, action, params, body, request);
default:
return false;
}
}
private async checkSplitPermission(
userId: string,
action: string,
params: any,
body: any,
request: any,
): Promise<boolean> {
const splitId = params.splitId || params.id || body.splitId;
if (!splitId && (action === "create" || action === "list")) {
// For creation and listing, we check at service level
return true;
}
if (!splitId) {
return false;
}
switch (action) {
case "read":
case "update":
case "delete":
return this.authorizationService.canAccessSplit(userId, splitId);
case "create_payment":
return this.authorizationService.canCreatePayment(userId, splitId);
case "add_participant":
return this.authorizationService.canAddParticipant(userId, splitId);
case "remove_participant":
return this.authorizationService.canRemoveParticipant(userId, splitId);
default:
return false;
}
}
private async checkReceiptPermission(
userId: string,
action: string,
params: any,
body: any,
request: any,
): Promise<boolean> {
const receiptId = params.receiptId || params.id || body.receiptId;
const splitId = params.splitId || body.splitId;
if (action === "create") {
return this.receiptPolicyService.canCreateReceipt(userId, splitId);
}
if (!receiptId) {
return false;
}
switch (action) {
case "read":
case "delete":
return this.receiptPolicyService.canAccessReceipt(userId, receiptId);
default:
return false;
}
}
private async checkPaymentPermission(
userId: string,
action: string,
params: any,
body: any,
request: any,
): Promise<boolean> {
const splitId = params.splitId || body.splitId;
const participantId = params.participantId || body.participantId;
switch (action) {
case "create":
if (splitId && participantId) {
return this.authorizationService.canCreatePaymentForParticipant(
userId,
splitId,
participantId,
);
}
return false;
case "read_split_payments":
return splitId
? this.authorizationService.canAccessSplit(userId, splitId)
: false;
case "read_participant_payments":
return participantId
? this.authorizationService.canAccessParticipantPayments(
userId,
participantId,
)
: false;
default:
return false;
}
}
private async checkExportPermission(
userId: string,
action: string,
params: any,
body: any,
request: any,
): Promise<boolean> {
// Export permissions are typically user-scoped
return true; // User can only export their own data, enforced at service level
}
private async checkInvitationPermission(
userId: string,
action: string,
params: any,
body: any,
request: any,
): Promise<boolean> {
const splitId = body.splitId;
switch (action) {
case "create":
return splitId
? this.authorizationService.canAccessSplit(userId, splitId)
: false;
case "read":
case "accept":
// Invitations can be read/accepted by anyone with the token
return true;
default:
return false;
}
}
private async checkDisputePermission(
userId: string,
action: string,
params: any,
body: any,
request: any,
): Promise<boolean> {
const disputeId = params.disputeId || params.id;
const splitId = params.splitId || body.splitId;
switch (action) {
case "create":
return splitId
? this.authorizationService.canAccessSplit(userId, splitId)
: false;
case "read":
return disputeId
? this.authorizationService.canAccessDispute(userId, disputeId)
: false;
case "resolve":
case "reject":
// Admin actions - check if user is admin
return this.authorizationService.isAdmin(userId);
default:
return false;
}
}
private async checkGroupPermission(
userId: string,
action: string,
params: any,
body: any,
request: any,
): Promise<boolean> {
const groupId = params.id || params.groupId;
if (!groupId && action === "create") {
return true; // Users can create groups
}
if (!groupId) {
return false;
}
switch (action) {
case "read":
case "update":
case "delete":
return this.authorizationService.canAccessGroup(userId, groupId);
case "add_member":
case "remove_member":
return this.authorizationService.canManageGroupMembers(userId, groupId);
case "create_split":
return this.authorizationService.canCreateGroupSplit(userId, groupId);
default:
return false;
}
}
}