{"version":3,"file":"getWacRule.mjs","names":["dataset"],"sources":["../../src/wac/getWacRule.ts"],"sourcesContent":["import { GetWacRuleSuccess } from \"./results/GetWacRuleSuccess\";\nimport { AuthorizationShapeType } from \"../_ldo/wac.shapeTypes\";\nimport type { AccessModeList, WacRule } from \"./WacRule\";\nimport type { Authorization } from \"../_ldo/wac.typings\";\nimport { WacRuleAbsent } from \"./results/WacRuleAbsent\";\nimport {\n  HttpErrorResult,\n  type HttpErrorResultType,\n} from \"../requester/results/error/HttpErrorResult\";\nimport { NoncompliantPodError } from \"../requester/results/error/NoncompliantPodError\";\nimport type { UnexpectedResourceError } from \"@ldo/connected\";\nimport type { SolidLeaf } from \"../resources/SolidLeaf\";\nimport type { SolidContainer } from \"../resources/SolidContainer\";\nimport { guaranteeFetch } from \"../util/guaranteeFetch\";\nimport type { BasicRequestOptions } from \"../requester/requests/requestOptions\";\nimport { rawTurtleToDataset } from \"../util/rdfUtils\";\n\nexport type GetWacRuleError<ResourceType extends SolidContainer | SolidLeaf> =\n  | HttpErrorResultType<ResourceType>\n  | NoncompliantPodError<ResourceType>\n  | UnexpectedResourceError<ResourceType>;\n\nexport type GetWacRuleResult<ResourceType extends SolidContainer | SolidLeaf> =\n  | GetWacRuleSuccess<ResourceType>\n  | GetWacRuleError<ResourceType>\n  | WacRuleAbsent<ResourceType>;\n\n/**\n * Given the URI of an ACL document, return the Web Access Control (WAC) rules\n * @param aclUri: The URI for the ACL document\n * @param options: Options object to include an authenticated fetch function\n * @returns GetWacRuleResult\n */\nexport async function getWacRuleWithAclUri(\n  aclUri: string,\n  resource: SolidContainer,\n  options?: BasicRequestOptions,\n): Promise<GetWacRuleResult<SolidContainer>>;\nexport async function getWacRuleWithAclUri(\n  aclUri: string,\n  resource: SolidLeaf,\n  options?: BasicRequestOptions,\n): Promise<GetWacRuleResult<SolidLeaf>>;\nexport async function getWacRuleWithAclUri(\n  aclUri: string,\n  resource: SolidLeaf | SolidContainer,\n  options?: BasicRequestOptions,\n): Promise<GetWacRuleResult<SolidLeaf | SolidContainer>>;\nexport async function getWacRuleWithAclUri(\n  aclUri: string,\n  resource: SolidLeaf | SolidContainer,\n  options?: BasicRequestOptions,\n): Promise<GetWacRuleResult<SolidLeaf | SolidContainer>> {\n  const fetch = guaranteeFetch(options?.fetch);\n  const response = await fetch(aclUri);\n  const errorResult = HttpErrorResult.checkResponse(resource, response);\n  if (errorResult) return errorResult;\n\n  if (response.status === 404) {\n    return new WacRuleAbsent(resource);\n  }\n\n  // Parse Turtle\n  const rawTurtle = await response.text();\n  const rawTurtleResult = await rawTurtleToDataset(rawTurtle, aclUri);\n  if (rawTurtleResult instanceof Error)\n    return new NoncompliantPodError(resource, rawTurtleResult.message);\n  const dataset = rawTurtleResult;\n  const authorizations = dataset\n    .usingType(AuthorizationShapeType)\n    .matchSubject(\n      \"http://www.w3.org/1999/02/22-rdf-syntax-ns#type\",\n      \"http://www.w3.org/ns/auth/acl#Authorization\",\n    );\n\n  const wacRule: WacRule = {\n    public: {\n      read: false,\n      write: false,\n      append: false,\n      control: false,\n    },\n    authenticated: {\n      read: false,\n      write: false,\n      append: false,\n      control: false,\n    },\n    agent: {},\n  };\n\n  function applyAccessModesToList(\n    accessModeList: AccessModeList,\n    authorization: Authorization,\n  ): void {\n    authorization.mode?.forEach((mode) => {\n      accessModeList[mode[\"@id\"].toLowerCase()] = true;\n    });\n  }\n\n  authorizations.forEach((authorization) => {\n    if (\n      authorization.agentClass?.some(\n        (agentClass) => agentClass[\"@id\"] === \"Agent\",\n      )\n    ) {\n      applyAccessModesToList(wacRule.public, authorization);\n      applyAccessModesToList(wacRule.authenticated, authorization);\n    }\n    if (\n      authorization.agentClass?.some(\n        (agentClass) => agentClass[\"@id\"] === \"AuthenticatedAgent\",\n      )\n    ) {\n      applyAccessModesToList(wacRule.authenticated, authorization);\n    }\n    authorization.agent?.forEach((agent) => {\n      if (!wacRule.agent[agent[\"@id\"]]) {\n        wacRule.agent[agent[\"@id\"]] = {\n          read: false,\n          write: false,\n          append: false,\n          control: false,\n        };\n      }\n      applyAccessModesToList(wacRule.agent[agent[\"@id\"]], authorization);\n    });\n  });\n\n  return new GetWacRuleSuccess(resource, wacRule);\n}\n"],"mappings":";;;;;;;;AAgDA,eAAsB,qBACpB,QACA,UACA,SACuD;CAEvD,MAAM,WAAW,MADH,eAAe,SAAS,MACV,CAAC,OAAO;CACpC,MAAM,cAAc,gBAAgB,cAAc,UAAU,SAAS;AACrE,KAAI,YAAa,QAAO;AAExB,KAAI,SAAS,WAAW,IACtB,QAAO,IAAI,cAAc,SAAS;CAKpC,MAAM,kBAAkB,MAAM,mBAAmB,MADzB,SAAS,MAAM,EACqB,OAAO;AACnE,KAAI,2BAA2B,MAC7B,QAAO,IAAI,qBAAqB,UAAU,gBAAgB,QAAQ;CAEpE,MAAM,iBAAiBA,gBACpB,UAAU,uBAAuB,CACjC,aACC,mDACA,8CACD;CAEH,MAAM,UAAmB;EACvB,QAAQ;GACN,MAAM;GACN,OAAO;GACP,QAAQ;GACR,SAAS;GACV;EACD,eAAe;GACb,MAAM;GACN,OAAO;GACP,QAAQ;GACR,SAAS;GACV;EACD,OAAO,EAAE;EACV;CAED,SAAS,uBACP,gBACA,eACM;AACN,gBAAc,MAAM,SAAS,SAAS;AACpC,kBAAe,KAAK,OAAO,aAAa,IAAI;IAC5C;;AAGJ,gBAAe,SAAS,kBAAkB;AACxC,MACE,cAAc,YAAY,MACvB,eAAe,WAAW,WAAW,QACvC,EACD;AACA,0BAAuB,QAAQ,QAAQ,cAAc;AACrD,0BAAuB,QAAQ,eAAe,cAAc;;AAE9D,MACE,cAAc,YAAY,MACvB,eAAe,WAAW,WAAW,qBACvC,CAED,wBAAuB,QAAQ,eAAe,cAAc;AAE9D,gBAAc,OAAO,SAAS,UAAU;AACtC,OAAI,CAAC,QAAQ,MAAM,MAAM,QACvB,SAAQ,MAAM,MAAM,UAAU;IAC5B,MAAM;IACN,OAAO;IACP,QAAQ;IACR,SAAS;IACV;AAEH,0BAAuB,QAAQ,MAAM,MAAM,SAAS,cAAc;IAClE;GACF;AAEF,QAAO,IAAI,kBAAkB,UAAU,QAAQ"}