forked from SolidLabResearch/loama
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOdrlController.ts
More file actions
175 lines (144 loc) · 7.69 KB
/
Copy pathOdrlController.ts
File metadata and controls
175 lines (144 loc) · 7.69 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
import { getDefaultSession } from "@inrupt/solid-client-authn-browser";
import { BaseSubject, Index, IndexItem, Permission, ResourcePermissions, Resources } from "../types";
import { IAccessRequest, IController, IInboxConstructor, IStore, IStoreConstructor, SubjectConfig, SubjectConfigs, SubjectKey, SubjectType } from "../types/modules";
import { type AccessRequest as AccessRequestObject } from "../types/modules";
import { AccessRequest } from "./accessRequests/AccessRequest";
import { ODRLAccessRequest } from "./accessRequests/OdrlAccessRequest";
import { Mutex } from "./utils/Mutex";
import { ODRLAccessRequestService } from "./utils/OdrlAccessRequestService";
/**
* Controller which makes it calls to the backend AS through ODRL requests.
* Makes use of the Inrupt SDK to authenticate users.
*/
export class ODRLController<T extends Record<keyof T, BaseSubject<keyof T & string>>> extends Mutex implements IController<T> {
private index: IStore<Index<T[keyof T & string]>>;
private resources: IStore<Resources>;
private accessRequest: AccessRequest;
private subjectConfigs: SubjectConfigs<T>;
private authorizationServerURL: string;
// TODO : Find a better way of constructing the controller with all the different modules
constructor(storeConstructor: IStoreConstructor, inboxConstructor: IInboxConstructor, subjects: SubjectConfigs<T>, authorizationServerURL: string) {
super();
// There is currently no "easy" solution to get around the as IStore...
this.index = new storeConstructor("index.json", () => ({ id: "", items: [] })) as IStore<Index<T[keyof T & string]>>;
this.resources = new storeConstructor("resources.json", () => ({ id: "", items: [] })) as IStore<Resources>;;
this.accessRequest = new ODRLAccessRequest(this as unknown as ODRLController<{}>, inboxConstructor, this.resources);
this.subjectConfigs = subjects;
this.authorizationServerURL = authorizationServerURL;
}
private getSubjectConfig<K extends SubjectKey<T>>(subject: T[K]): SubjectConfig<T, T[K]> {
const subjectConfig = this.subjectConfigs[subject.type];
if (!subjectConfig) {
throw new Error(`No config found for subject type ${subject.type}`);
}
return subjectConfig as SubjectConfig<T, T[K]>
}
private async updateItem<K extends SubjectKey<T>>(resourceUrl: string, subject: SubjectType<T, K>, permissions: Permission[], alwaysKeepItem = false) {
}
AccessRequest(): IAccessRequest {
return this.accessRequest;
}
async setPodUrl(podUrl: string) {
}
unsetPodUrl() {
}
async getOrCreateIndex() {
return { id: "", items: [] }
}
getLabelForSubject<K extends SubjectKey<T>>(subject: T[K]): string {
const { resolver } = this.getSubjectConfig(subject);
return resolver.toLabel(subject);
}
/**
* Assemble the information for a subject in a resource
* @returns
*/
async getItem<K extends SubjectKey<T>>(resourceUrl: string, subject: SubjectType<T, K>): Promise<IndexItem<T[K]> | undefined> {
const subjectConfig = this.getSubjectConfig(subject)
const subjects = await subjectConfig.manager.getRemotePermissions<K>(resourceUrl);
const subjectPermission = subjects.find(entry => subjectConfig.resolver.checkMatch(entry.subject, subject))
if (!subjectPermission) return undefined
return {
id: "string",
requestId: "string",
isEnabled: subjectPermission.isEnabled,
permissions: [...subjectPermission.permissions ?? []],
resource: resourceUrl,
subject: subject,
} as IndexItem<T[K]>
}
async addPermission<K extends SubjectKey<T>>(resourceUrl: string, addedPermission: Permission, subject: SubjectType<T, K>) {
const release = await this.acquire();
try {
// 1. Create a new permission for the subject
await this.getSubjectConfig(subject).manager.createPermissions(resourceUrl, subject, [addedPermission])
// 2. Let the manager add the permission, return the updated version
const webId = getDefaultSession().info.webId!;
const permissions = await this.getSubjectConfig(subject).manager.getTargetPermissionsForUser(webId, subject.selector?.url ?? "", resourceUrl);
return permissions;
} catch (e) {
throw e;
} finally {
release();
}
}
async removeSubject<K extends SubjectKey<T>>(resourceUrl: string, subject: SubjectType<T, K>) {
const subjectConfig = this.getSubjectConfig(subject);
const item = await this.getItem(resourceUrl, subject);
await subjectConfig.manager.deletePermissions(resourceUrl, subject, item?.permissions ?? []);
}
async removePermission<K extends SubjectKey<T>>(resourceUrl: string, removedPermission: Permission, subject: SubjectType<T, K>) {
const release = await this.acquire()
try {
// 1. Delete a permission for the subject
await this.getSubjectConfig(subject).manager.deletePermissions(resourceUrl, subject, [removedPermission]);
// 2. Let the manager delete the permission, return the updated version
const webId = getDefaultSession().info.webId!;
const permissions = await this.getSubjectConfig(subject).manager.getTargetPermissionsForUser(webId, subject.selector!.url, resourceUrl);
return permissions
} catch (error) {
return []
} finally {
release();
}
}
async enablePermissions<K extends SubjectKey<T>>(resource: string, subject: SubjectType<T, K>) {
// won't fix
}
async disablePermissions<K extends SubjectKey<T>>(resourceUrl: string, subject: SubjectType<T, K>) {
// won't fix
}
async getContainerPermissionList(containerUrl: string): Promise<ResourcePermissions<T[keyof T]>[]> {
return this.getSubjectConfig({ type: "public" } as T[SubjectKey<T>]).manager.getContainerPermissionList(containerUrl);
}
// NOTE: Do we want to force this to only use the index stored in the store?
async getResourcePermissionList(resourceUrl: string): Promise<ResourcePermissions<T[keyof T]>> {
const result = await this.getSubjectConfig({ type: "public" } as T[SubjectKey<T>]).manager.getRemotePermissions(resourceUrl);
return {
resourceUrl,
canRequestAccess: true, // TODO
permissionsPerSubject: result
};
}
isSubjectSupported<K extends string, B extends BaseSubject<K>>(subject: BaseSubject<K>): IController<Record<K, B>> {
if (!this.subjectConfigs[subject.type as unknown as keyof T]) {
throw new Error(`Subject type ${subject.type} is not supported`);
}
return this as unknown as IController<Record<K, B>>
}
// ! added for access requests
async requestAccess(permission: { action: string; resource: string; }): Promise<void> {
const webid = getDefaultSession().info.webId!;
await new ODRLAccessRequestService(this.authorizationServerURL).requestAccess(permission.resource, webid, permission.action);
}
async handleAccessRequest(requestId: string, status: 'accepted' | 'denied'): Promise<void> {
const webid = getDefaultSession().info.webId!;
await new ODRLAccessRequestService(this.authorizationServerURL).acceptOrDenyAccess(requestId, webid, status);
}
async getAccessRequests(): Promise<{
asRequestingParty: AccessRequestObject[];
asResourceOwner: AccessRequestObject[];
}> {
return new ODRLAccessRequestService(this.authorizationServerURL).retrieveAccessRequests(getDefaultSession().info.webId!);
}
}