| 1 | import { logger } from "./log.js"; |
| 2 | import axios from "axios"; |
| 3 | |
| 4 | export async function generateSelfDescription({ apiSpec, type, didIssuer, certInfo }) { |
| 5 | logger.info(`Generando Self-Description desde especificación ${type}...`); |
| 6 | |
| 7 | try { |
| 8 | // Cargar y validar la especificación API |
| 9 | const apiDocument = await loadAPISpec(apiSpec); |
| 10 | |
| 11 | // Crear estructura base del Self-Description |
| 12 | const selfDescription = { |
| 13 | "@context": [ |
| 14 | "https://www.w3.org/2018/credentials/v1", |
| 15 | "https://w3id.org/gaia-x/service#" |
| 16 | ], |
| 17 | type: ["VerifiableCredential", "GaiaXService"], |
| 18 | issuer: didIssuer, |
| 19 | issuanceDate: new Date().toISOString(), |
| 20 | credentialSubject: { |
| 21 | id: didIssuer, |
| 22 | type: "Service", |
| 23 | name: apiDocument.info.title, |
| 24 | description: apiDocument.info.description, |
| 25 | version: apiDocument.info.version, |
| 26 | providedBy: didIssuer, |
| 27 | certificate: { |
| 28 | issuer: certInfo.issuer, |
| 29 | subject: certInfo.subject, |
| 30 | validFrom: certInfo.validFrom, |
| 31 | validUntil: certInfo.validUntil |
| 32 | } |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | logger.debug("Self-Description generado exitosamente"); |
| 37 | return selfDescription; |
| 38 | |
| 39 | } catch (error) { |
| 40 | logger.error("Error generando Self-Description:", error); |
| 41 | throw new Error(`Error en la generación del Self-Description: ${error.message}`); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | async function loadAPISpec(specPath) { |
| 46 | try { |
| 47 | if (specPath.startsWith('http')) { |
| 48 | const response = await axios.get(specPath); |
| 49 | return response.data; |
| 50 | } else { |
| 51 | // Si es una ruta local, importar el archivo |
| 52 | return (await import(specPath)).default; |
| 53 | } |
| 54 | } catch (error) { |
| 55 | throw new Error(`Error cargando especificación API: ${error.message}`); |
| 56 | } |
| 57 | } |