-
Notifications
You must be signed in to change notification settings - Fork 7
/
data-authorization.ts
296 lines (259 loc) · 10.9 KB
/
data-authorization.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
import { INTEROP, asyncIterableToArray } from '@janeirodigital/interop-utils';
import { Memoize } from 'typescript-memoize';
import {
AuthorizationAgentFactory,
CRUDAgentRegistry,
ImmutableDataGrant,
DataGrantData,
CRUDDataRegistry,
ReadableDataRegistration,
InheritableDataGrant,
CRUDAgentRegistration
} from '..';
import { ReadableResource, SelectedFromRegistryDataGrant } from '.';
export class ReadableDataAuthorization extends ReadableResource {
factory: AuthorizationAgentFactory;
hasInheritingAuthorization: ReadableDataAuthorization[];
async inheritingAuthorizations(): Promise<ReadableDataAuthorization[]> {
const childIris = this.getSubjectsArray(INTEROP.inheritsFromAuthorization).map((subject) => subject.value);
return Promise.all(childIris.map((iri) => this.factory.readable.dataAuthorization(iri)));
}
async bootstrap(): Promise<void> {
await this.fetchData();
this.hasInheritingAuthorization = await this.inheritingAuthorizations();
}
@Memoize()
get grantee(): string {
return this.getObject('grantee').value;
}
@Memoize()
get registeredShapeTree(): string {
return this.getObject('registeredShapeTree').value;
}
@Memoize()
get scopeOfAuthorization(): string {
return this.getObject('scopeOfAuthorization').value;
}
@Memoize()
get grantedBy(): string {
return this.getObject('grantedBy').value;
}
@Memoize()
get dataOwner(): string | undefined {
return this.getObject('dataOwner')?.value;
}
@Memoize()
get accessMode(): string[] {
return this.getObjectsArray('accessMode').map((object) => object.value);
}
@Memoize()
get hasDataRegistration(): string | undefined {
return this.getObject('hasDataRegistration')?.value;
}
@Memoize()
get hasDataInstance(): string[] {
return this.getObjectsArray('hasDataInstance').map((obj) => obj.value);
}
public static async build(iri: string, factory: AuthorizationAgentFactory): Promise<ReadableDataAuthorization> {
const instance = new ReadableDataAuthorization(iri, factory);
await instance.bootstrap();
return instance;
}
private generateChildDelegatedDataGrants(
parentGrantIri: string,
sourceGrant: InheritableDataGrant,
granteeRegistration: CRUDAgentRegistration
): ImmutableDataGrant[] {
return this.hasInheritingAuthorization
.map((childAuthorization) => {
const childGrantIri = granteeRegistration.iriForContained();
const childSourceGrant = [...sourceGrant.hasInheritingGrant].find(
(grant) => grant.registeredShapeTree === childAuthorization.registeredShapeTree
);
if (!childSourceGrant) {
return null;
}
const childData: DataGrantData = {
dataOwner: childSourceGrant.dataOwner,
registeredShapeTree: childAuthorization.registeredShapeTree,
hasDataRegistration: childSourceGrant.hasDataRegistration,
scopeOfGrant: INTEROP.Inherited.value,
accessMode: childAuthorization.accessMode.filter((mode) => childSourceGrant.accessMode.includes(mode)),
inheritsFromGrant: parentGrantIri,
delegationOfGrant: childSourceGrant.iri
};
return this.factory.immutable.dataGrant(childGrantIri, childData);
})
.filter(Boolean);
}
private async generateDelegatedDataGrants(
agentRegistry: CRUDAgentRegistry,
granteeRegistration: CRUDAgentRegistration
): Promise<ImmutableDataGrant[]> {
if (this.scopeOfAuthorization === INTEROP.Inherited.value) {
throw new Error('this method should not be callend on data authorizations with Inherited scope');
}
let result: ImmutableDataGrant[] = [];
for await (const agentRegistration of agentRegistry.socialAgentRegistrations) {
// data onwer is specified but it is not their registration
if (this.dataOwner && this.dataOwner !== agentRegistration.registeredAgent) {
continue;
}
// don't create delegated data grants for data owned by the grantee (registeredAgent)
if (this.grantee === agentRegistration.registeredAgent) {
continue;
}
const accessGrantIri = agentRegistration.reciprocalRegistration?.hasAccessGrant;
if (!accessGrantIri) continue;
const accessGrant = await this.factory.readable.accessGrant(accessGrantIri);
// match shape tree
let matchingDataGrants = accessGrant.hasDataGrant.filter(
(grant) => grant.registeredShapeTree === this.registeredShapeTree
);
// match registration if restricted
if (this.hasDataRegistration) {
matchingDataGrants = matchingDataGrants.filter(
(grant) => grant.hasDataRegistration === this.hasDataRegistration
);
}
result = [
...matchingDataGrants.flatMap((sourceGrant) => {
const regularGrantIri = granteeRegistration.iriForContained();
const childDataGrants: ImmutableDataGrant[] = this.generateChildDelegatedDataGrants(
regularGrantIri,
sourceGrant as InheritableDataGrant,
granteeRegistration
);
// TODO (elf-pavlik) use hasDataInstance if present - add snippet
const data: DataGrantData = {
dataOwner: sourceGrant.dataOwner,
registeredShapeTree: sourceGrant.registeredShapeTree,
hasDataRegistration: sourceGrant.hasDataRegistration,
scopeOfGrant: sourceGrant.scopeOfGrant.value,
delegationOfGrant: sourceGrant.iri,
accessMode: this.accessMode.filter((mode) => sourceGrant.accessMode.includes(mode))
};
if (sourceGrant.scopeOfGrant.value === INTEROP.SelectedFromRegistry.value) {
data.hasDataInstance = [...(sourceGrant as SelectedFromRegistryDataGrant).hasDataInstance];
}
if (childDataGrants.length) {
data.hasInheritingGrant = childDataGrants;
}
const regularGrant = this.factory.immutable.dataGrant(regularGrantIri, data);
return [regularGrant, ...childDataGrants];
}),
...result
];
}
return result;
}
private generateChildSourceDataGrants(
parentGrantIri: string,
dataRegistrations: ReadableDataRegistration[],
granteeRegistration: CRUDAgentRegistration
): ImmutableDataGrant[] {
return this.hasInheritingAuthorization
.map((childAuthorization) => {
const childGrantIri = granteeRegistration.iriForContained();
// each data registry has only one data registration for any given shape tree
const dataRegistration = dataRegistrations.find(
(registration) => registration.registeredShapeTree === childAuthorization.registeredShapeTree
);
if (!dataRegistration) {
return null;
}
const childData: DataGrantData = {
dataOwner: childAuthorization.grantedBy,
registeredShapeTree: childAuthorization.registeredShapeTree,
hasDataRegistration: dataRegistration.iri,
scopeOfGrant: INTEROP.Inherited.value,
accessMode: childAuthorization.accessMode,
inheritsFromGrant: parentGrantIri
};
return this.factory.immutable.dataGrant(childGrantIri, childData);
})
.filter(Boolean);
}
private async generateSourceDataGrants(
dataRegistries: CRUDDataRegistry[],
granteeRegistration: CRUDAgentRegistration
): Promise<ImmutableDataGrant[]> {
if (this.scopeOfAuthorization === INTEROP.Inherited.value) {
throw new Error('this method should not be callend on data authorizations with Inherited scope');
}
const generatedDataGrants: ImmutableDataGrant[] = [];
// FIXME handle each data registry independently
for (const dataRegistry of dataRegistries) {
// const dataRegistrations = dataRegistriesArr.flat();
const dataRegistrations = await asyncIterableToArray(dataRegistry.registrations);
let matchingRegistration: ReadableDataRegistration;
if (this.hasDataRegistration) {
// match registration if specified
matchingRegistration = dataRegistrations.find((registration) => registration.iri === this.hasDataRegistration);
} else {
// match shape tree
matchingRegistration = dataRegistrations.find(
(registration) => registration.registeredShapeTree === this.registeredShapeTree
);
}
if (!matchingRegistration) continue;
// create source grant
const regularGrantIri = granteeRegistration.iriForContained();
// create children if needed
const childDataGrants: ImmutableDataGrant[] = this.generateChildSourceDataGrants(
regularGrantIri,
dataRegistrations,
granteeRegistration
);
let scopeOfGrant = INTEROP.AllFromRegistry.value;
if (this.scopeOfAuthorization === INTEROP.SelectedFromRegistry.value)
scopeOfGrant = INTEROP.SelectedFromRegistry.value;
const data: DataGrantData = {
dataOwner: this.grantedBy,
registeredShapeTree: this.registeredShapeTree,
hasDataRegistration: matchingRegistration.iri,
scopeOfGrant,
accessMode: this.accessMode
};
if (this.hasDataInstance.length) {
data.hasDataInstance = this.hasDataInstance;
}
if (childDataGrants.length) {
data.hasInheritingGrant = childDataGrants;
}
const regularGrant = this.factory.immutable.dataGrant(regularGrantIri, data);
generatedDataGrants.push(regularGrant, ...childDataGrants);
}
if (!generatedDataGrants.length) throw new Error('no data grants were generated!');
return generatedDataGrants;
}
public async generateDataGrants(
dataRegistries: CRUDDataRegistry[],
agentRegistry: CRUDAgentRegistry,
granteeRegistration: CRUDAgentRegistration
): Promise<ImmutableDataGrant[]> {
const dataGrants: ImmutableDataGrant[] = [];
/* Source grants are only created if Data Authorization is registred by the data owner.
* This can only happen with scope:
* - All - there will be no dataOwner set
* - AllFromAgent - dataOwner will equal grantedBy
* - lower with same condition as previous
* Otherwise only delegated data grants are created
*/
if (!this.dataOwner || this.dataOwner === this.grantedBy) {
dataGrants.push(...(await this.generateSourceDataGrants(dataRegistries, granteeRegistration)));
}
// do not create delegated data grants if granted by data owner, source grants will be created instead
/* Delegated grants are only created for data owned by others than agent granting the authorization
* This can only happen with scopes:
* - All - there will be no dataOwner set
* - All From Agent - dataOwner will be different than grantedBy
* - lower with same condition as previous
* Otherwise only source data grants are created
*/
if (!this.dataOwner || this.dataOwner !== this.grantedBy) {
dataGrants.push(...(await this.generateDelegatedDataGrants(agentRegistry, granteeRegistration)));
}
return dataGrants;
}
}