@twin.org/rights-management-service 0.0.1-next.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,448 @@
1
+ import { HttpParameterHelper } from '@twin.org/api-models';
2
+ import { Guards, ComponentFactory, Coerce, GeneralError, Is } from '@twin.org/core';
3
+ import { OdrlContexts } from '@twin.org/standards-w3c-odrl';
4
+ import { HttpStatusCode } from '@twin.org/web';
5
+
6
+ // Copyright 2024 IOTA Stiftung.
7
+ // SPDX-License-Identifier: Apache-2.0.
8
+ /**
9
+ * The source used when communicating about these routes.
10
+ */
11
+ const ROUTES_SOURCE = "rightsManagementRoutes";
12
+ /**
13
+ * The tag to associate with the routes.
14
+ */
15
+ const tags = [
16
+ {
17
+ name: "Policy Administration Point",
18
+ description: "Endpoints for managing ODRL policies in the Policy Administration Point"
19
+ }
20
+ ];
21
+ /**
22
+ * The REST routes for the Rights Management.
23
+ * @param baseRouteName Prefix to prepend to the paths.
24
+ * @param componentName The name of the component to use in the routes stored in the ComponentFactory.
25
+ * @returns The generated routes.
26
+ */
27
+ function generateRestRoutesRightsManagement(baseRouteName, componentName) {
28
+ const createRoute = {
29
+ operationId: "papCreate",
30
+ summary: "Create a policy",
31
+ tag: tags[0].name,
32
+ method: "POST",
33
+ path: `${baseRouteName}/pap/`,
34
+ handler: async (httpRequestContext, request) => papCreate(httpRequestContext, componentName, request),
35
+ requestType: {
36
+ type: "IPapCreateRequest",
37
+ examples: [
38
+ {
39
+ id: "papCreateExample",
40
+ request: {
41
+ body: {
42
+ "@context": OdrlContexts.ContextRoot,
43
+ "@type": "Set",
44
+ permission: [
45
+ {
46
+ target: "http://example.com/asset/1",
47
+ action: "use"
48
+ }
49
+ ]
50
+ }
51
+ }
52
+ }
53
+ ]
54
+ },
55
+ responseType: [
56
+ {
57
+ type: "ICreatedResponse",
58
+ examples: [
59
+ {
60
+ id: "papCreateResponseExample",
61
+ response: {
62
+ statusCode: 201,
63
+ headers: {
64
+ location: "urn:rights-management:abc123def456"
65
+ }
66
+ }
67
+ }
68
+ ]
69
+ }
70
+ ]
71
+ };
72
+ const updateRoute = {
73
+ operationId: "papUpdate",
74
+ summary: "Update a policy",
75
+ tag: tags[0].name,
76
+ method: "PUT",
77
+ path: `${baseRouteName}/pap/:id`,
78
+ handler: async (httpRequestContext, request) => papUpdate(httpRequestContext, componentName, request),
79
+ requestType: {
80
+ type: "IPapUpdateRequest",
81
+ examples: [
82
+ {
83
+ id: "papUpdateExample",
84
+ request: {
85
+ pathParams: {
86
+ id: "urn:rights-management:abc123def456"
87
+ },
88
+ body: {
89
+ "@context": OdrlContexts.ContextRoot,
90
+ "@type": "Set",
91
+ uid: "urn:rights-management:abc123def456",
92
+ permission: [
93
+ {
94
+ target: "http://example.com/asset/2",
95
+ action: "read"
96
+ }
97
+ ]
98
+ }
99
+ }
100
+ }
101
+ ]
102
+ },
103
+ responseType: [
104
+ {
105
+ type: "INoContentResponse"
106
+ }
107
+ ]
108
+ };
109
+ const retrieveRoute = {
110
+ operationId: "papRetrieve",
111
+ summary: "Retrieve a policy",
112
+ tag: tags[0].name,
113
+ method: "GET",
114
+ path: `${baseRouteName}/pap/:id`,
115
+ handler: async (httpRequestContext, request) => papRetrieve(httpRequestContext, componentName, request),
116
+ requestType: {
117
+ type: "IPapRetrieveRequest",
118
+ examples: [
119
+ {
120
+ id: "papRetrieveExample",
121
+ request: {
122
+ pathParams: {
123
+ id: "urn:rights-management:abc123def456"
124
+ }
125
+ }
126
+ }
127
+ ]
128
+ },
129
+ responseType: [
130
+ {
131
+ type: "IPapRetrieveResponse",
132
+ examples: [
133
+ {
134
+ id: "papRetrieveResponseExample",
135
+ response: {
136
+ body: {
137
+ "@context": OdrlContexts.ContextRoot,
138
+ "@type": "Set",
139
+ uid: "urn:rights-management:abc123def456",
140
+ permission: [
141
+ {
142
+ target: "http://example.com/asset/1",
143
+ action: "use"
144
+ }
145
+ ]
146
+ }
147
+ }
148
+ }
149
+ ]
150
+ }
151
+ ]
152
+ };
153
+ const removeRoute = {
154
+ operationId: "papRemove",
155
+ summary: "Remove a policy",
156
+ tag: tags[0].name,
157
+ method: "DELETE",
158
+ path: `${baseRouteName}/pap/:id`,
159
+ handler: async (httpRequestContext, request) => papRemove(httpRequestContext, componentName, request),
160
+ requestType: {
161
+ type: "IPapRemoveRequest",
162
+ examples: [
163
+ {
164
+ id: "papRemoveExample",
165
+ request: {
166
+ pathParams: {
167
+ id: "urn:rights-management:abc123def456"
168
+ }
169
+ }
170
+ }
171
+ ]
172
+ },
173
+ responseType: [
174
+ {
175
+ type: "INoContentResponse"
176
+ }
177
+ ]
178
+ };
179
+ const queryRoute = {
180
+ operationId: "papQuery",
181
+ summary: "Query policies",
182
+ tag: tags[0].name,
183
+ method: "GET",
184
+ path: `${baseRouteName}/pap/query`,
185
+ handler: async (httpRequestContext, request) => papQuery(httpRequestContext, componentName, request),
186
+ requestType: {
187
+ type: "IPapQueryRequest",
188
+ examples: [
189
+ {
190
+ id: "papQueryExample",
191
+ request: {
192
+ query: {
193
+ cursor: "optional-pagination-cursor"
194
+ }
195
+ }
196
+ }
197
+ ]
198
+ },
199
+ responseType: [
200
+ {
201
+ type: "IPapQueryResponse",
202
+ examples: [
203
+ {
204
+ id: "papQueryResponseExample",
205
+ response: {
206
+ body: {
207
+ cursor: "next-page-cursor",
208
+ policies: [
209
+ {
210
+ "@context": OdrlContexts.ContextRoot,
211
+ "@type": "Set",
212
+ uid: "urn:rights-management:abc123def456",
213
+ permission: [
214
+ {
215
+ target: "http://example.com/asset/1",
216
+ action: "use"
217
+ }
218
+ ]
219
+ }
220
+ ]
221
+ }
222
+ }
223
+ }
224
+ ]
225
+ }
226
+ ]
227
+ };
228
+ return [createRoute, updateRoute, retrieveRoute, removeRoute, queryRoute];
229
+ }
230
+ /**
231
+ * PAP: Create a policy.
232
+ * @param httpRequestContext The request context for the API.
233
+ * @param componentName The name of the component to use in the routes.
234
+ * @param request The request.
235
+ * @returns The response object with additional http response properties.
236
+ */
237
+ async function papCreate(httpRequestContext, componentName, request) {
238
+ Guards.object(ROUTES_SOURCE, "request", request);
239
+ Guards.object(ROUTES_SOURCE, "request.body", request.body);
240
+ Guards.stringValue(ROUTES_SOURCE, "httpRequestContext.nodeIdentity", httpRequestContext.nodeIdentity);
241
+ const component = ComponentFactory.get(componentName);
242
+ const policy = request.body;
243
+ const uid = await component.papCreate(policy);
244
+ return {
245
+ statusCode: HttpStatusCode.created,
246
+ headers: {
247
+ location: uid
248
+ }
249
+ };
250
+ }
251
+ /**
252
+ * PAP: Update a policy.
253
+ * @param httpRequestContext The request context for the API.
254
+ * @param componentName The name of the component to use in the routes.
255
+ * @param request The request.
256
+ * @returns The response object with additional http response properties.
257
+ */
258
+ async function papUpdate(httpRequestContext, componentName, request) {
259
+ Guards.object(ROUTES_SOURCE, "request", request);
260
+ Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
261
+ Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
262
+ Guards.object(ROUTES_SOURCE, "request.body", request.body);
263
+ Guards.stringValue(ROUTES_SOURCE, "httpRequestContext.nodeIdentity", httpRequestContext.nodeIdentity);
264
+ const component = ComponentFactory.get(componentName);
265
+ await component.papUpdate(request.body);
266
+ return {
267
+ statusCode: HttpStatusCode.noContent
268
+ };
269
+ }
270
+ /**
271
+ * PAP: Retrieve a policy.
272
+ * @param httpRequestContext The request context for the API.
273
+ * @param componentName The name of the component to use in the routes.
274
+ * @param request The request.
275
+ * @returns The response object with additional http response properties.
276
+ */
277
+ async function papRetrieve(httpRequestContext, componentName, request) {
278
+ Guards.object(ROUTES_SOURCE, "request", request);
279
+ Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
280
+ Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
281
+ Guards.stringValue(ROUTES_SOURCE, "httpRequestContext.nodeIdentity", httpRequestContext.nodeIdentity);
282
+ const component = ComponentFactory.get(componentName);
283
+ const policy = await component.papRetrieve(request.pathParams.id);
284
+ return {
285
+ body: policy
286
+ };
287
+ }
288
+ /**
289
+ * PAP: Remove a policy.
290
+ * @param httpRequestContext The request context for the API.
291
+ * @param componentName The name of the component to use in the routes.
292
+ * @param request The request.
293
+ * @returns The response object with additional http response properties.
294
+ */
295
+ async function papRemove(httpRequestContext, componentName, request) {
296
+ Guards.object(ROUTES_SOURCE, "request", request);
297
+ Guards.object(ROUTES_SOURCE, "request.pathParams", request.pathParams);
298
+ Guards.stringValue(ROUTES_SOURCE, "request.pathParams.id", request.pathParams.id);
299
+ const component = ComponentFactory.get(componentName);
300
+ await component.papRemove(request.pathParams.id);
301
+ return {
302
+ statusCode: HttpStatusCode.noContent
303
+ };
304
+ }
305
+ /**
306
+ * PAP: Query policies.
307
+ * @param httpRequestContext The request context for the API.
308
+ * @param componentName The name of the component to use in the routes.
309
+ * @param request The request.
310
+ * @returns The response object with additional http response properties.
311
+ */
312
+ async function papQuery(httpRequestContext, componentName, request) {
313
+ Guards.object(ROUTES_SOURCE, "request", request);
314
+ const component = ComponentFactory.get(componentName);
315
+ const result = await component.papQuery(HttpParameterHelper.objectFromString(request.query?.conditions), request.query?.cursor, Coerce.integer(request.query?.pageSize));
316
+ return {
317
+ body: {
318
+ cursor: result.cursor,
319
+ policies: result.policies
320
+ }
321
+ };
322
+ }
323
+
324
+ // Copyright 2024 IOTA Stiftung.
325
+ // SPDX-License-Identifier: Apache-2.0.
326
+ /**
327
+ * Service for performing Rights Management operations.
328
+ * This is a unified service that provides access to all Rights Management components.
329
+ */
330
+ class RightsManagementService {
331
+ /**
332
+ * The namespace supported by the Rights Management service.
333
+ */
334
+ static NAMESPACE = "rights-management";
335
+ /**
336
+ * Runtime name for the class.
337
+ */
338
+ CLASS_NAME = "RightsManagementService";
339
+ /**
340
+ * The PAP component implementation.
341
+ * @internal
342
+ */
343
+ _papComponent;
344
+ /**
345
+ * Create a new instance of RightsManagementService.
346
+ * @param options The options for the service.
347
+ */
348
+ constructor(options) {
349
+ this._papComponent = ComponentFactory.get(options?.papComponentType ?? "pap");
350
+ }
351
+ /**
352
+ * PAP: Create a new policy with auto-generated UID.
353
+ * @param policy The policy to create (uid will be auto-generated).
354
+ * @returns The UID of the created policy.
355
+ */
356
+ async papCreate(policy) {
357
+ try {
358
+ Guards.object(this.CLASS_NAME, "policy", policy);
359
+ const result = await this._papComponent.create(policy);
360
+ return result;
361
+ }
362
+ catch (error) {
363
+ throw new GeneralError(this.CLASS_NAME, "papCreateFailed", undefined, error);
364
+ }
365
+ }
366
+ /**
367
+ * PAP: Update an existing policy.
368
+ * @param policy The policy to update (must include uid).
369
+ * @returns Nothing.
370
+ */
371
+ async papUpdate(policy) {
372
+ try {
373
+ Guards.object(this.CLASS_NAME, "policy", policy);
374
+ await this._papComponent.update(policy);
375
+ }
376
+ catch (error) {
377
+ throw new GeneralError(this.CLASS_NAME, "papUpdateFailed", undefined, error);
378
+ }
379
+ }
380
+ /**
381
+ * PAP: Retrieve a policy.
382
+ * @param policyId The id of the policy to retrieve.
383
+ * @returns The policy.
384
+ */
385
+ async papRetrieve(policyId) {
386
+ try {
387
+ Guards.stringValue(this.CLASS_NAME, "policyId", policyId);
388
+ const policy = await this._papComponent.retrieve(policyId);
389
+ return policy;
390
+ }
391
+ catch (error) {
392
+ throw new GeneralError(this.CLASS_NAME, "papRetrieveFailed", undefined, error);
393
+ }
394
+ }
395
+ /**
396
+ * PAP: Remove a policy.
397
+ * @param policyId The id of the policy to remove.
398
+ * @returns Nothing.
399
+ */
400
+ async papRemove(policyId) {
401
+ try {
402
+ Guards.stringValue(this.CLASS_NAME, "policyId", policyId);
403
+ await this._papComponent.remove(policyId);
404
+ }
405
+ catch (error) {
406
+ throw new GeneralError(this.CLASS_NAME, "papRemoveFailed", undefined, error);
407
+ }
408
+ }
409
+ /**
410
+ * PAP: Query the policies using the specified conditions.
411
+ * @param conditions The conditions to use for the query.
412
+ * @param cursor The cursor to use for pagination.
413
+ * @param pageSize The number of results to return per page.
414
+ * @returns Cursor for next page of results and the policies matching the query.
415
+ */
416
+ async papQuery(conditions, cursor, pageSize) {
417
+ try {
418
+ if (!Is.empty(conditions)) {
419
+ Guards.object(this.CLASS_NAME, "conditions", conditions);
420
+ }
421
+ if (!Is.empty(cursor)) {
422
+ Guards.stringValue(this.CLASS_NAME, "cursor", cursor);
423
+ }
424
+ if (!Is.empty(pageSize)) {
425
+ Guards.integer(this.CLASS_NAME, "pageSize", pageSize);
426
+ }
427
+ const result = await this._papComponent.query(conditions, cursor, pageSize);
428
+ return result;
429
+ }
430
+ catch (error) {
431
+ throw new GeneralError(this.CLASS_NAME, "papQueryFailed", undefined, error);
432
+ }
433
+ }
434
+ }
435
+
436
+ /**
437
+ * Entry points for the REST API.
438
+ */
439
+ const restEntryPoints = [
440
+ {
441
+ name: "rights-management",
442
+ defaultBaseRoute: "rights-management",
443
+ tags,
444
+ generateRoutes: generateRestRoutesRightsManagement
445
+ }
446
+ ];
447
+
448
+ export { RightsManagementService, generateRestRoutesRightsManagement, papCreate, papQuery, papRemove, papRetrieve, papUpdate, restEntryPoints, tags };
@@ -0,0 +1,4 @@
1
+ export * from "./models/IRightsManagementServiceConstructorOptions";
2
+ export * from "./rightsManagementRoutes";
3
+ export * from "./rightsManagementService";
4
+ export * from "./restEntryPoints";
@@ -0,0 +1,10 @@
1
+ /**
2
+ * The constructor options for the RightsManagementService.
3
+ */
4
+ export interface IRightsManagementServiceConstructorOptions {
5
+ /**
6
+ * The type of the Policy Administration Point (PAP) component.
7
+ * @default pap
8
+ */
9
+ papComponentType?: string;
10
+ }
@@ -0,0 +1,5 @@
1
+ import type { IRestRouteEntryPoint } from "@twin.org/api-models";
2
+ /**
3
+ * Entry points for the REST API.
4
+ */
5
+ export declare const restEntryPoints: IRestRouteEntryPoint[];
@@ -0,0 +1,53 @@
1
+ import { type ICreatedResponse, type IHttpRequestContext, type INoContentResponse, type IRestRoute, type ITag } from "@twin.org/api-models";
2
+ import type { IPapCreateRequest, IPapQueryRequest, IPapQueryResponse, IPapRemoveRequest, IPapRetrieveRequest, IPapRetrieveResponse, IPapUpdateRequest } from "@twin.org/rights-management-models";
3
+ /**
4
+ * The tag to associate with the routes.
5
+ */
6
+ export declare const tags: ITag[];
7
+ /**
8
+ * The REST routes for the Rights Management.
9
+ * @param baseRouteName Prefix to prepend to the paths.
10
+ * @param componentName The name of the component to use in the routes stored in the ComponentFactory.
11
+ * @returns The generated routes.
12
+ */
13
+ export declare function generateRestRoutesRightsManagement(baseRouteName: string, componentName: string): IRestRoute[];
14
+ /**
15
+ * PAP: Create a policy.
16
+ * @param httpRequestContext The request context for the API.
17
+ * @param componentName The name of the component to use in the routes.
18
+ * @param request The request.
19
+ * @returns The response object with additional http response properties.
20
+ */
21
+ export declare function papCreate(httpRequestContext: IHttpRequestContext, componentName: string, request: IPapCreateRequest): Promise<ICreatedResponse>;
22
+ /**
23
+ * PAP: Update a policy.
24
+ * @param httpRequestContext The request context for the API.
25
+ * @param componentName The name of the component to use in the routes.
26
+ * @param request The request.
27
+ * @returns The response object with additional http response properties.
28
+ */
29
+ export declare function papUpdate(httpRequestContext: IHttpRequestContext, componentName: string, request: IPapUpdateRequest): Promise<INoContentResponse>;
30
+ /**
31
+ * PAP: Retrieve a policy.
32
+ * @param httpRequestContext The request context for the API.
33
+ * @param componentName The name of the component to use in the routes.
34
+ * @param request The request.
35
+ * @returns The response object with additional http response properties.
36
+ */
37
+ export declare function papRetrieve(httpRequestContext: IHttpRequestContext, componentName: string, request: IPapRetrieveRequest): Promise<IPapRetrieveResponse>;
38
+ /**
39
+ * PAP: Remove a policy.
40
+ * @param httpRequestContext The request context for the API.
41
+ * @param componentName The name of the component to use in the routes.
42
+ * @param request The request.
43
+ * @returns The response object with additional http response properties.
44
+ */
45
+ export declare function papRemove(httpRequestContext: IHttpRequestContext, componentName: string, request: IPapRemoveRequest): Promise<INoContentResponse>;
46
+ /**
47
+ * PAP: Query policies.
48
+ * @param httpRequestContext The request context for the API.
49
+ * @param componentName The name of the component to use in the routes.
50
+ * @param request The request.
51
+ * @returns The response object with additional http response properties.
52
+ */
53
+ export declare function papQuery(httpRequestContext: IHttpRequestContext, componentName: string, request: IPapQueryRequest): Promise<IPapQueryResponse>;
@@ -0,0 +1,58 @@
1
+ import type { EntityCondition } from "@twin.org/entity";
2
+ import type { IRightsManagementComponent } from "@twin.org/rights-management-models";
3
+ import type { IOdrlPolicy } from "@twin.org/standards-w3c-odrl";
4
+ import type { IRightsManagementServiceConstructorOptions } from "./models/IRightsManagementServiceConstructorOptions";
5
+ /**
6
+ * Service for performing Rights Management operations.
7
+ * This is a unified service that provides access to all Rights Management components.
8
+ */
9
+ export declare class RightsManagementService implements IRightsManagementComponent {
10
+ /**
11
+ * The namespace supported by the Rights Management service.
12
+ */
13
+ static readonly NAMESPACE: string;
14
+ /**
15
+ * Runtime name for the class.
16
+ */
17
+ readonly CLASS_NAME: string;
18
+ /**
19
+ * Create a new instance of RightsManagementService.
20
+ * @param options The options for the service.
21
+ */
22
+ constructor(options?: IRightsManagementServiceConstructorOptions);
23
+ /**
24
+ * PAP: Create a new policy with auto-generated UID.
25
+ * @param policy The policy to create (uid will be auto-generated).
26
+ * @returns The UID of the created policy.
27
+ */
28
+ papCreate(policy: Omit<IOdrlPolicy, "uid">): Promise<string>;
29
+ /**
30
+ * PAP: Update an existing policy.
31
+ * @param policy The policy to update (must include uid).
32
+ * @returns Nothing.
33
+ */
34
+ papUpdate(policy: IOdrlPolicy): Promise<void>;
35
+ /**
36
+ * PAP: Retrieve a policy.
37
+ * @param policyId The id of the policy to retrieve.
38
+ * @returns The policy.
39
+ */
40
+ papRetrieve(policyId: string): Promise<IOdrlPolicy>;
41
+ /**
42
+ * PAP: Remove a policy.
43
+ * @param policyId The id of the policy to remove.
44
+ * @returns Nothing.
45
+ */
46
+ papRemove(policyId: string): Promise<void>;
47
+ /**
48
+ * PAP: Query the policies using the specified conditions.
49
+ * @param conditions The conditions to use for the query.
50
+ * @param cursor The cursor to use for pagination.
51
+ * @param pageSize The number of results to return per page.
52
+ * @returns Cursor for next page of results and the policies matching the query.
53
+ */
54
+ papQuery(conditions?: EntityCondition<IOdrlPolicy>, cursor?: string, pageSize?: number): Promise<{
55
+ cursor?: string;
56
+ policies: IOdrlPolicy[];
57
+ }>;
58
+ }