-
Notifications
You must be signed in to change notification settings - Fork 37
/
control.ts
545 lines (510 loc) · 21.3 KB
/
control.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//
// Copyright Inrupt Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import { acp } from "../constants";
import type {
SolidDataset,
Thing,
ThingPersisted,
Url,
UrlString,
WithResourceInfo,
WithServerResourceInfo,
} from "../interfaces";
import { hasServerResourceInfo } from "../interfaces";
import { getSourceUrl } from "../resource/resource";
import { addIri } from "../thing/add";
import { getIriAll, getUrlAll } from "../thing/get";
import { removeAll, removeIri } from "../thing/remove";
import { createThing, getThing, setThing } from "../thing/thing";
import type { WithAccessibleAcr } from "./acp";
import {
internal_addMemberPolicyUrl,
internal_addPolicyUrl,
internal_getAcr,
internal_getControlAll,
internal_getInitialisedControl,
internal_getMemberPolicyUrlAll,
internal_getPolicyUrlAll,
internal_removeMemberPolicyUrl,
internal_removeMemberPolicyUrlAll,
internal_removePolicyUrl,
internal_removePolicyUrlAll,
internal_setAcr,
internal_setControl,
} from "./control.internal";
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Given a Resource, check whether it is governed by Access Policies.
* (Specifically, a Resource that is governed by Access Policies will refer to exactly one Access
* Control Resource, and expose that to users who are allowed to see or modify access to the given
* Resource.)
*
* @param resource Resource which may or may not be governed by Access Policies.
* @returns True if the Resource refers to an Access Control Resource and is hence governed by Access Policies, or false if it does not.
* @since 1.6.0
*/
export function hasLinkedAcr<Resource extends WithServerResourceInfo>(
resource: Resource,
): resource is WithLinkedAcr<Resource> {
return (
hasServerResourceInfo(resource) &&
Array.isArray(
resource.internal_resourceInfo.linkedResources[acp.accessControl],
) &&
resource.internal_resourceInfo.linkedResources[acp.accessControl].length ===
1
);
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* An Access Control Resource, containing [[Control]]s specifying which [[Policy]]'s
* apply to the Resource this Access Control Resource is linked to.
* @since 1.6.0
*/
export type AccessControlResource = SolidDataset &
WithResourceInfo & { accessTo: UrlString };
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* A Control, usually contained in an [[AccessControlResource]]. It describes which
* [[Policy]]'s apply to a Resource.
* @since 1.6.0
*/
export type Control = Thing;
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* If this type applies to a Resource, it is governed by an Access Control Resource,
* and thus not the Web Access Control spec.
* It does not indicate that this Access Control Resource will also be accessible to the current
* user.
* @since 1.6.0
*/
export type WithLinkedAcr<
Resource extends WithServerResourceInfo = WithServerResourceInfo,
> = Resource & {
internal_resourceInfo: {
linkedResources: {
[acp.accessControl]: [string];
};
};
};
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Add a [[Policy]] to an Access Control Resource such that that [[Policy]] applies to the Access
* Control Resource itself, rather than the Resource it governs.
*
* @param resourceWithAcr The Resource with an Access Control Resource to which the ACR Policy should be added.
* @param policyUrl URL of the Policy that should apply to the given Access Control Resource.
* @returns A Resource with a new Access Control Resource equal to the original ACR, but with the given ACR Policy added to it.
* @since 1.6.0
*/
export function addAcrPolicyUrl<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
policyUrl: Url | UrlString | ThingPersisted,
): ResourceExt {
const acr = internal_getAcr(resourceWithAcr);
const acrUrl = getSourceUrl(acr);
let acrThing = getThing(acr, acrUrl) ?? createThing({ url: acrUrl });
acrThing = addIri(acrThing, acp.access, policyUrl);
const updatedAcr = setThing(acr, acrThing);
const updatedResource = internal_setAcr(resourceWithAcr, updatedAcr);
return updatedResource;
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Add a [[Policy]] to a Resource's Access Control Resource such that that
* Policy applies to the Access Control Resources of child Resources.
*
* @param resourceWithAcr The Resource with an Access Control Resource to which the ACR Policy should be added.
* @param policyUrl URL of the Policy that should apply to the given Access Control Resources of children of the Resource.
* @returns A Resource with a new Access Control Resource equal to the original ACR, but with the given ACR Policy added to it.
* @since 1.6.0
*/
export function addMemberAcrPolicyUrl<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
policyUrl: Url | UrlString | ThingPersisted,
): ResourceExt {
const acr = internal_getAcr(resourceWithAcr);
const acrUrl = getSourceUrl(acr);
let acrThing = getThing(acr, acrUrl) ?? createThing({ url: acrUrl });
acrThing = addIri(acrThing, acp.accessMembers, policyUrl);
const updatedAcr = setThing(acr, acrThing);
const updatedResource = internal_setAcr(resourceWithAcr, updatedAcr);
return updatedResource;
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Get the URLs of the Access Policies that apply to an Access Control Resource itself, rather than
* to the Resource it governs.
*
* @param resourceWithAcr The Resource with the Access Control Resource of which to get the URLs of the Policies that govern access to it.
* @returns URLs of the Policies that govern access to the given Access Control Resource.
* @since 1.6.0
*/
export function getAcrPolicyUrlAll<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
): UrlString[] {
const acr = internal_getAcr(resourceWithAcr);
const acrUrl = getSourceUrl(acr);
const acrThing = getThing(acr, acrUrl);
if (acrThing === null) {
return [];
}
return getIriAll(acrThing, acp.access);
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Get the URLs of the Access Policies that apply to the Access Control Resources of the Resource's
* children.
*
* @param resourceWithAcr The Resource with the Access Control Resource of which to get the URLs of the Policies that govern access to its children.
* @returns URLs of the Policies that govern access to the Access Control Resources of the given Resource's children.
* @since 1.6.0
*/
export function getMemberAcrPolicyUrlAll<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
): UrlString[] {
const acr = internal_getAcr(resourceWithAcr);
const acrUrl = getSourceUrl(acr);
const acrThing = getThing(acr, acrUrl);
if (acrThing === null) {
return [];
}
return getIriAll(acrThing, acp.accessMembers);
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Stop the URL of a given [[Policy]] from applying to an Access Control Resource itself.
*
* @param resourceWithAcr The Resource with the Access Control Resource to which the given URL of a Policy should no longer apply.
* @param policyUrl The URL of the Policy that should no longer apply.
* @returns A Resource with a new Access Control Resource equal to the original ACR, but with the given ACR Policy removed from it.
* @since 1.6.0
*/
export function removeAcrPolicyUrl<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
policyUrl: Url | UrlString | ThingPersisted,
): ResourceExt {
const acr = internal_getAcr(resourceWithAcr);
const acrUrl = getSourceUrl(acr);
const acrThing = getThing(acr, acrUrl);
if (acrThing === null) {
return resourceWithAcr;
}
if (!getIriAll(acrThing, acp.access).includes(policyUrl.toString())) {
return resourceWithAcr;
}
const updatedAcrThing = removeIri(acrThing, acp.access, policyUrl);
const updatedAcr = setThing(acr, updatedAcrThing);
return internal_setAcr(resourceWithAcr, updatedAcr);
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Stop the URL of a given [[Policy]] from applying to the Access Control Resources of the
* Resource's children.
*
* @param resourceWithAcr The Resource with the Access Control Resource to whose children's ACRs the given URL of a Policy should no longer apply.
* @param policyUrl The URL of the Policy that should no longer apply.
* @returns A Resource with a new Access Control Resource equal to the original ACR, but with the given member ACR Policy removed from it.
* @since 1.6.0
*/
export function removeMemberAcrPolicyUrl<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
policyUrl: Url | UrlString | ThingPersisted,
): ResourceExt {
const acr = internal_getAcr(resourceWithAcr);
const acrUrl = getSourceUrl(acr);
const acrThing = getThing(acr, acrUrl);
if (acrThing === null) {
return resourceWithAcr;
}
if (!getIriAll(acrThing, acp.accessMembers).includes(policyUrl.toString())) {
return resourceWithAcr;
}
const updatedAcrThing = removeIri(acrThing, acp.accessMembers, policyUrl);
const updatedAcr = setThing(acr, updatedAcrThing);
return internal_setAcr(resourceWithAcr, updatedAcr);
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Stop all URL of Access Policies from applying to an Access Control Resource itself.
*
* @param resourceWithAcr The Resource with the Access Control Resource to which no more Policies should apply.
* @returns A Resource with a new Access Control Resource equal to the original ACR, but without any Policy applying to it.
* @since 1.6.0
*/
export function removeAcrPolicyUrlAll<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
): ResourceExt {
const acr = internal_getAcr(resourceWithAcr);
const acrUrl = getSourceUrl(acr);
const acrThing = getThing(acr, acrUrl);
if (acrThing === null) {
return resourceWithAcr;
}
const updatedAcrThing = removeAll(acrThing, acp.access);
const updatedAcr = setThing(acr, updatedAcrThing);
return internal_setAcr(resourceWithAcr, updatedAcr);
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Stop all URL of Access Policies from applying to the Access Control Resources of the Resource's
* children.
*
* @param resourceWithAcr The Resource with the Access Control Resource that should no longer apply Policies to its children's ACRs.
* @returns A Resource with a new Access Control Resource equal to the original ACR, but without any Policy applying to its children's ACRs.
* @since 1.6.0
*/
export function removeMemberAcrPolicyUrlAll<
ResourceExt extends WithAccessibleAcr,
>(resourceWithAcr: ResourceExt): ResourceExt {
const acr = internal_getAcr(resourceWithAcr);
const acrUrl = getSourceUrl(acr);
const acrThing = getThing(acr, acrUrl);
if (acrThing === null) {
return resourceWithAcr;
}
const updatedAcrThing = removeAll(acrThing, acp.accessMembers);
const updatedAcr = setThing(acr, updatedAcrThing);
return internal_setAcr(resourceWithAcr, updatedAcr);
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Add a [[Policy]] to an Access Control Resource such that that [[Policy]] applies to that Resource.
*
* @param resourceWithAcr The Resource to which the ACR Policy should be added.
* @param policyUrl URL of the Policy that should apply to the given Resource.
* @returns A Resource with a new Access Control Resource equal to the original ACR, but with the given Policy added to it.
* @since 1.6.0
*/
export function addPolicyUrl<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
policyUrl: Url | UrlString,
): ResourceExt {
const control = internal_getInitialisedControl(resourceWithAcr);
const updatedControl = internal_addPolicyUrl(control, policyUrl);
const updatedResource = internal_setControl(resourceWithAcr, updatedControl);
return updatedResource;
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Add a [[Policy]] to a Resource's Access Control Resource such that that
* Policy applies to that Resource's children.
*
* @param resourceWithAcr The Resource to whose Access Control Resource the Policy should be added.
* @param policyUrl URL of the Policy that should apply to the given Resource's children.
* @returns A new Resource equal to the given Resource, but with the given Member Policy added to its Access Control Resource.
* @since 1.6.0
*/
export function addMemberPolicyUrl<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
policyUrl: Url | UrlString,
): ResourceExt {
const control = internal_getInitialisedControl(resourceWithAcr);
const updatedControl = internal_addMemberPolicyUrl(control, policyUrl);
const updatedResource = internal_setControl(resourceWithAcr, updatedControl);
return updatedResource;
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Get the URLs of the Access Policies that apply to a Resource.
*
* @param resourceWithAcr The Resource with the Access Control Resource of which to get the URLs of the Policies that govern access to it.
* @returns URLs of the Policies that govern access to the given Resource.
* @since 1.6.0
*/
export function getPolicyUrlAll<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
): UrlString[] {
const controls = internal_getControlAll(resourceWithAcr);
const policyUrlsByControl = controls.map((control) =>
internal_getPolicyUrlAll(control),
);
const uniquePolicyUrls = new Set<UrlString>();
policyUrlsByControl.forEach((policyUrls) => {
policyUrls.forEach((url) => uniquePolicyUrls.add(url));
});
return Array.from(uniquePolicyUrls);
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Get the URLs of the Access Policies that apply to a Resource's children.
*
* @param resourceWithAcr The Resource with the Access Control Resource of which to get the URLs of the Policies that govern access to its children.
* @returns URLs of the Policies that govern access to the given Resource's children.
* @since 1.6.0
*/
export function getMemberPolicyUrlAll<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
): UrlString[] {
const controls = internal_getControlAll(resourceWithAcr);
const memberPolicyUrlsByControl = controls.map((control) =>
internal_getMemberPolicyUrlAll(control),
);
const uniquePolicyUrls = new Set<UrlString>();
memberPolicyUrlsByControl.forEach((policyUrls) => {
policyUrls.forEach((url) => uniquePolicyUrls.add(url));
});
return Array.from(uniquePolicyUrls);
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Stop the URL of a given [[Policy]] from applying to a Resource.
*
* @param resourceWithAcr The Resource, with its Access Control Resource, to which the given URL of a Policy should no longer apply.
* @param policyUrl The URL of the Policy that should no longer apply.
* @returns A Resource with a new Access Control Resource equal to the original ACR, but with the given Policy removed from it.
* @since 1.6.0
*/
export function removePolicyUrl<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
policyUrl: Url | UrlString | ThingPersisted,
): ResourceExt {
const controls = internal_getControlAll(resourceWithAcr);
return controls
.filter((control) => getUrlAll(control, acp.apply).length > 0)
.map((control) => internal_removePolicyUrl(control, policyUrl))
.reduce(internal_setControl, resourceWithAcr);
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Stop the URL of a given [[Policy]] from applying to the Resource's children.
*
* @param resourceWithAcr The Resource with the Access Control Resource to whose children the given URL of a Policy should no longer apply.
* @param policyUrl The URL of the Policy that should no longer apply.
* @returns A Resource with a new Access Control Resource equal to the original ACR, but with the given Member Policy removed from it.
* @since 1.6.0
*/
export function removeMemberPolicyUrl<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
policyUrl: Url | UrlString | ThingPersisted,
): ResourceExt {
const controls = internal_getControlAll(resourceWithAcr);
const updatedControls = controls.map((control) =>
internal_removeMemberPolicyUrl(control, policyUrl),
);
const updatedResource = updatedControls.reduce(
internal_setControl,
resourceWithAcr,
);
return updatedResource;
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Stop all URL of Access Policies from applying to a Resource.
*
* @param resourceWithAcr The Resource, with its Access Control Resource, to which no more Policies should apply.
* @returns A Resource with a new Access Control Resource equal to the original ACR, but without any Policy applying to the Resource.
* @since 1.6.0
*/
export function removePolicyUrlAll<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
): ResourceExt {
const controls = internal_getControlAll(resourceWithAcr);
const updatedControls = controls.map((control) =>
internal_removePolicyUrlAll(control),
);
const updatedResource = updatedControls.reduce(
internal_setControl,
resourceWithAcr,
);
return updatedResource;
}
/**
* ```{note} The Web Access Control specification is not yet finalised. As such, this
* function is still experimental and subject to change, even in a non-major release.
* ```
*
* Stop all URL of Access Policies from applying to the Resource's children.
*
* @param resourceWithAcr The Resource with the Access Control Resource that should no longer apply Policies to its children.
* @returns A Resource with a new Access Control Resource equal to the original ACR, but without any Policy applying to the Resource's children.
* @since 1.6.0
*/
export function removeMemberPolicyUrlAll<ResourceExt extends WithAccessibleAcr>(
resourceWithAcr: ResourceExt,
): ResourceExt {
const controls = internal_getControlAll(resourceWithAcr);
const updatedControls = controls.map((control) =>
internal_removeMemberPolicyUrlAll(control),
);
const updatedResource = updatedControls.reduce(
internal_setControl,
resourceWithAcr,
);
return updatedResource;
}