UNPKG

@sphereon/ssi-sdk.oid4vci-issuer-store

Version:

773 lines (769 loc) • 26.3 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 = { IDidAuthSiopOpAuthenticator: { components: { schemas: { IGetSiopSessionArgs: { type: "object", properties: { sessionId: { type: "string" }, additionalProperties: false }, required: ["sessionId"], description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.getSessionForSiop } " }, IRegisterSiopSessionArgs: { type: "object", properties: { identifier: { type: "object", properties: { did: { type: "string" }, alias: { type: "string" }, provider: { type: "string" }, controllerKeyId: { type: "string" }, keys: { type: "array", items: { type: "object", properties: { additionalProperties: true } } }, services: { type: "array", items: { type: "object", properties: { additionalProperties: true } } } }, additionalProperties: false, required: ["did", "provider", "keys", "services"] }, sessionId: { type: "string" }, expiresIn: { type: "number" }, additionalProperties: false }, required: ["identifier"], description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.registerSessionForSiop } " }, IRemoveSiopSessionArgs: { type: "object", properties: { sessionId: { type: "string" }, additionalProperties: false }, required: ["sessionId"], description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.removeSessionForSiop } " }, IAuthenticateWithSiopArgs: { type: "object", properties: { sessionId: { type: "string" }, stateId: { type: "string" }, redirectUrl: { type: "string" }, additionalProperties: false }, required: ["sessionId", "stateId", "redirectUrl"], description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.authenticateWithSiop } " }, IResponse: { type: "object", properties: { status: { type: "number" }, additionalProperties: true }, required: ["status"], description: "Result of {@link DidAuthSiopOpAuthenticator.authenticateWithSiop & DidAuthSiopOpAuthenticator.sendSiopAuthenticationResponse } " }, IGetSiopAuthenticationRequestFromRpArgs: { type: "object", properties: { sessionId: { type: "string" }, stateId: { type: "string" }, redirectUrl: { type: "string" }, additionalProperties: false }, required: ["sessionId", "stateId", "redirectUrl"], description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.getSiopAuthenticationRequestFromRP } " }, ParsedAuthenticationRequestURI: { type: "object", properties: { jwt: { type: "string" }, requestPayload: { type: "object", properties: { additionalProperties: true } }, registration: { type: "object", properties: { additionalProperties: true } }, additionalProperties: false }, required: ["jwt", "requestPayload", "registration"], description: "Result of {@link DidAuthSiopOpAuthenticator.getSiopAuthenticationRequestFromRP } " }, IGetSiopAuthenticationRequestDetailsArgs: { type: "object", properties: { sessionId: { type: "string" }, verifiedAuthenticationRequest: { type: "object", properties: { additionalProperties: true } }, credentialFilter: { type: "object", properties: { additionalProperties: true } }, additionalProperties: false }, required: ["sessionId", "verifiedAuthenticationRequest"], description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.getSiopAuthenticationRequestDetails } " }, IAuthRequestDetails: { type: "object", properties: { id: { type: "string" }, alsoKnownAs: { type: "array", items: { type: "string" } }, vpResponseOpts: { type: "object", properties: { additionalProperties: true } }, additionalProperties: false }, required: ["id", "vpResponseOpts"], description: "Result of {@link DidAuthSiopOpAuthenticator.getSiopAuthenticationRequestDetails } " }, IVerifySiopAuthenticationRequestUriArgs: { type: "object", properties: { sessionId: { type: "string" }, ParsedAuthenticationRequestURI: { type: "object", properties: { additionalProperties: true } }, additionalProperties: false }, required: ["sessionId", "ParsedAuthenticationRequestURI"], description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.verifySiopAuthenticationRequestURI } " }, VerifiedAuthorizationRequest: { type: "object", properties: { payload: { type: "object", properties: { additionalProperties: true } }, presentationDefinitions: { type: "object", properties: { additionalProperties: true } }, verifyOpts: { type: "object", properties: { additionalProperties: true } }, additionalProperties: false }, required: ["payload", "verifyOpts"], description: "Result of {@link DidAuthSiopOpAuthenticator.verifySiopAuthenticationRequestURI } " }, ISendSiopAuthenticationResponseArgs: { type: "object", properties: { sessionId: { type: "string" }, verifiedAuthenticationRequest: { type: "object", properties: { additionalProperties: true } }, verifiablePresentationResponse: { type: "object", properties: { additionalProperties: true } }, additionalProperties: false }, required: ["sessionId", "verifiedAuthenticationRequest"], description: "Arguments needed for {@link DidAuthSiopOpAuthenticator.sendSiopAuthenticationResponse } " } }, methods: { getSessionForSiop: { description: "Get SIOP session", arguments: { $ref: "#/components/schemas/IGetSiopSessionArgs" }, returnType: "object" }, registerSessionForSiop: { description: "Register SIOP session", arguments: { $ref: "#/components/schemas/IRegisterSiopSessionArgs" }, returnType: "object" }, removeSessionForSiop: { description: "Remove SIOP session", arguments: { $ref: "#/components/schemas/IRemoveSiopSessionArgs" }, returnType: "boolean" }, authenticateWithSiop: { description: "Authenticate using DID Auth SIOP", arguments: { $ref: "#/components/schemas/IAuthenticateWithSiopArgs" }, returnType: { $ref: "#/components/schemas/Response" } }, getSiopAuthenticationRequestFromRP: { description: "Get authentication request from RP", arguments: { $ref: "#/components/schemas/IGetSiopAuthenticationRequestFromRpArgs" }, returnType: { $ref: "#/components/schemas/ParsedAuthenticationRequestURI" } }, getSiopAuthenticationRequestDetails: { description: "Get authentication request details", arguments: { $ref: "#/components/schemas/IGetSiopAuthenticationRequestDetailsArgs" }, returnType: { $ref: "#/components/schemas/IAuthRequestDetails" } }, verifySiopAuthenticationRequestURI: { description: "Verify authentication request URI", arguments: { $ref: "#/components/schemas/IVerifySiopAuthenticationRequestUriArgs" }, returnType: { $ref: "#/components/schemas/VerifiedAuthorizationRequest" } }, sendSiopAuthenticationResponse: { description: "Send authentication response", arguments: { $ref: "#/components/schemas/ISendSiopAuthenticationResponseArgs" }, returnType: { $ref: "#/components/schemas/IRequiredContext" } } } } } }; } }); // src/index.ts var index_exports = {}; __export(index_exports, { OID4VCIStore: () => OID4VCIStore, oid4vciStoreMethods: () => oid4vciStoreMethods, schema: () => schema }); module.exports = __toCommonJS(index_exports); // src/agent/OID4VCIStore.ts var import_ssi_sdk = require("@sphereon/ssi-sdk.kv-store-temp"); var oid4vciStoreMethods = [ "oid4vciStoreDefaultMetadata", "oid4vciStoreDefaultIssuerOptions", "oid4vciStoreDefaultStoreId", "oid4vciStoreDefaultNamespace", "oid4vciStoreGetIssuerOpts", "oid4vciStoreHasIssuerOpts", "oid4vciStorePersistIssuerOpts", "oid4vciStoreRemoveIssuerOpts", "oid4vciStoreClearAllIssuerOpts", "oid4vciStoreGetMetadata", "oid4vciStoreListMetadata", "oid4vciStoreHasMetadata", "oid4vciStorePersistMetadata", "oid4vciStoreRemoveMetadata", "oid4vciStoreClearAllMetadata" ]; var OID4VCIStore = class { static { __name(this, "OID4VCIStore"); } get defaultOpts() { return this._defaultOpts; } set defaultOpts(value) { this._defaultOpts = value; } _issuerMetadataStores; _authorizationServerMetadataStores; _optionStores; defaultStoreId; defaultNamespace; _defaultOpts; methods = { oid4vciStoreDefaultMetadata: this.oid4vciStoreDefaultMetadata.bind(this), oid4vciStoreDefaultIssuerOptions: this.oid4vciStoreIssuerOptions.bind(this), oid4vciStoreDefaultStoreId: this.oid4vciStoreDefaultStoreId.bind(this), oid4vciStoreDefaultNamespace: this.oid4vciStoreDefaultNamespace.bind(this), oid4vciStoreGetIssuerOpts: this.oid4vciStoreGetIssuerOpts.bind(this), oid4vciStoreHasIssuerOpts: this.oid4vciStoreHasIssuerOpts.bind(this), oid4vciStorePersistIssuerOpts: this.oid4vciStorePersistIssuerOpts.bind(this), oid4vciStoreRemoveIssuerOpts: this.oid4vciStoreRemoveIssuerOpts.bind(this), oid4vciStoreClearAllIssuerOpts: this.oid4vciStoreClearAllIssuerOpts.bind(this), oid4vciStoreGetMetadata: this.oid4vciStoreGetMetadata.bind(this), oid4vciStoreListMetadata: this.oid4vciStoreListMetadata.bind(this), oid4vciStoreHasMetadata: this.oid4vciStoreHasMetadata.bind(this), oid4vciStorePersistMetadata: this.oid4vciStorePersistMetadata.bind(this), oid4vciStoreRemoveMetadata: this.oid4vciStoreRemoveMetadata.bind(this), oid4vciStoreClearAllMetadata: this.oid4vciStoreClearAllMetadata.bind(this) }; constructor(opts) { this.defaultStoreId = opts.defaultStore ?? "_default"; this.defaultNamespace = opts.defaultNamespace ?? "oid4vci"; if (opts.defaultOpts) { this._defaultOpts = opts.defaultOpts; } if (opts?.issuerMetadataStores && opts.issuerMetadataStores instanceof Map) { this._issuerMetadataStores = opts.issuerMetadataStores; } else if (opts?.issuerMetadataStores) { this._issuerMetadataStores = (/* @__PURE__ */ new Map()).set(this.defaultStoreId, opts.issuerMetadataStores); } else { this._issuerMetadataStores = (/* @__PURE__ */ new Map()).set(this.defaultStoreId, new import_ssi_sdk.KeyValueStore({ namespace: this.defaultNamespace, store: /* @__PURE__ */ new Map() })); } if (opts?.authorizationServerMetadataStores && opts.authorizationServerMetadataStores instanceof Map) { this._authorizationServerMetadataStores = opts.authorizationServerMetadataStores; } else if (opts?.authorizationServerMetadataStores) { this._authorizationServerMetadataStores = (/* @__PURE__ */ new Map()).set(this.defaultStoreId, opts.authorizationServerMetadataStores); } else { this._authorizationServerMetadataStores = (/* @__PURE__ */ new Map()).set(this.defaultStoreId, new import_ssi_sdk.KeyValueStore({ namespace: this.defaultNamespace, store: /* @__PURE__ */ new Map() })); } if (opts?.issuerOptsStores && opts.issuerOptsStores instanceof Map) { this._optionStores = opts.issuerOptsStores; } else if (opts?.issuerOptsStores) { this._optionStores = (/* @__PURE__ */ new Map()).set(this.defaultStoreId, opts.issuerOptsStores); } else { this._optionStores = (/* @__PURE__ */ new Map()).set(this.defaultStoreId, new import_ssi_sdk.KeyValueStore({ namespace: this.defaultNamespace, store: /* @__PURE__ */ new Map() })); } if (opts && Array.isArray(opts?.importIssuerOpts)) { opts.importIssuerOpts.forEach((opt) => this.oid4vciStorePersistIssuerOpts(opt)); } if (opts && Array.isArray(opts?.importMetadatas)) { void this.importMetadatas(opts.importMetadatas); } } async oid4vciStoreGetIssuerOpts({ correlationId, storeId, namespace }) { return await this.store({ stores: this._optionStores, storeId }).get(this.prefix({ namespace, correlationId })) ?? this.defaultOpts; } importIssuerOpts(importOpts) { importOpts.forEach((opt) => this.oid4vciStorePersistIssuerOpts(opt)); } async importMetadatas(metadataImports) { for (const metaImport of metadataImports) { const meta = metaImport; const storeId = meta.storeId ?? this.defaultStoreId; const namespace = meta.namespace ?? this.defaultNamespace; const correlationId = meta.correlationId; const existingMetadata = await this.oid4vciStoreGetMetadata({ metadataType: meta.metadataType, correlationId, storeId, namespace }); let metadataToPersist; if (existingMetadata) { if (meta.overwriteExisting === false) { continue; } metadataToPersist = this.deepMerge(existingMetadata, meta.metadata); } else { metadataToPersist = meta.metadata; } await this.oid4vciStorePersistMetadata({ metadataType: meta.metadataType, metadata: metadataToPersist, storeId, correlationId, namespace, overwriteExisting: true }); } } deepMerge(existing, incoming) { if (!incoming) { return existing; } if (!existing) { return incoming; } const merged = { ...existing }; for (const key in incoming) { if (!Object.prototype.hasOwnProperty.call(incoming, key)) { continue; } const incomingValue = incoming[key]; const existingValue = existing[key]; if (this.isPlainObject(incomingValue) && this.isPlainObject(existingValue)) { merged[key] = this.deepMerge(existingValue, incomingValue); } else if (incomingValue !== void 0) { merged[key] = incomingValue; } } return merged; } isPlainObject(value) { return value !== null && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype; } async oid4vciStoreHasIssuerOpts({ correlationId, storeId, namespace }) { return this.store({ stores: this._optionStores, storeId }).has(this.prefix({ namespace, correlationId })); } async oid4vciStorePersistIssuerOpts(args) { const storeId = this.storeIdStr(args); const namespace = this.namespaceStr(args); const { correlationId, issuerOpts, ttl } = args; if (args?.validation !== false) { } const existing = await this.store({ stores: this._optionStores, storeId }).getAsValueData(this.prefix({ namespace, correlationId })); if (!existing.value || existing.value && args.overwriteExisting !== false) { return await this.store({ stores: this._optionStores, storeId }).set(this.prefix({ namespace, correlationId }), issuerOpts, ttl); } return existing; } async oid4vciStoreRemoveIssuerOpts({ storeId, correlationId, namespace }) { return this.store({ stores: this._optionStores, storeId }).delete(this.prefix({ namespace, correlationId })); } async oid4vciStoreClearAllIssuerOpts({ storeId }) { return await this.store({ stores: this._optionStores, storeId }).clear().then(() => true); } async oid4vciStoreGetMetadata({ metadataType, correlationId, storeId, namespace }) { switch (metadataType) { case "authorizationServer": return this.store({ stores: this._authorizationServerMetadataStores, storeId }).get(this.prefix({ namespace, correlationId })); case "issuer": return this.store({ stores: this._issuerMetadataStores, storeId }).get(this.prefix({ namespace, correlationId })); } return void 0; } async oid4vciStoreListMetadata({ metadataType, storeId, namespace }) { switch (metadataType) { case "authorizationServer": return this.store({ stores: this._authorizationServerMetadataStores, storeId }).getMany([ `${this.namespaceStr({ namespace })}` ]); case "issuer": return this.store({ stores: this._issuerMetadataStores, storeId }).getMany([ `${this.namespaceStr({ namespace })}` ]); } return []; } async oid4vciStoreHasMetadata({ metadataType, correlationId, storeId, namespace }) { switch (metadataType) { case "authorizationServer": return this.store({ stores: this._authorizationServerMetadataStores, storeId }).has(this.prefix({ namespace, correlationId })); case "issuer": return this.store({ stores: this._issuerMetadataStores, storeId }).has(this.prefix({ namespace, correlationId })); } return false; } async oid4vciStorePersistMetadata(args) { const namespace = this.namespaceStr(args); const storeId = this.storeIdStr(args); const { correlationId, metadata, ttl, metadataType } = args; if (args?.validation !== false) { } switch (metadataType) { case "authorizationServer": const existingAuth = await this.store({ stores: this._authorizationServerMetadataStores, storeId }).getAsValueData(this.prefix({ namespace, correlationId })); if (!existingAuth.value || existingAuth.value && args.overwriteExisting !== false) { return await this.store({ stores: this._authorizationServerMetadataStores, storeId }).set(this.prefix({ namespace, correlationId }), metadata, ttl); } return existingAuth; case "issuer": const existingIssuer = await this.store({ stores: this._issuerMetadataStores, storeId }).getAsValueData(this.prefix({ namespace, correlationId })); if (!existingIssuer.value || existingIssuer.value && args.overwriteExisting !== false) { return await this.store({ stores: this._issuerMetadataStores, storeId }).set(this.prefix({ namespace, correlationId }), metadata, ttl); } return existingIssuer; } return void 0; } async oid4vciStoreRemoveMetadata(args) { const namespace = this.namespaceStr(args); const storeId = this.storeIdStr(args); switch (args.metadataType) { case "authorizationServer": return this.store({ stores: this._authorizationServerMetadataStores, storeId }).delete(this.prefix({ namespace, correlationId: args.correlationId })); case "issuer": return this.store({ stores: this._issuerMetadataStores, storeId }).delete(this.prefix({ namespace, correlationId: args.correlationId })); } return false; } async oid4vciStoreClearAllMetadata({ metadataType, storeId }) { switch (metadataType) { case "authorizationServer": return await this.store({ stores: this._authorizationServerMetadataStores, storeId }).clear().then(() => true); case "issuer": return await this.store({ stores: this._issuerMetadataStores, storeId }).clear().then(() => true); } return false; } oid4vciStoreIssuerOptions() { return Promise.resolve(this.store({ stores: this._optionStores, storeId: this.defaultStoreId })); } oid4vciStoreDefaultMetadata() { return Promise.resolve(this.store({ stores: this._issuerMetadataStores, storeId: this.defaultStoreId })); } oid4vciStoreDefaultStoreId() { return Promise.resolve(this.defaultStoreId); } oid4vciStoreDefaultNamespace() { return Promise.resolve(this.defaultNamespace); } store(args) { const storeId = this.storeIdStr({ storeId: args.storeId }); const store = args.stores.get(storeId); if (!store) { throw Error(`Could not get issuer metadata store: ${storeId}`); } return store; } storeIdStr({ storeId }) { return storeId ?? this.defaultStoreId; } namespaceStr({ namespace }) { return namespace ?? this.defaultNamespace; } prefix({ namespace, correlationId }) { return `${this.namespaceStr({ namespace })}:${correlationId}`; } }; // src/index.ts var schema = require_plugin_schema(); //# sourceMappingURL=index.cjs.map