| 1 | import axios from "axios"; |
| 2 | import { logger } from "./log.js"; |
| 3 | import { createProof, getProperty, sha256, writeFile } from "./utils.js"; |
| 4 | |
| 5 | const LICENSE_EUPL12 = |
| 6 | "https://joinup.ec.europa.eu/sites/default/files/custom-page/attachment/2020-03/EUPL-1.2%20EN.txt"; |
| 7 | |
| 8 | export async function buildOpenAPIResources({ |
| 9 | openAPIUrl, |
| 10 | didIssuer, |
| 11 | participantUrl, |
| 12 | virtResourceUrl, |
| 13 | virtResourceWritePath = undefined, |
| 14 | instVirtResourceUrl, |
| 15 | instVirtResourceWritePath = undefined, |
| 16 | license = LICENSE_EUPL12, |
| 17 | }) { |
| 18 | const issuanceDate = new Date().toISOString(); |
| 19 | |
| 20 | const context = [ |
| 21 | "https://www.w3.org/2018/credentials/v1", |
| 22 | "https://w3id.org/security/suites/jws-2020/v1", |
| 23 | "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/trustframework#", |
| 24 | ]; |
| 25 | |
| 26 | const virtualResource = { |
| 27 | "@context": context, |
| 28 | type: "VerifiableCredential", |
| 29 | id: virtResourceUrl, |
| 30 | issuer: didIssuer, |
| 31 | issuanceDate: issuanceDate, |
| 32 | credentialSubject: { |
| 33 | id: virtResourceUrl, |
| 34 | type: "gx:VirtualResource", |
| 35 | "gx:description": `HTTP API that is formally described using the OpenAPI schema at ${openAPIUrl}`, |
| 36 | "gx:copyrightOwnedBy": [participantUrl], |
| 37 | "gx:license": [license], |
| 38 | "gx:policy": [""], |
| 39 | }, |
| 40 | }; |
| 41 | |
| 42 | logger.debug("Fetching OpenAPI schema: %s", openAPIUrl); |
| 43 | const apiSchemaRes = await axios.get(openAPIUrl); |
| 44 | const apiSchema = apiSchemaRes.data; |
| 45 | |
| 46 | const instVirtResourceSubject = { |
| 47 | id: instVirtResourceUrl, |
| 48 | type: ["gx:InstantiatedVirtualResource", "dcat:DataService"], |
| 49 | "gx:maintainedBy": [participantUrl], |
| 50 | "gx:hostedOn": virtResourceUrl, |
| 51 | "gx:instanceOf": virtResourceUrl, |
| 52 | "gx:tenantOwnedBy": [participantUrl], |
| 53 | "gx:serviceAccessPoint": [openAPIUrl], |
| 54 | "dcat:endpointDescription": openAPIUrl, |
| 55 | }; |
| 56 | |
| 57 | if (apiSchema.servers && apiSchema.servers.length > 0) { |
| 58 | const server = apiSchema.servers[0]; |
| 59 | const serverUrl = getProperty(server, "url"); |
| 60 | |
| 61 | if (serverUrl) { |
| 62 | Object.assign(instVirtResourceSubject, { |
| 63 | "dcat:endpointURL": serverUrl, |
| 64 | }); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const instantiatedVirtualResource = { |
| 69 | "@context": [...context, "https://www.w3.org/ns/dcat.jsonld"], |
| 70 | type: "VerifiableCredential", |
| 71 | id: instVirtResourceUrl, |
| 72 | issuer: didIssuer, |
| 73 | issuanceDate: issuanceDate, |
| 74 | credentialSubject: instVirtResourceSubject, |
| 75 | }; |
| 76 | |
| 77 | for (const [doc, writePath] of [ |
| 78 | [virtualResource, virtResourceWritePath], |
| 79 | [instantiatedVirtualResource, instVirtResourceWritePath], |
| 80 | ]) { |
| 81 | Object.assign(doc, { proof: await createProof(doc) }); |
| 82 | |
| 83 | if (writePath) { |
| 84 | await writeFile(writePath, doc); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return { |
| 89 | instantiatedVirtualResource, |
| 90 | virtualResource, |
| 91 | }; |
| 92 | } |
| 93 | |
| 94 | export async function buildServiceOffering({ |
| 95 | didIssuer, |
| 96 | legalParticipantUrl, |
| 97 | termsConditionsUrl, |
| 98 | termsConditionsHash = undefined, |
| 99 | serviceOfferingUrl, |
| 100 | serviceOfferingWritePath = undefined, |
| 101 | aggregatedResourceUrls = undefined, |
| 102 | }) { |
| 103 | const issuanceDate = new Date().toISOString(); |
| 104 | |
| 105 | if (!termsConditionsHash) { |
| 106 | const tcRes = await axios.get(termsConditionsUrl, { |
| 107 | responseType: "arraybuffer", |
| 108 | }); |
| 109 | |
| 110 | termsConditionsHash = sha256(tcRes.data); |
| 111 | } |
| 112 | |
| 113 | const doc = { |
| 114 | "@context": [ |
| 115 | "https://www.w3.org/2018/credentials/v1", |
| 116 | "https://w3id.org/security/suites/jws-2020/v1", |
| 117 | "https://registry.lab.gaia-x.eu/development/api/trusted-shape-registry/v1/shapes/jsonld/trustframework#", |
| 118 | ], |
| 119 | type: "VerifiableCredential", |
| 120 | id: serviceOfferingUrl, |
| 121 | issuer: didIssuer, |
| 122 | issuanceDate: issuanceDate, |
| 123 | credentialSubject: { |
| 124 | id: serviceOfferingUrl, |
| 125 | type: "gx:ServiceOffering", |
| 126 | "gx:providedBy": { |
| 127 | id: legalParticipantUrl, |
| 128 | }, |
| 129 | "gx:policy": "", |
| 130 | "gx:termsAndConditions": { |
| 131 | "gx:URL": termsConditionsUrl, |
| 132 | "gx:hash": termsConditionsHash, |
| 133 | }, |
| 134 | "gx:dataAccountExport": { |
| 135 | "gx:requestType": "API", |
| 136 | "gx:accessType": "digital", |
| 137 | "gx:formatType": "application/json", |
| 138 | }, |
| 139 | }, |
| 140 | }; |
| 141 | |
| 142 | // ToDo: Remove comment |
| 143 | // We started getting errors like the following: |
| 144 | // Error: Error in Compliance API request: |
| 145 | // {"statusCode":409,"message":{"conforms":false,"results":["ERROR: https://gaiax.cticpoc.com/.well-known/serviceoffering.json null: "]},"error":"Conflict"} |
| 146 | // It seems the conformance errors come from gx:aggregationOf |
| 147 | // if (aggregatedResourceUrls) { |
| 148 | // Object.assign(doc.credentialSubject, { |
| 149 | // "gx:aggregationOf": aggregatedResourceUrls, |
| 150 | // }); |
| 151 | // } |
| 152 | |
| 153 | const proof = await createProof(doc); |
| 154 | Object.assign(doc, { proof }); |
| 155 | |
| 156 | if (serviceOfferingWritePath) { |
| 157 | await writeFile(serviceOfferingWritePath, doc); |
| 158 | } |
| 159 | |
| 160 | return doc; |
| 161 | } |