@twin.org/rights-management-models 0.0.2-next.1 → 0.0.2-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.
- package/dist/cjs/index.cjs +386 -0
- package/dist/esm/index.mjs +379 -1
- package/dist/types/index.d.ts +65 -16
- package/dist/types/models/IPolicyLocator.d.ts +21 -0
- package/dist/types/models/api/dap/IDapCreateRequest.d.ts +27 -0
- package/dist/types/models/api/dap/IDapGetRequest.d.ts +26 -0
- package/dist/types/models/api/dap/IDapGetResponse.d.ts +17 -0
- package/dist/types/models/api/dap/IDapQueryRequest.d.ts +27 -0
- package/dist/types/models/api/dap/IDapQueryResponse.d.ts +17 -0
- package/dist/types/models/api/dap/IDapRemoveRequest.d.ts +26 -0
- package/dist/types/models/api/dap/IDapUpdateRequest.d.ts +31 -0
- package/dist/types/models/api/{policyAdministrationPoint → pap}/IPapCreateRequest.d.ts +3 -1
- package/dist/types/models/api/{policyAdministrationPoint/IPapRetrieveRequest.d.ts → pap/IPapGetRequest.d.ts} +3 -3
- package/dist/types/models/api/{policyAdministrationPoint/IPapRetrieveResponse.d.ts → pap/IPapGetResponse.d.ts} +2 -2
- package/dist/types/models/api/{policyAdministrationPoint → pap}/IPapUpdateRequest.d.ts +1 -1
- package/dist/types/models/api/pnap/IPnapGetRequest.d.ts +14 -0
- package/dist/types/models/api/pnap/IPnapGetResponse.d.ts +10 -0
- package/dist/types/models/api/pnap/IPnapQueryRequest.d.ts +19 -0
- package/dist/types/models/api/pnap/IPnapQueryResponse.d.ts +19 -0
- package/dist/types/models/api/pnap/IPnapRemoveRequest.d.ts +14 -0
- package/dist/types/models/api/pnap/IPnapSetRequest.d.ts +19 -0
- package/dist/types/models/api/pnp/IPnpAgreementRequest.d.ts +27 -0
- package/dist/types/models/api/pnp/IPnpAgreementVerificationRequest.d.ts +27 -0
- package/dist/types/models/api/pnp/IPnpContractNegotiationResponse.d.ts +21 -0
- package/dist/types/models/api/pnp/IPnpContractResponse.d.ts +21 -0
- package/dist/types/models/api/pnp/IPnpEventRequest.d.ts +27 -0
- package/dist/types/models/api/pnp/IPnpNegotiateRequest.d.ts +27 -0
- package/dist/types/models/api/pnp/IPnpNegotiationGetRequest.d.ts +22 -0
- package/dist/types/models/api/pnp/IPnpOfferRequest.d.ts +27 -0
- package/dist/types/models/api/pnp/IPnpTerminateRequest.d.ts +27 -0
- package/dist/types/models/dap/IDataAccessHandler.d.ts +52 -0
- package/dist/types/models/dap/IDataAccessPointComponent.d.ts +70 -0
- package/dist/types/models/dap/IDataAccessPointRequestComponent.d.ts +55 -0
- package/dist/types/models/dap/jsonLd/IDataAccessQuery.d.ts +33 -0
- package/dist/types/models/dap/jsonLd/IDataAccessQueryResponse.d.ts +24 -0
- package/dist/types/models/dap/jsonLd/IDataAccessRequest.d.ts +23 -0
- package/dist/types/models/dap/jsonLd/IDataAccessRequestWithObject.d.ts +24 -0
- package/dist/types/models/{IPolicyAdministrationPointComponent.d.ts → pap/IPolicyAdministrationPointComponent.d.ts} +6 -4
- package/dist/types/models/pdp/IPolicyArbiter.d.ts +23 -0
- package/dist/types/models/pdp/IPolicyDecision.d.ts +14 -0
- package/dist/types/models/{IPolicyDecisionPointComponent.d.ts → pdp/IPolicyDecisionPointComponent.d.ts} +19 -7
- package/dist/types/models/pdp/policyDecision.d.ts +17 -0
- package/dist/types/models/pep/IPolicyEnforcementPointComponent.d.ts +31 -0
- package/dist/types/models/pep/IPolicyEnforcementProcessor.d.ts +15 -0
- package/dist/types/models/pip/IPolicyInformation.d.ts +7 -0
- package/dist/types/models/pip/IPolicyInformationItems.d.ts +5 -0
- package/dist/types/models/pip/IPolicyInformationPointComponent.d.ts +35 -0
- package/dist/types/models/pip/IPolicyInformationSource.d.ts +18 -0
- package/dist/types/models/pip/policyInformationAccessMode.d.ts +21 -0
- package/dist/types/models/{IPolicyManagementPointComponent.d.ts → pmp/IPolicyManagementPointComponent.d.ts} +7 -5
- package/dist/types/models/pnp/IPolicyNegotiation.d.ts +75 -0
- package/dist/types/models/pnp/IPolicyNegotiationAdminPointComponent.d.ts +38 -0
- package/dist/types/models/pnp/IPolicyNegotiationPointComponent.d.ts +110 -0
- package/dist/types/models/pnp/IPolicyNegotiator.d.ts +30 -0
- package/dist/types/models/pnp/IPolicyRequester.d.ts +32 -0
- package/dist/types/models/pnp/jsonLd/IPolicyRequest.d.ts +28 -0
- package/dist/types/models/pxp/IPolicyExecutionAction.d.ts +24 -0
- package/dist/types/models/{IPolicyExecutionPointComponent.d.ts → pxp/IPolicyExecutionPointComponent.d.ts} +11 -10
- package/dist/types/models/rightsManagementContexts.d.ts +13 -0
- package/dist/types/models/rightsManagementNamespaces.d.ts +17 -0
- package/dist/types/models/rightsManagementTypes.d.ts +29 -0
- package/dist/types/utils/locatorHelper.d.ts +26 -0
- package/dist/types/utils/odrlPolicyHelper.d.ts +54 -0
- package/dist/types/utils/rightsManagementTokenHelper.d.ts +43 -0
- package/docs/changelog.md +84 -0
- package/docs/reference/classes/LocatorHelper.md +91 -0
- package/docs/reference/classes/OdrlPolicyHelper.md +211 -0
- package/docs/reference/classes/RightsManagementTokenHelper.md +151 -0
- package/docs/reference/index.md +67 -10
- package/docs/reference/interfaces/IDapCreateRequest.md +41 -0
- package/docs/reference/interfaces/IDapGetRequest.md +39 -0
- package/docs/reference/interfaces/IDapGetResponse.md +23 -0
- package/docs/reference/interfaces/IDapQueryRequest.md +41 -0
- package/docs/reference/interfaces/IDapQueryResponse.md +23 -0
- package/docs/reference/interfaces/IDapRemoveRequest.md +39 -0
- package/docs/reference/interfaces/IDapUpdateRequest.md +47 -0
- package/docs/reference/interfaces/IDataAccessHandler.md +169 -0
- package/docs/reference/interfaces/IDataAccessPointComponent.md +242 -0
- package/docs/reference/interfaces/IDataAccessPointRequestComponent.md +191 -0
- package/docs/reference/interfaces/IDataAccessQuery.md +51 -0
- package/docs/reference/interfaces/IDataAccessQueryResponse.md +35 -0
- package/docs/reference/interfaces/IDataAccessRequest.md +35 -0
- package/docs/reference/interfaces/IDataAccessRequestWithObject.md +35 -0
- package/docs/reference/interfaces/IPapCreateRequest.md +7 -1
- package/docs/reference/interfaces/{IPapRetrieveRequest.md → IPapGetRequest.md} +3 -3
- package/docs/reference/interfaces/IPapGetResponse.md +11 -0
- package/docs/reference/interfaces/IPapUpdateRequest.md +1 -1
- package/docs/reference/interfaces/IPnapGetRequest.md +17 -0
- package/docs/reference/interfaces/IPnapGetResponse.md +11 -0
- package/docs/reference/interfaces/IPnapQueryRequest.md +23 -0
- package/docs/reference/interfaces/IPnapQueryResponse.md +23 -0
- package/docs/reference/interfaces/IPnapRemoveRequest.md +17 -0
- package/docs/reference/interfaces/IPnapSetRequest.md +25 -0
- package/docs/reference/interfaces/IPnpAgreementRequest.md +41 -0
- package/docs/reference/interfaces/IPnpAgreementVerificationRequest.md +41 -0
- package/docs/reference/interfaces/IPnpContractNegotiationResponse.md +31 -0
- package/docs/reference/interfaces/IPnpContractResponse.md +31 -0
- package/docs/reference/interfaces/IPnpEventRequest.md +41 -0
- package/docs/reference/interfaces/IPnpNegotiateRequest.md +41 -0
- package/docs/reference/interfaces/IPnpNegotiationGetRequest.md +33 -0
- package/docs/reference/interfaces/IPnpOfferRequest.md +41 -0
- package/docs/reference/interfaces/IPnpTerminateRequest.md +41 -0
- package/docs/reference/interfaces/IPolicyAdministrationPointComponent.md +5 -5
- package/docs/reference/interfaces/IPolicyArbiter.md +63 -0
- package/docs/reference/interfaces/IPolicyDecision.md +19 -0
- package/docs/reference/interfaces/IPolicyDecisionPointComponent.md +52 -20
- package/docs/reference/interfaces/IPolicyEnforcementPointComponent.md +54 -18
- package/docs/reference/interfaces/IPolicyEnforcementProcessor.md +47 -0
- package/docs/reference/interfaces/IPolicyExecutionAction.md +69 -0
- package/docs/reference/interfaces/IPolicyExecutionPointComponent.md +24 -36
- package/docs/reference/interfaces/IPolicyInformation.md +7 -0
- package/docs/reference/interfaces/IPolicyInformationPointComponent.md +61 -17
- package/docs/reference/interfaces/IPolicyInformationSource.md +49 -0
- package/docs/reference/interfaces/IPolicyLocator.md +39 -0
- package/docs/reference/interfaces/IPolicyManagementPointComponent.md +11 -23
- package/docs/reference/interfaces/IPolicyNegotiation.md +191 -0
- package/docs/reference/interfaces/IPolicyNegotiationAdminPointComponent.md +103 -0
- package/docs/reference/interfaces/IPolicyNegotiationPointComponent.md +396 -0
- package/docs/reference/interfaces/IPolicyNegotiator.md +81 -0
- package/docs/reference/interfaces/IPolicyRequest.md +43 -0
- package/docs/reference/interfaces/IPolicyRequester.md +103 -0
- package/docs/reference/type-aliases/IPolicyInformationItems.md +5 -0
- package/docs/reference/type-aliases/PolicyDecision.md +5 -0
- package/docs/reference/type-aliases/PolicyInformationAccessMode.md +5 -0
- package/docs/reference/type-aliases/RightsManagementContexts.md +5 -0
- package/docs/reference/type-aliases/RightsManagementNamespaces.md +5 -0
- package/docs/reference/type-aliases/RightsManagementTypes.md +5 -0
- package/docs/reference/variables/PolicyDecision.md +19 -0
- package/docs/reference/variables/PolicyDecisionStage.md +1 -1
- package/docs/reference/variables/PolicyInformationAccessMode.md +25 -0
- package/docs/reference/variables/RightsManagementContexts.md +13 -0
- package/docs/reference/variables/RightsManagementNamespaces.md +19 -0
- package/docs/reference/variables/RightsManagementTypes.md +37 -0
- package/locales/en.json +10 -1
- package/package.json +7 -2
- package/dist/types/models/IPolicyEnforcementPointComponent.d.ts +0 -19
- package/dist/types/models/IPolicyInformationPointComponent.d.ts +0 -19
- package/dist/types/models/IRightsManagementComponent.d.ts +0 -44
- package/dist/types/models/policyActionCallback.d.ts +0 -16
- package/docs/reference/interfaces/IPapRetrieveResponse.md +0 -11
- package/docs/reference/interfaces/IRightsManagementComponent.md +0 -130
- package/docs/reference/type-aliases/PolicyActionCallback.md +0 -59
- /package/dist/types/models/api/{policyAdministrationPoint → pap}/IPapQueryRequest.d.ts +0 -0
- /package/dist/types/models/api/{policyAdministrationPoint → pap}/IPapQueryResponse.d.ts +0 -0
- /package/dist/types/models/api/{policyAdministrationPoint → pap}/IPapRemoveRequest.d.ts +0 -0
- /package/dist/types/models/{policyDecisionStage.d.ts → pdp/policyDecisionStage.d.ts} +0 -0
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,5 +1,26 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var core = require('@twin.org/core');
|
|
4
|
+
var identityModels = require('@twin.org/identity-models');
|
|
5
|
+
var standardsW3cDid = require('@twin.org/standards-w3c-did');
|
|
6
|
+
|
|
7
|
+
// Copyright 2024 IOTA Stiftung.
|
|
8
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
9
|
+
/**
|
|
10
|
+
* The type of decision from a Policy Decision Point (PDP).
|
|
11
|
+
*/
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
13
|
+
const PolicyDecision = {
|
|
14
|
+
/**
|
|
15
|
+
* Granted.
|
|
16
|
+
*/
|
|
17
|
+
Granted: "Granted",
|
|
18
|
+
/**
|
|
19
|
+
* Denied.
|
|
20
|
+
*/
|
|
21
|
+
Denied: "Denied"
|
|
22
|
+
};
|
|
23
|
+
|
|
3
24
|
// Copyright 2024 IOTA Stiftung.
|
|
4
25
|
// SPDX-License-Identifier: Apache-2.0.
|
|
5
26
|
/**
|
|
@@ -17,4 +38,369 @@ const PolicyDecisionStage = {
|
|
|
17
38
|
After: "after"
|
|
18
39
|
};
|
|
19
40
|
|
|
41
|
+
// Copyright 2024 IOTA Stiftung.
|
|
42
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
43
|
+
/**
|
|
44
|
+
* The mode that can be used to retrieve information from PIP sources.
|
|
45
|
+
*/
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
47
|
+
const PolicyInformationAccessMode = {
|
|
48
|
+
/**
|
|
49
|
+
* Public.
|
|
50
|
+
*/
|
|
51
|
+
Public: "public",
|
|
52
|
+
/**
|
|
53
|
+
* Private.
|
|
54
|
+
*/
|
|
55
|
+
Private: "private",
|
|
56
|
+
/**
|
|
57
|
+
* Any.
|
|
58
|
+
*/
|
|
59
|
+
Any: "any"
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
// Copyright 2024 IOTA Stiftung.
|
|
63
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
64
|
+
/**
|
|
65
|
+
* The LD Contexts concerning Rights Management.
|
|
66
|
+
*/
|
|
67
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
68
|
+
const RightsManagementContexts = {
|
|
69
|
+
/**
|
|
70
|
+
* The Rights Management LD Context.
|
|
71
|
+
*/
|
|
72
|
+
ContextRoot: "https://schema.twindev.org/rights-management"
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
// Copyright 2024 IOTA Stiftung.
|
|
76
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
77
|
+
/**
|
|
78
|
+
* The namespaces for rights management.
|
|
79
|
+
*/
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
81
|
+
const RightsManagementNamespaces = {
|
|
82
|
+
/**
|
|
83
|
+
* Policy.
|
|
84
|
+
*/
|
|
85
|
+
Policy: "policy",
|
|
86
|
+
/**
|
|
87
|
+
* Contract Negotiation.
|
|
88
|
+
*/
|
|
89
|
+
ContractNegotiation: "contract-negotiation"
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
// Copyright 2024 IOTA Stiftung.
|
|
93
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
94
|
+
/**
|
|
95
|
+
* The types of Rights Management data.
|
|
96
|
+
*/
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
98
|
+
const RightsManagementTypes = {
|
|
99
|
+
/**
|
|
100
|
+
* Represents policy request.
|
|
101
|
+
*/
|
|
102
|
+
PolicyRequest: "PolicyRequest",
|
|
103
|
+
/**
|
|
104
|
+
* Represents data access request.
|
|
105
|
+
*/
|
|
106
|
+
DataAccessRequest: "DataAccessRequest",
|
|
107
|
+
/**
|
|
108
|
+
* Represents data access request with object.
|
|
109
|
+
*/
|
|
110
|
+
DataAccessRequestWithObject: "DataAccessRequestWithObject",
|
|
111
|
+
/**
|
|
112
|
+
* Represents data access request query.
|
|
113
|
+
*/
|
|
114
|
+
DataAccessQuery: "DataAccessQuery",
|
|
115
|
+
/**
|
|
116
|
+
* Represents data access request query response.
|
|
117
|
+
*/
|
|
118
|
+
DataAccessQueryResponse: "DataAccessQueryResponse"
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// Copyright 2024 IOTA Stiftung.
|
|
122
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
123
|
+
/**
|
|
124
|
+
* Helper methods for Locator.
|
|
125
|
+
*/
|
|
126
|
+
class LocatorHelper {
|
|
127
|
+
/**
|
|
128
|
+
* Converts to a readable string.
|
|
129
|
+
* @param locator The policy locator.
|
|
130
|
+
* @returns The details of the locator as a string.
|
|
131
|
+
*/
|
|
132
|
+
static toString(locator) {
|
|
133
|
+
const parts = [];
|
|
134
|
+
if (core.Is.stringValue(locator.assignee)) {
|
|
135
|
+
parts.push(`Assignee: ${locator.assignee}`);
|
|
136
|
+
}
|
|
137
|
+
if (core.Is.stringValue(locator.action)) {
|
|
138
|
+
parts.push(`Action: ${locator.action}`);
|
|
139
|
+
}
|
|
140
|
+
if (core.Is.stringValue(locator.assetType)) {
|
|
141
|
+
parts.push(`Asset Type: ${locator.assetType}`);
|
|
142
|
+
}
|
|
143
|
+
if (core.Is.stringValue(locator.resourceId)) {
|
|
144
|
+
parts.push(`Resource ID: ${locator.resourceId}`);
|
|
145
|
+
}
|
|
146
|
+
return parts.join(", ");
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Compares locators to see if they match.
|
|
150
|
+
* @param locator1 The first policy locator.
|
|
151
|
+
* @param locator2 The second policy locator.
|
|
152
|
+
* @returns True if the locators match, false otherwise.
|
|
153
|
+
*/
|
|
154
|
+
static matches(locator1, locator2) {
|
|
155
|
+
return (
|
|
156
|
+
// The type assertions return boolean so don't want to use nullish coalescing
|
|
157
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
158
|
+
(core.Is.empty(locator1.assetType) || locator1.assetType === locator2.assetType) &&
|
|
159
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
160
|
+
(core.Is.empty(locator1.action) || locator1.action === locator2.action) &&
|
|
161
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
162
|
+
(core.Is.empty(locator1.assignee) || locator1.assignee === locator2.assignee) &&
|
|
163
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
164
|
+
(core.Is.empty(locator1.resourceId) || locator1.resourceId === locator2.resourceId));
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Finds a matching locator from a list of locators.
|
|
168
|
+
* @param locators The list of policy locators.
|
|
169
|
+
* @param targetLocator The target policy locator to find.
|
|
170
|
+
* @returns The matching locator if found, undefined otherwise.
|
|
171
|
+
*/
|
|
172
|
+
static findMatchingLocator(locators, targetLocator) {
|
|
173
|
+
if (!core.Is.arrayValue(locators)) {
|
|
174
|
+
return undefined;
|
|
175
|
+
}
|
|
176
|
+
return locators.find(locator => this.matches(locator, targetLocator));
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Copyright 2024 IOTA Stiftung.
|
|
181
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
182
|
+
/**
|
|
183
|
+
* Helper methods for Odrl Policies.
|
|
184
|
+
*/
|
|
185
|
+
class OdrlPolicyHelper {
|
|
186
|
+
/**
|
|
187
|
+
* Find the expiration date of the policy.
|
|
188
|
+
* @param policy The policy to check.
|
|
189
|
+
* @param assetType The type of the asset, if undefined will match any asset type.
|
|
190
|
+
* @param action The action to check, if undefined will match any action.
|
|
191
|
+
* @returns The expiration date of the policy, or undefined if not found.
|
|
192
|
+
*/
|
|
193
|
+
static findExpirationDate(policy, assetType, action) {
|
|
194
|
+
if (core.Is.arrayValue(policy.permission)) {
|
|
195
|
+
for (const permission of policy.permission) {
|
|
196
|
+
const matchesPermission = OdrlPolicyHelper.matchTargetAndAction(permission.target, permission.action, {
|
|
197
|
+
assetType,
|
|
198
|
+
action
|
|
199
|
+
});
|
|
200
|
+
if (matchesPermission && core.Is.arrayValue(permission.constraint)) {
|
|
201
|
+
for (const constraint of permission.constraint) {
|
|
202
|
+
if (constraint.leftOperand === "dateTime" &&
|
|
203
|
+
constraint.operator === "lteq" &&
|
|
204
|
+
core.Is.dateTimeString(constraint.rightOperand)) {
|
|
205
|
+
return constraint.rightOperand;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Match the target to the requested asset type.
|
|
214
|
+
* @param target The target to match.
|
|
215
|
+
* @param matchAssetType The asset type to match.
|
|
216
|
+
* @param matchResourceId The resource id to match.
|
|
217
|
+
* @returns True if the target is empty, the target matches the requested asset, false otherwise.
|
|
218
|
+
*/
|
|
219
|
+
static matchAsset(target, matchAssetType, matchResourceId) {
|
|
220
|
+
if (core.Is.empty(target) || core.Is.empty(matchAssetType)) {
|
|
221
|
+
return true;
|
|
222
|
+
}
|
|
223
|
+
if (core.Is.arrayValue(target)) {
|
|
224
|
+
return target.some(t => OdrlPolicyHelper.matchAsset(t, matchAssetType));
|
|
225
|
+
}
|
|
226
|
+
if (core.Is.stringValue(target)) {
|
|
227
|
+
return target === matchAssetType;
|
|
228
|
+
}
|
|
229
|
+
// TODO: This currently only handles the simple case of matching a single asset type.
|
|
230
|
+
// we need further processing if the target is more complex.
|
|
231
|
+
// we also need to support the resource id matching.
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Match the action to the asset type.
|
|
236
|
+
* @param action The action to match.
|
|
237
|
+
* @param matchAction The action to match.
|
|
238
|
+
* @returns True if the action is empty, the action matches the asset type, false otherwise.
|
|
239
|
+
*/
|
|
240
|
+
static matchAction(action, matchAction) {
|
|
241
|
+
if (core.Is.empty(action) || core.Is.empty(matchAction)) {
|
|
242
|
+
return true;
|
|
243
|
+
}
|
|
244
|
+
if (core.Is.arrayValue(action)) {
|
|
245
|
+
return action.some(a => OdrlPolicyHelper.matchAction(a, matchAction));
|
|
246
|
+
}
|
|
247
|
+
if (core.Is.stringValue(action)) {
|
|
248
|
+
return action === matchAction;
|
|
249
|
+
}
|
|
250
|
+
// TODO: This currently only handles the simple case of matching a single action type.
|
|
251
|
+
// we need further processing if the action is more complex.
|
|
252
|
+
return false;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Match the assignee.
|
|
256
|
+
* @param assignee The assignee to match.
|
|
257
|
+
* @param matchAssignee The assignee to match.
|
|
258
|
+
* @returns True if the assignee is empty, the assignee matches the asset type, false otherwise.
|
|
259
|
+
*/
|
|
260
|
+
static matchAssignee(assignee, matchAssignee) {
|
|
261
|
+
if (core.Is.empty(assignee) || core.Is.empty(matchAssignee)) {
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
if (core.Is.stringValue(assignee)) {
|
|
265
|
+
return assignee === matchAssignee;
|
|
266
|
+
}
|
|
267
|
+
// TODO: This currently only handles the simple case of matching a single assignee.
|
|
268
|
+
// we need further processing if the assignee is more complex.
|
|
269
|
+
return false;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Match the target and action to the requested asset type and action.
|
|
273
|
+
* @param target The target to match.
|
|
274
|
+
* @param action The action to match.
|
|
275
|
+
* @param locator The locator to match resource id if provided.
|
|
276
|
+
* @returns True if the target and action match the requested asset type and action, false otherwise.
|
|
277
|
+
*/
|
|
278
|
+
static matchTargetAndAction(target, action, locator) {
|
|
279
|
+
const assetTypeMatch = OdrlPolicyHelper.matchAsset(target, locator?.assetType, locator?.resourceId);
|
|
280
|
+
const actionMatch = OdrlPolicyHelper.matchAction(action, locator?.action);
|
|
281
|
+
return assetTypeMatch && actionMatch;
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Match the complete locator.
|
|
285
|
+
* @param assignee The assignee to match.
|
|
286
|
+
* @param target The target to match.
|
|
287
|
+
* @param action The action to match.
|
|
288
|
+
* @param locator The locator to match resource id if provided.
|
|
289
|
+
* @returns True if the complete locator matches, false otherwise.
|
|
290
|
+
*/
|
|
291
|
+
static matchLocator(assignee, target, action, locator) {
|
|
292
|
+
const assetTypeMatch = OdrlPolicyHelper.matchAsset(target, locator?.assetType, locator?.resourceId);
|
|
293
|
+
const assigneeMatch = OdrlPolicyHelper.matchAssignee(assignee, locator?.assignee);
|
|
294
|
+
const actionMatch = OdrlPolicyHelper.matchAction(action, locator?.action);
|
|
295
|
+
return assetTypeMatch && assigneeMatch && actionMatch;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// Copyright 2024 IOTA Stiftung.
|
|
300
|
+
// SPDX-License-Identifier: Apache-2.0.
|
|
301
|
+
/**
|
|
302
|
+
* Helper methods for creating and verifying rights managements requests.
|
|
303
|
+
*/
|
|
304
|
+
class RightsManagementTokenHelper {
|
|
305
|
+
/**
|
|
306
|
+
* The class name of the Rights Management Token Helper.
|
|
307
|
+
*/
|
|
308
|
+
static CLASS_NAME = "RightsManagementTokenHelper";
|
|
309
|
+
/**
|
|
310
|
+
* Create the token for an object.
|
|
311
|
+
* @param identityConnector The identity connector to use for creating the token.
|
|
312
|
+
* @param verificationMethodId The verification method id to use for creating the token.
|
|
313
|
+
* @param item The item to create the token for.
|
|
314
|
+
* @param tokenTtlInSeconds The time-to-live (TTL) for the token in seconds.
|
|
315
|
+
* @returns The token.
|
|
316
|
+
* @throws GeneralError is the token creation fails.
|
|
317
|
+
*/
|
|
318
|
+
static async createToken(identityConnector, verificationMethodId, item, tokenTtlInSeconds) {
|
|
319
|
+
core.Guards.object(RightsManagementTokenHelper.CLASS_NAME, "identityConnector", identityConnector);
|
|
320
|
+
core.Guards.stringValue(RightsManagementTokenHelper.CLASS_NAME, "verificationMethodId", verificationMethodId);
|
|
321
|
+
core.Guards.integer(RightsManagementTokenHelper.CLASS_NAME, "tokenTtlInSeconds", tokenTtlInSeconds);
|
|
322
|
+
const ttlMs = tokenTtlInSeconds * 1000;
|
|
323
|
+
const parts = identityModels.DocumentHelper.parseId(verificationMethodId);
|
|
324
|
+
const credential = await identityConnector.createVerifiableCredential(parts.id, verificationMethodId, undefined, item, {
|
|
325
|
+
expirationDate: new Date(Date.now() + ttlMs)
|
|
326
|
+
});
|
|
327
|
+
return credential.jwt;
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* Verify the token.
|
|
331
|
+
* @param identityConnector The identity connector to use for verifying the token.
|
|
332
|
+
* @param checkProperties Properties to compare against the subject to see if they match.
|
|
333
|
+
* @param token The token containing the necessary information.
|
|
334
|
+
* @param tokenTtlInSeconds The time-to-live (TTL) for the token in seconds.
|
|
335
|
+
* @returns The verifiable credential if the token is valid.
|
|
336
|
+
* @throws GeneralError is the token verification fails.
|
|
337
|
+
*/
|
|
338
|
+
static async verifyToken(identityConnector, checkProperties, token, tokenTtlInSeconds) {
|
|
339
|
+
core.Guards.object(RightsManagementTokenHelper.CLASS_NAME, "identityConnector", identityConnector);
|
|
340
|
+
core.Guards.stringValue(RightsManagementTokenHelper.CLASS_NAME, "token", token);
|
|
341
|
+
try {
|
|
342
|
+
const result = await identityConnector.checkVerifiableCredential(token);
|
|
343
|
+
const verifiableCredential = result.verifiableCredential;
|
|
344
|
+
if (core.Is.empty(verifiableCredential)) {
|
|
345
|
+
throw new core.GeneralError(RightsManagementTokenHelper.CLASS_NAME, "tokenNoCredential");
|
|
346
|
+
}
|
|
347
|
+
const issuer = core.Is.stringValue(verifiableCredential.issuer)
|
|
348
|
+
? verifiableCredential.issuer
|
|
349
|
+
: undefined;
|
|
350
|
+
if (core.Is.empty(issuer)) {
|
|
351
|
+
throw new core.GeneralError(RightsManagementTokenHelper.CLASS_NAME, "tokenNoIssuer");
|
|
352
|
+
}
|
|
353
|
+
for (const checkProperty of Object.keys(checkProperties)) {
|
|
354
|
+
if (core.ObjectHelper.propertyGet(checkProperties, checkProperty) !==
|
|
355
|
+
core.ObjectHelper.propertyGet(verifiableCredential.credentialSubject, checkProperty)) {
|
|
356
|
+
throw new core.GeneralError(RightsManagementTokenHelper.CLASS_NAME, "tokenItemMismatch", {
|
|
357
|
+
property: checkProperty
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
await RightsManagementTokenHelper.verifyIssuanceDate(standardsW3cDid.VerifiableCredentialHelper.getValidFrom(verifiableCredential), issuer, tokenTtlInSeconds);
|
|
362
|
+
return {
|
|
363
|
+
...verifiableCredential,
|
|
364
|
+
issuer
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
catch (err) {
|
|
368
|
+
throw new core.GeneralError(RightsManagementTokenHelper.CLASS_NAME, "tokenFailed", undefined, err);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Verify that the token has an issuance date and that it is within the allowed time-to-live (TTL).
|
|
373
|
+
* @param issuanceDate The issuance date from the token.
|
|
374
|
+
* @param assignee The identity of the node performing the action.
|
|
375
|
+
* @param tokenTtlInSeconds The time-to-live (TTL) for the token in seconds.
|
|
376
|
+
* @throws GeneralError if the token is missing the issuance date or if it has expired.
|
|
377
|
+
*/
|
|
378
|
+
static async verifyIssuanceDate(issuanceDate, assignee, tokenTtlInSeconds) {
|
|
379
|
+
core.Guards.stringValue(RightsManagementTokenHelper.CLASS_NAME, "assignee", assignee);
|
|
380
|
+
core.Guards.number(RightsManagementTokenHelper.CLASS_NAME, "tokenTtlInSeconds", tokenTtlInSeconds);
|
|
381
|
+
if (core.Is.empty(issuanceDate)) {
|
|
382
|
+
throw new core.GeneralError(RightsManagementTokenHelper.CLASS_NAME, "tokenMissingIssuanceDate", {
|
|
383
|
+
assignee
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
const tokenCreated = new Date(issuanceDate);
|
|
387
|
+
const now = Date.now();
|
|
388
|
+
const tokenTtlInMs = tokenTtlInSeconds * 1000;
|
|
389
|
+
// If the token has expired then we should reject it
|
|
390
|
+
if (tokenCreated.getTime() + tokenTtlInMs < now) {
|
|
391
|
+
throw new core.GeneralError(RightsManagementTokenHelper.CLASS_NAME, "tokenExpired", {
|
|
392
|
+
assignee
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
exports.LocatorHelper = LocatorHelper;
|
|
399
|
+
exports.OdrlPolicyHelper = OdrlPolicyHelper;
|
|
400
|
+
exports.PolicyDecision = PolicyDecision;
|
|
20
401
|
exports.PolicyDecisionStage = PolicyDecisionStage;
|
|
402
|
+
exports.PolicyInformationAccessMode = PolicyInformationAccessMode;
|
|
403
|
+
exports.RightsManagementContexts = RightsManagementContexts;
|
|
404
|
+
exports.RightsManagementNamespaces = RightsManagementNamespaces;
|
|
405
|
+
exports.RightsManagementTokenHelper = RightsManagementTokenHelper;
|
|
406
|
+
exports.RightsManagementTypes = RightsManagementTypes;
|