|
1 | 1 | /* eslint-disable max-len */ |
2 | 2 |
|
3 | | -import { Parser, Writer, Store } from 'n3'; |
| 3 | +import { Parser, Writer, Store, DataFactory } from 'n3'; |
4 | 4 | import { SimplePolicy, demoPolicy } from "./policyCreation"; |
5 | 5 |
|
| 6 | +export type PolicyFormData = { |
| 7 | + target: string, |
| 8 | + assignee: string, |
| 9 | + startDate: Date, |
| 10 | + endDate: Date, |
| 11 | + purpose: string, |
| 12 | + description: string, |
| 13 | +} |
| 14 | + |
6 | 15 | const parser = new Parser(); |
7 | 16 | const writer = new Writer(); |
8 | 17 |
|
9 | | -const terms = { |
| 18 | +export const terms = { |
10 | 19 | solid: { |
11 | 20 | umaServer: 'http://www.w3.org/ns/solid/terms#umaServer', |
12 | 21 | viewIndex: 'http://www.w3.org/ns/solid/terms#viewIndex', |
@@ -49,57 +58,64 @@ export async function readPolicyDirectory () { |
49 | 58 | let resourceURIs = resourceQuads.map(q => q.object.value) |
50 | 59 |
|
51 | 60 | console.log('IRIS', responseText, resourceURIs) |
52 | | - const policyObjects = await Promise.all(resourceURIs.map(async (location) => { |
| 61 | + let policyObjects = await Promise.all(resourceURIs.map(async (location) => { |
53 | 62 | const resource = await fetch(location); |
54 | 63 | const resourceText = await resource.text() |
55 | 64 | const policy = await readPolicy(resourceText) |
56 | 65 | return policy |
57 | 66 | })) |
58 | | - |
59 | | - return policyObjects || [] |
| 67 | + policyObjects = policyObjects.filter(e => e !== null) |
| 68 | + return (policyObjects || []) as SimplePolicy[] |
60 | 69 |
|
61 | 70 | } |
62 | 71 |
|
63 | 72 | export async function readPolicy(policyText: string) { |
| 73 | + if (policyText === '') return null; |
64 | 74 | const parsed = await new Parser().parse(policyText) |
65 | 75 | const store = new Store() |
66 | 76 | store.addQuads(parsed) |
67 | 77 | const policyIRI = store.getQuads(null, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'http://www.w3.org/ns/odrl/2/Agreement', null)[0]?.subject.value |
68 | 78 | const ruleIRI = store.getQuads(null, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type', 'http://www.w3.org/ns/odrl/2/Permission', null)[0]?.subject.value |
| 79 | + const description = store.getQuads(null, 'http://purl.org/dc/elements/1.1/description', null, null)[0]?.object.value |
69 | 80 | let simplePolicy: SimplePolicy = { |
70 | 81 | representation: store, |
71 | 82 | policyIRI, |
72 | 83 | ruleIRIs: [ruleIRI], |
73 | 84 | policyText: policyText, |
| 85 | + description, |
74 | 86 | } |
75 | 87 |
|
76 | 88 | return simplePolicy |
77 | 89 | } |
78 | 90 |
|
79 | | -export async function createPolicy(policyText: string) { |
| 91 | +export async function createAndSubmitPolicy(formdata: PolicyFormData) { |
80 | 92 | console.log('Creating policy') |
81 | 93 |
|
82 | 94 | const policyContainer = 'http://localhost:3000/ruben/settings/policies/'; |
83 | 95 |
|
84 | | - |
85 | | - log(`Having been notified in some way of the access request, Ruben could go to his Authz Companion app, and add a policy allowing the requested access.`); |
86 | | - |
87 | | - const startDate = new Date(); |
88 | | - const endDate = new Date(startDate.valueOf() + 14 * 24 * 60 * 60 * 1000); |
89 | | - const purpose = 'urn:solidlab:uma:claims:purpose:age-verification' |
90 | | - const policy = demoPolicy(terms.views.age, terms.agents.vendor, { startDate, endDate, purpose }) |
| 96 | + const policy = demoPolicy(formdata.target, formdata.assignee, |
| 97 | + { startDate: formdata.startDate, endDate: formdata.endDate, purpose: formdata.purpose }) |
| 98 | + |
| 99 | + const descriptionQuad = DataFactory.quad( |
| 100 | + DataFactory.namedNode(policy.policyIRI), |
| 101 | + DataFactory.namedNode('http://purl.org/dc/elements/1.1/description'), |
| 102 | + DataFactory.literal(formdata.description) |
| 103 | + ) |
| 104 | + const policyString = writer.quadsToString(policy.representation.getQuads(null, null, null, null).concat(descriptionQuad)) |
91 | 105 |
|
92 | 106 | // create container if it does not exist yet |
93 | 107 | await initContainer(policyContainer) |
94 | 108 | const policyCreationResponse = await fetch(policyContainer, { |
95 | 109 | method: 'POST', |
96 | 110 | headers: { 'content-type': 'text/turtle' }, |
97 | | - body: writer.quadsToString(policy.representation.getQuads(null, null, null, null)) |
| 111 | + body: policyString |
98 | 112 | }); |
99 | 113 |
|
100 | 114 | if (policyCreationResponse.status !== 201) { log('Adding a policy did not succeed...'); throw 0; } |
101 | 115 |
|
102 | 116 | log(`Now that the policy has been set, and the agent has possibly been notified in some way, the agent can try the access request again.`); |
| 117 | + policy.policyText = policyString |
| 118 | + return policy |
103 | 119 |
|
104 | 120 | } |
105 | 121 |
|
|
0 commit comments