|
| 1 | +import { AccessControl, AccessControlResource, AcrDataset } from "../acp/mod.js" |
| 2 | +import type { DatasetCore } from "@rdfjs/types" |
| 3 | +import { Authorization } from "../wac/mod.js" |
| 4 | +import { ACL, ACP, FOAF } from "../vocabulary/mod.js" |
| 5 | +import { AcpToWacError } from "./AcpToWacError.js" |
| 6 | + |
| 7 | +export function acpToWac(source: AcrDataset, target: DatasetCore) { |
| 8 | + if (source.acr === undefined) { |
| 9 | + return |
| 10 | + } |
| 11 | + |
| 12 | + const auth = new Authorization(source.acr.factory.blankNode(), target, source.acr.factory) |
| 13 | + auth.type.add(ACL.Authorization) |
| 14 | + |
| 15 | + processAcr(source.acr, auth) |
| 16 | +} |
| 17 | + |
| 18 | +function processAcr(source: AccessControlResource, target: Authorization) { |
| 19 | + if (source.accessControl.size > 0) { |
| 20 | + target.accessTo = source.resource |
| 21 | + } |
| 22 | + |
| 23 | + if (source.memberAccessControl.size > 0) { |
| 24 | + target.default = source.resource |
| 25 | + } |
| 26 | + |
| 27 | + for (const ac of source.accessControl) { |
| 28 | + processAc(ac, target) |
| 29 | + } |
| 30 | + |
| 31 | + for (const ac of source.memberAccessControl) { |
| 32 | + processAc(ac, target) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function processAc(source: AccessControl, target: Authorization) { |
| 37 | + for (const policy of source.apply) { |
| 38 | + if (policy.deny.size > 0) { |
| 39 | + throw new AcpToWacError("deny") |
| 40 | + } |
| 41 | + |
| 42 | + if (policy.anyOf.size > 0) { |
| 43 | + throw new AcpToWacError("anyOf") |
| 44 | + } |
| 45 | + |
| 46 | + if (policy.noneOf.size > 0) { |
| 47 | + throw new AcpToWacError("noneOf") |
| 48 | + } |
| 49 | + |
| 50 | + for (const mode of policy.allow) { |
| 51 | + target.mode.add(mode) |
| 52 | + } |
| 53 | + |
| 54 | + for (const matcher of policy.allOf) { |
| 55 | + if (matcher.client.size > 0) { |
| 56 | + throw new AcpToWacError("client") |
| 57 | + } |
| 58 | + |
| 59 | + if (matcher.issuer.size > 0) { |
| 60 | + throw new AcpToWacError("issuer") |
| 61 | + } |
| 62 | + |
| 63 | + if (matcher.vc.size > 0) { |
| 64 | + throw new AcpToWacError("vc") |
| 65 | + } |
| 66 | + |
| 67 | + for (const agent of matcher.agent) { |
| 68 | + if (agent === ACP.CreatorAgent) { |
| 69 | + throw new AcpToWacError("CreatorAgent") |
| 70 | + } |
| 71 | + |
| 72 | + if (agent === ACP.OwnerAgent) { |
| 73 | + throw new AcpToWacError("OwnerAgent") |
| 74 | + } |
| 75 | + |
| 76 | + if (agent === ACP.PublicAgent) { |
| 77 | + target.agent.add(FOAF.Agent) |
| 78 | + } else if (agent === ACP.AuthenticatedAgent) { |
| 79 | + target.agent.add(ACL.AuthenticatedAgent) |
| 80 | + } else { |
| 81 | + target.agent.add(agent) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments