UNPKG

@sphereon/ssi-sdk.ms-request-api

Version:

323 lines (317 loc) • 12.1 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __commonJS = (cb, mod) => function __require() { return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // plugin.schema.json var require_plugin_schema = __commonJS({ "plugin.schema.json"(exports, module2) { module2.exports = { IMsRequestApi: { components: { schemas: { IClientIssueRequest: { type: "object", properties: { authenticationInfo: { $ref: "#/components/schemas/IMsAuthenticationClientCredentialArgs" }, clientIssuanceConfig: { $ref: "#/components/schemas/IClientIssuanceConfig" }, claims: { $ref: "#/components/schemas/CredentialSubject" } }, required: ["authenticationInfo", "clientIssuanceConfig", "claims"], additionalProperties: false }, IMsAuthenticationClientCredentialArgs: { type: "object", properties: { azClientId: { type: "string" }, azTenantId: { type: "string" }, azClientSecret: { type: "string" }, credentialManifestUrl: { type: "string" }, authority: { type: "string" }, region: { type: "string" }, scopes: { type: "array", items: { type: "string" } }, skipCache: { type: "boolean" }, piiLoggingEnabled: { type: "boolean" }, logLevel: { $ref: "#/components/schemas/LogLevel" } }, required: ["azClientId", "azTenantId", "azClientSecret"], additionalProperties: false, description: "azClientId: clientId of the application you're trying to login azClientSecret: secret of the application you're trying to login azTenantId: your MS Azure tenantId credentialManifestUrl: url of your credential manifest. usually in following format: https://beta.eu.did.msidentity.com/v1.0/<tenant_id>/verifiableCredential/contracts/<verifiable_credential_schema> authority: optional. if not provided, we'll use the azClientId to create the Tenanted format if provided should be one of these two formats: - Tenanted: https://login.microsoftonline.com/{tenant}/, where {tenant} is either the GUID representing the tenant ID or a domain name associated with the tenant. - Work and school accounts: https://login.microsoftonline.com/organizations/. region?: if present will use the provided, if not will make a request to determine the region scopes?: scopes that you want to access via this authentication skipCache?: whether to skip cache piiLoggingEnabled?: if not provided defaults to false logLevel?: can be one of these values: Error = 0, Warning = 1, Info = 2, Verbose = 3, Trace = 4 if not provided defaults to LogLevel.Verbose" }, LogLevel: { type: "number", enum: [0, 1, 2, 3, 4], description: "Log message level." }, IClientIssuanceConfig: { type: "object", properties: { authority: { type: "string" }, includeQRCode: { type: "boolean" }, registration: { $ref: "#/components/schemas/Registration" }, callback: { $ref: "#/components/schemas/Callback" }, issuance: { $ref: "#/components/schemas/IClientIssuance" } }, required: ["authority", "includeQRCode", "registration", "callback", "issuance"], additionalProperties: false }, Registration: { type: "object", properties: { clientName: { type: "string" } }, required: ["clientName"], additionalProperties: false }, Callback: { type: "object", properties: { url: { type: "string" }, state: { type: "string" }, headers: { $ref: "#/components/schemas/Headers" } }, required: ["url", "state", "headers"], additionalProperties: false }, Headers: { type: "object", properties: { apiKey: { type: "string" } }, required: ["apiKey"], additionalProperties: false }, IClientIssuance: { type: "object", properties: { type: { type: "string" }, manifest: { type: "string" }, pin: { $ref: "#/components/schemas/Pin" } }, required: ["type", "manifest", "pin"], additionalProperties: false }, Pin: { type: "object", properties: { value: { type: "string" }, length: { type: "number" } }, required: ["value", "length"], additionalProperties: false }, CredentialSubject: { type: "object" }, IIssueRequestResponse: { type: "object", properties: { id: { type: "string" }, requestId: { type: "string" }, url: { type: "string" }, expiry: { type: "string", format: "date-time" }, pin: { type: "string" } }, required: ["id", "requestId", "url", "expiry", "pin"], additionalProperties: false } }, methods: { issuanceRequestMsVc: { description: "", arguments: { $ref: "#/components/schemas/IClientIssueRequest" }, returnType: { $ref: "#/components/schemas/IIssueRequestResponse" } } } } } }; } }); // src/index.ts var index_exports = {}; __export(index_exports, { MsRequestApi: () => MsRequestApi, fetchIssuanceRequestMs: () => fetchIssuanceRequestMs, generatePin: () => generatePin, schema: () => schema }); module.exports = __toCommonJS(index_exports); // src/agent/MsRequestApi.ts var import_ssi_sdk = require("@sphereon/ssi-sdk.ms-authenticator"); // src/IssuerUtil.ts var import_cross_fetch = require("cross-fetch"); async function fetchIssuanceRequestMs(issuanceInfo, accessToken, msIdentityHostName) { const requestEndpoint = `${msIdentityHostName}${issuanceInfo.authenticationInfo.azTenantId}/verifiablecredentials/request`; const payload = JSON.stringify(issuanceInfo.issuanceConfig); const fetchOptions = { method: "POST", body: payload, headers: { "Content-Type": "application/json", "Content-Length": payload.length.toString(), Authorization: `Bearer ${accessToken}` } }; const response = await (0, import_cross_fetch.fetch)(requestEndpoint, fetchOptions); return await response.json(); } __name(fetchIssuanceRequestMs, "fetchIssuanceRequestMs"); function generatePin(digits) { const add = 1; let max = 12 - add; max = Math.pow(10, digits + add); const min = max / 10; const number = Math.floor(Math.random() * (max - min + 1)) + min; return ("" + number).substring(add); } __name(generatePin, "generatePin"); // src/agent/MsRequestApi.ts var MsRequestApi = class { static { __name(this, "MsRequestApi"); } clients = /* @__PURE__ */ new Map(); methods = { issuanceRequestMsVc: this.issuanceRequestMsVc.bind(this) }; /** {@inheritDoc IMsRequestApi.issuanceRequestMsVc} */ async issuanceRequestMsVc(clientIssueRequest, context) { const id = (0, import_ssi_sdk.determineMSAuthId)(clientIssueRequest.authenticationInfo); if (!this.clients.has(id)) { this.clients.set(id, await (0, import_ssi_sdk.newMSClientCredentialAuthenticator)(clientIssueRequest.authenticationInfo)); } const clientInfo = this.clients.get(id); if (!clientInfo) { throw Error(`Could not get client from arguments for id: ${id}`); } const authResult = await (0, import_ssi_sdk.getMSClientCredentialAccessToken)(clientIssueRequest.authenticationInfo, { confidentialClient: clientInfo.confidentialClient }); const accessToken = authResult.accessToken; const msIdentityHostName = await (0, import_ssi_sdk.assertEntraCredentialManifestUrlInCorrectRegion)(clientIssueRequest.authenticationInfo); if (!clientIssueRequest.authenticationInfo.azTenantId) { throw new Error("azTenantId is missing."); } if (clientIssueRequest.clientIssuanceConfig.issuance.pin) { clientIssueRequest.clientIssuanceConfig.issuance.pin.value = generatePin(clientIssueRequest.clientIssuanceConfig.issuance.pin.length); } const issuance = { type: clientIssueRequest.clientIssuanceConfig.issuance.type, manifest: clientIssueRequest.clientIssuanceConfig.issuance.manifest, pin: clientIssueRequest.clientIssuanceConfig.issuance.pin, claims: clientIssueRequest.claims }; const issuanceConfig = { authority: clientIssueRequest.clientIssuanceConfig.authority, includeQRCode: clientIssueRequest.clientIssuanceConfig.includeQRCode, registration: clientIssueRequest.clientIssuanceConfig.registration, callback: clientIssueRequest.clientIssuanceConfig.callback, issuance }; const issueRequest = { authenticationInfo: clientIssueRequest.authenticationInfo, issuanceConfig }; const resp = await fetchIssuanceRequestMs(issueRequest, accessToken, msIdentityHostName); resp.id = issueRequest.issuanceConfig.callback.state; if (issueRequest.issuanceConfig.issuance.pin) { resp.pin = issueRequest.issuanceConfig.issuance.pin.value; } return resp; } }; // src/index.ts var schema = require_plugin_schema(); //# sourceMappingURL=index.cjs.map