-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-ucp-enforcement.ts
223 lines (192 loc) · 8.61 KB
/
test-ucp-enforcement.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
#!/usr/bin/env ts-node
const crud = `
@prefix odrl: <http://www.w3.org/ns/odrl/2/> .
@prefix : <http://example.org/> .
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@prefix fno: <https://w3id.org/function/ontology#> .
@prefix log: <http://www.w3.org/2000/10/swap/log#> .
@prefix string: <http://www.w3.org/2000/10/swap/string#> .
@prefix list: <http://www.w3.org/2000/10/swap/list#> .
# Read ODRL rule
{
?permission a odrl:Permission;
odrl:action ?action ;
odrl:target ?targetResource ;
odrl:assignee ?requestedParty;
odrl:assigner ?resourceOwner .
?action list:in (odrl:use odrl:read) . # multiple options
?SCOPE log:notIncludes { ?permission odrl:constraint ?anything }. # No odrl:constraints may be present
# context of a request
?context
:resourceOwner ?resourceOwner;
:requestingParty ?requestedParty;
:target ?targetResource;
:requestPermission acl:Read.
:uuid5 log:uuid ?uuidStringdataUsagePolicyExecution.
( "urn:uuid:" ?uuidStringdataUsagePolicyExecution) string:concatenation ?urnUuidStringdataUsagePolicyExecution.
?dataUsagePolicyExecution log:uri ?urnUuidStringdataUsagePolicyExecution .
} =>
{
?dataUsagePolicyExecution a fno:Execution;
fno:executes <http://example.org/dataUsage> ;
:accessModesAllowed acl:Read.
}.
# Update ODRL Rule (odrl:modify: new asset is not created, not same as acl:write)
{
?permission a odrl:Permission;
odrl:action ?action ;
odrl:target ?targetResource ;
odrl:assignee ?requestedParty;
odrl:assigner ?resourceOwner .
?action list:in (odrl:use odrl:modify). # multiple options
?SCOPE log:notIncludes { ?permission odrl:constraint ?anything }. # No odrl:constraints may be present
# context of a request
?context
:resourceOwner ?resourceOwner;
:requestingParty ?requestedParty;
:target ?targetResource;
:requestPermission acl:Write.
:uuid6 log:uuid ?uuidStringdataUsagePolicyExecution.
( "urn:uuid:" ?uuidStringdataUsagePolicyExecution) string:concatenation ?urnUuidStringdataUsagePolicyExecution.
?dataUsagePolicyExecution log:uri ?urnUuidStringdataUsagePolicyExecution .
} =>
{
?dataUsagePolicyExecution a fno:Execution;
fno:executes <http://example.org/dataUsage> ;
:accessModesAllowed acl:Write.
}.
`
import { EyeJsReasoner } from "koreografeye";
import { PolicyExecutor, SimplePolicy, UconRequest, UcpPatternEnforcement, UcpPlugin, turtleStringToStore, MemoryUCRulesStorage } from "@solidlab/ucp";
/**
* Interface for a Usage Control Policy.
* Note: a Usage Control policy currently only has one rule.
*/
export interface UCPPolicy {
action: string,
owner: string,
resource: string,
requestingParty: string,
constraints?: Constraint[]
}
/**
* Interface for a Usage Control Policy Constraint
*/
export interface Constraint {
type: string,
operator: string,
value: any
}
/**
* Create a simple policy with an agreement and one rule
* Note: should and can be made synchronous
* @param type
* @param baseIri
* @returns
*/
export async function basicPolicy(type: UCPPolicy, baseIri?: string): Promise<SimplePolicy> {
baseIri = baseIri ?? `http://example.org/${new Date().valueOf()}#` // better would be uuid
const agreement = baseIri + "usagePolicy";
const rule = baseIri + "permission";
const policy = `@prefix odrl: <http://www.w3.org/ns/odrl/2/> .
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
<${agreement}>
a odrl:Agreement ;
odrl:permission <${rule}>.
<${rule}>
a odrl:Permission ;
odrl:action <${type.action}> ;
odrl:target <${type.resource}>;
odrl:assignee <${type.requestingParty}> ;
odrl:assigner <${type.owner}> .`
const constraints = createConstraints(rule, type.constraints ?? [])
const policyStore = await turtleStringToStore([policy, constraints].join("\n"))
return { representation: policyStore, policyIRI: agreement, ruleIRIs: [rule] }
}
export function createConstraints(ruleIRI: string, constraints: Constraint[]): string {
let constraintsString = ""
for (const constraint of constraints) {
// note: only temporal constraints currently, so the type is not checked
constraintsString += `@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix odrl: <http://www.w3.org/ns/odrl/2/> .
<${ruleIRI}> odrl:constraint [
odrl:leftOperand odrl:dateTime ;
odrl:operator <${constraint.operator}> ;
odrl:rightOperand "${(constraint.value as Date).toISOString()}"^^xsd:dateTime ] .
`
}
return constraintsString
}
async function main() {
// Note: assumption - Solid server is set up with public read and write access on port 3123
// $ npx @solid/community-server -p 3123 -c memory.json
// constants
const aclRead = "http://www.w3.org/ns/auth/acl#Read"
const aclWrite = "http://www.w3.org/ns/auth/acl#Write"
const odrlRead = "http://www.w3.org/ns/odrl/2/read"
const odrlWrite = "http://www.w3.org/ns/odrl/2/modify"
const odrlUse = "http://www.w3.org/ns/odrl/2/use"
const owner = "https://pod.woutslabbinck.com/profile/card#me"
const resource = "http://localhost:3000/test.ttl"
const requestingParty = "https://woslabbi.pod.knows.idlab.ugent.be/profile/card#me"
// requests
const readPolicyRequest: UconRequest = {
subject: requestingParty, action: [aclRead], resource: resource, owner: owner
}
const writePolicyRequest: UconRequest = {
subject: requestingParty, action: [aclWrite], resource: resource, owner: owner
}
const usePolicyRequest: UconRequest = {
subject: requestingParty, action: [aclWrite, aclRead], resource: resource, owner: owner
}
// policies
const readPolicy: UCPPolicy = { action: odrlRead, owner, resource, requestingParty }
const writePolicy: UCPPolicy = { action: odrlWrite, owner, resource, requestingParty }
const temporalReadPolicyOutOfBound: UCPPolicy = {
action: odrlRead, owner, resource, requestingParty,
constraints: [
{ operator: "http://www.w3.org/ns/odrl/2/gt", type: "temporal", value: new Date("2024-01-01") }, // from: must be greater than given date
{ operator: "http://www.w3.org/ns/odrl/2/lt", type: "temporal", value: new Date("2024-01-02") }, // to: must be smaller than given date
]
}
const temporalReadPolicyWithinBound: UCPPolicy = {
action: odrlRead, owner, resource, requestingParty,
constraints: [
{ operator: "http://www.w3.org/ns/odrl/2/gt", type: "temporal", value: new Date(0) }, // from: must be greater than given date
{ operator: "http://www.w3.org/ns/odrl/2/lt", type: "temporal", value: new Date(new Date().valueOf() + 30_000) }, // to: must be smaller than given date
]
}
const temporalWritePolicyOutOfBound: UCPPolicy = {
action: odrlWrite, owner, resource, requestingParty,
constraints: [
{ operator: "http://www.w3.org/ns/odrl/2/gt", type: "temporal", value: new Date("2024-01-01") }, // from: must be greater than given date
{ operator: "http://www.w3.org/ns/odrl/2/lt", type: "temporal", value: new Date("2024-01-02") }, // to: must be smaller than given date
]
}
const temporalWritePolicyWithinBound: UCPPolicy = {
action: odrlWrite, owner, resource, requestingParty,
constraints: [
{ operator: "http://www.w3.org/ns/odrl/2/gt", type: "temporal", value: new Date(0) }, // from: must be greater than given date
{ operator: "http://www.w3.org/ns/odrl/2/lt", type: "temporal", value: new Date(new Date().valueOf() + 30_000) }, // to: must be smaller than given date
]
}
const usePolicy: UCPPolicy = { action: odrlUse, owner, resource, requestingParty }
// load plugin
const plugins = { "http://example.org/dataUsage": new UcpPlugin() }
// instantiate koreografeye policy executor
const policyExecutor = new PolicyExecutor(plugins)
// ucon storage
const uconRulesStorage = new MemoryUCRulesStorage()
// load N3 Rules from a directory | TODO: utils are needed
const n3Rules: string[] = [crud]
// instantiate the enforcer using the policy executor,
const ucpPatternEnforcement = new UcpPatternEnforcement(uconRulesStorage, n3Rules, new EyeJsReasoner([
"--quiet",
"--nope",
"--pass"]), policyExecutor)
const policy = await basicPolicy(readPolicy)
await uconRulesStorage.addRule(policy.representation)
const result = await ucpPatternEnforcement.calculateAccessModes(readPolicyRequest)
console.log(result);
}
main()