|
| 1 | +import { |
| 2 | + AccessControl, |
| 3 | + AccessControlResource, |
| 4 | + AclResource, |
| 5 | + AcrDataset, |
| 6 | + Authorization, |
| 7 | + Matcher, |
| 8 | + Policy |
| 9 | +} from "@solid/object" |
| 10 | +import type { DatasetCore } from "@rdfjs/types" |
| 11 | +import { suite, test } from "node:test" |
| 12 | +import { DataFactory, Parser, Store, Writer } from "n3" |
| 13 | + |
| 14 | +//#region WAC -> ACP |
| 15 | + |
| 16 | +const wacRdf = ` |
| 17 | +BASE <http://example.com/> |
| 18 | +PREFIX : <http://www.w3.org/ns/auth/acl#> |
| 19 | +
|
| 20 | +<ControlReadWrite> |
| 21 | + a :Authorization ; |
| 22 | + :accessTo <resource> ; |
| 23 | + :agent <someone> ; |
| 24 | + :mode :Control, :Read, :Write ; |
| 25 | +. |
| 26 | +
|
| 27 | +<ControlReadWriteDefault> |
| 28 | + a :Authorization ; |
| 29 | + :default <resource> ; |
| 30 | + :agent <someone> ; |
| 31 | + :mode :Control, :Read, :Write ; |
| 32 | +. |
| 33 | +
|
| 34 | +<ReadDefault> |
| 35 | + a :Authorization ; |
| 36 | + :default <resource> ; |
| 37 | + :agent <someoneElse> ; |
| 38 | + :mode :Read ; |
| 39 | + # :origin <http://bot>, <http://example.com>, <http://webid> ; |
| 40 | +. |
| 41 | +`; |
| 42 | + |
| 43 | +async function wacToAcp() { |
| 44 | + const wac = new AclResource(read(wacRdf), DataFactory) |
| 45 | + const result = new Store() |
| 46 | + |
| 47 | + for (const auth of wac.authorizations) { |
| 48 | + processAuthorization(auth, result) |
| 49 | + } |
| 50 | + |
| 51 | + console.log("--------------------------") |
| 52 | + console.log("WAC -> ACP") |
| 53 | + console.log(await write(result)) |
| 54 | + console.log("--------------------------") |
| 55 | +} |
| 56 | + |
| 57 | +function processAuthorization(auth: Authorization, result: DatasetCore) { |
| 58 | + if (auth.origin.size > 0) { |
| 59 | + throw new Error("WAC origin cannot be translated to ACP") |
| 60 | + } |
| 61 | + |
| 62 | + if (auth.accessTo !== undefined) { |
| 63 | + const acr = new AccessControlResource(auth.factory.blankNode(), result, auth.factory) |
| 64 | + populatePolicy(acr, auth, auth.accessTo, acr.accessControl) |
| 65 | + } |
| 66 | + |
| 67 | + if (auth.default !== undefined) { |
| 68 | + const acr = new AccessControlResource(auth.factory.blankNode(), result, auth.factory) |
| 69 | + populatePolicy(acr, auth, auth.default, acr.memberAccessControl) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +function populatePolicy(acr: AccessControlResource, auth: Authorization, resource: string, accessControls: Set<AccessControl>) { |
| 74 | + const accessControl = new AccessControl(acr.factory.blankNode(), acr.dataset, acr.factory) |
| 75 | + const policy = new Policy(acr.factory.blankNode(), acr.dataset, acr.factory) |
| 76 | + const matcher = new Matcher(acr.factory.blankNode(), acr.dataset, acr.factory) |
| 77 | + |
| 78 | + accessControls.add(accessControl) |
| 79 | + accessControl.apply.add(policy) |
| 80 | + policy.allOf.add(matcher) |
| 81 | + |
| 82 | + acr.resource = resource |
| 83 | + |
| 84 | + for (const mode of auth.mode) { |
| 85 | + policy.allow.add(mode) |
| 86 | + } |
| 87 | + |
| 88 | + for (const agent of auth.agent) { |
| 89 | + matcher.agent.add(agent) |
| 90 | + } |
| 91 | + |
| 92 | + if (auth.agentGroup !== undefined) { |
| 93 | + for (const member of auth.agentGroup.hasMember) { |
| 94 | + matcher.agent.add(member) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (auth.agentClass.has(ACL.AuthenticatedAgent)) { |
| 99 | + matcher.agent.add(ACP.AuthenticatedAgent) |
| 100 | + } |
| 101 | + |
| 102 | + if (auth.agentClass.has(FOAF.Agent)) { |
| 103 | + matcher.agent.add(ACP.PublicAgent) |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +//#endregion |
| 108 | + |
| 109 | +//#region ACP -> WAC |
| 110 | + |
| 111 | +const acpRdf = ` |
| 112 | +`; |
| 113 | + |
| 114 | +function acpToWac() { |
| 115 | + const acp = new AcrDataset(read(acpRdf), DataFactory) |
| 116 | + |
| 117 | + if (acp.acr != undefined) { |
| 118 | + processAcr(acp.acr) |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +function processAcr(acr: AccessControlResource) { |
| 123 | + |
| 124 | +} |
| 125 | + |
| 126 | +//#endregion |
| 127 | + |
| 128 | +//#region Utilities |
| 129 | + |
| 130 | +//#region Namespaces |
| 131 | + |
| 132 | +const ACL = { |
| 133 | + AuthenticatedAgent: "http://www.w3.org/ns/auth/acl#AuthenticatedAgent", |
| 134 | +} as const |
| 135 | + |
| 136 | +const ACP = { |
| 137 | + AuthenticatedAgent: "http://www.w3.org/ns/solid/acp#AuthenticatedAgent", |
| 138 | + PublicAgent: "http://www.w3.org/ns/solid/acp#PublicAgent", |
| 139 | +} as const |
| 140 | + |
| 141 | +const FOAF = { |
| 142 | + Agent: "http://xmlns.com/foaf/0.1/Agent", |
| 143 | +} as const |
| 144 | + |
| 145 | +//#endregion |
| 146 | + |
| 147 | +await suite("Convert access control representation", async () => { |
| 148 | + await test("From WAC to ACP", wacToAcp) |
| 149 | + await test("From ACP to WAC", acpToWac) |
| 150 | +}) |
| 151 | + |
| 152 | +function read(rdf: string): DatasetCore { |
| 153 | + const dataset = new Store() |
| 154 | + dataset.addQuads(new Parser().parse(rdf)) |
| 155 | + |
| 156 | + return dataset |
| 157 | +} |
| 158 | + |
| 159 | +export function write(dataset: DatasetCore): Promise<string> { |
| 160 | + return new Promise((resolve, reject) => { |
| 161 | + const writer = new Writer({ |
| 162 | + prefixes: { |
| 163 | + "": "http://example.com/", |
| 164 | + acl: "http://www.w3.org/ns/auth/acl#", |
| 165 | + acp: "http://www.w3.org/ns/solid/acp#", |
| 166 | + } |
| 167 | + }) |
| 168 | + |
| 169 | + writer.addQuads([...dataset]) |
| 170 | + |
| 171 | + writer.end((error, result) => { |
| 172 | + if (error) { |
| 173 | + reject(error) |
| 174 | + } else { |
| 175 | + resolve(result) |
| 176 | + } |
| 177 | + }) |
| 178 | + }) |
| 179 | +} |
| 180 | + |
| 181 | +//#endregion |
0 commit comments