@sphereon/ssi-sdk.oid4vci-issuer-store
Version:
308 lines • 15 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.OID4VCIStore = void 0;
const ssi_sdk_kv_store_temp_1 = require("@sphereon/ssi-sdk.kv-store-temp");
class OID4VCIStore {
get defaultOpts() {
return this._defaultOpts;
}
set defaultOpts(value) {
this._defaultOpts = value;
}
constructor(opts) {
var _a, _b;
this.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),
};
this.defaultStoreId = (_a = opts.defaultStore) !== null && _a !== void 0 ? _a : '_default';
this.defaultNamespace = (_b = opts.defaultNamespace) !== null && _b !== void 0 ? _b : 'oid4vci';
if (opts.defaultOpts) {
this._defaultOpts = opts.defaultOpts;
}
if ((opts === null || opts === void 0 ? void 0 : opts.issuerMetadataStores) && opts.issuerMetadataStores instanceof Map) {
this._issuerMetadataStores = opts.issuerMetadataStores;
}
else if (opts === null || opts === void 0 ? void 0 : opts.issuerMetadataStores) {
this._issuerMetadataStores = new Map().set(this.defaultStoreId, opts.issuerMetadataStores);
}
else {
this._issuerMetadataStores = new Map().set(this.defaultStoreId, new ssi_sdk_kv_store_temp_1.KeyValueStore({
namespace: this.defaultNamespace,
store: new Map(),
}));
}
if ((opts === null || opts === void 0 ? void 0 : opts.authorizationServerMetadataStores) && opts.authorizationServerMetadataStores instanceof Map) {
this._authorizationServerMetadataStores = opts.authorizationServerMetadataStores;
}
else if (opts === null || opts === void 0 ? void 0 : opts.authorizationServerMetadataStores) {
this._authorizationServerMetadataStores = new Map().set(this.defaultStoreId, opts.authorizationServerMetadataStores);
}
else {
this._authorizationServerMetadataStores = new Map().set(this.defaultStoreId, new ssi_sdk_kv_store_temp_1.KeyValueStore({
namespace: this.defaultNamespace,
store: new Map(),
}));
}
if (opts && Array.isArray(opts === null || opts === void 0 ? void 0 : opts.importMetadatas)) {
opts.importMetadatas.forEach((metaImport) => {
var _a, _b;
const meta = metaImport;
void this.oid4vciStorePersistMetadata({
metadataType: meta.metadataType,
metadata: meta.metadata,
storeId: (_a = meta.storeId) !== null && _a !== void 0 ? _a : this.defaultStoreId,
correlationId: meta.correlationId,
namespace: (_b = meta.namespace) !== null && _b !== void 0 ? _b : this.defaultNamespace,
overwriteExisting: meta.overwriteExisting === undefined ? true : meta.overwriteExisting,
});
});
}
if ((opts === null || opts === void 0 ? void 0 : opts.issuerOptsStores) && opts.issuerOptsStores instanceof Map) {
this._optionStores = opts.issuerOptsStores;
}
else if (opts === null || opts === void 0 ? void 0 : opts.issuerOptsStores) {
this._optionStores = new Map().set(this.defaultStoreId, opts.issuerOptsStores);
}
else {
this._optionStores = new Map().set(this.defaultStoreId, new ssi_sdk_kv_store_temp_1.KeyValueStore({
namespace: this.defaultNamespace,
store: new Map(),
}));
}
if (opts && Array.isArray(opts === null || opts === void 0 ? void 0 : opts.importIssuerOpts)) {
opts.importIssuerOpts.forEach((opt) => this.oid4vciStorePersistIssuerOpts(opt));
}
}
oid4vciStoreGetIssuerOpts(_a) {
return __awaiter(this, arguments, void 0, function* ({ correlationId, storeId, namespace }) {
var _b;
return ((_b = (yield this.store({ stores: this._optionStores, storeId }).get(this.prefix({
namespace,
correlationId,
})))) !== null && _b !== void 0 ? _b : this.defaultOpts);
});
}
importIssuerOpts(importOpts) {
importOpts.forEach((opt) => this.oid4vciStorePersistIssuerOpts(opt));
}
oid4vciStoreHasIssuerOpts(_a) {
return __awaiter(this, arguments, void 0, function* ({ correlationId, storeId, namespace }) {
return this.store({ stores: this._optionStores, storeId }).has(this.prefix({ namespace, correlationId }));
});
}
oid4vciStorePersistIssuerOpts(args) {
return __awaiter(this, void 0, void 0, function* () {
const storeId = this.storeIdStr(args);
const namespace = this.namespaceStr(args);
const { correlationId, issuerOpts, ttl } = args;
if ((args === null || args === void 0 ? void 0 : args.validation) !== false) {
//todo
}
const existing = yield this.store({ stores: this._optionStores, storeId }).getAsValueData(this.prefix({
namespace,
correlationId,
}));
if (!existing.value || (existing.value && args.overwriteExisting !== false)) {
return yield this.store({ stores: this._optionStores, storeId }).set(this.prefix({
namespace,
correlationId,
}), issuerOpts, ttl);
}
return existing;
});
}
oid4vciStoreRemoveIssuerOpts(_a) {
return __awaiter(this, arguments, void 0, function* ({ storeId, correlationId, namespace }) {
return this.store({ stores: this._optionStores, storeId }).delete(this.prefix({ namespace, correlationId }));
});
}
oid4vciStoreClearAllIssuerOpts(_a) {
return __awaiter(this, arguments, void 0, function* ({ storeId }) {
return yield this.store({ stores: this._optionStores, storeId })
.clear()
.then(() => true);
});
}
oid4vciStoreGetMetadata(_a) {
return __awaiter(this, arguments, void 0, function* ({ 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 undefined;
});
}
oid4vciStoreListMetadata(_a) {
return __awaiter(this, arguments, void 0, function* ({ 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 [];
});
}
oid4vciStoreHasMetadata(_a) {
return __awaiter(this, arguments, void 0, function* ({ 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;
});
}
oid4vciStorePersistMetadata(args) {
return __awaiter(this, void 0, void 0, function* () {
const namespace = this.namespaceStr(args);
const storeId = this.storeIdStr(args);
const { correlationId, metadata, ttl, metadataType } = args;
if ((args === null || args === void 0 ? void 0 : args.validation) !== false) {
//todo
}
switch (metadataType) {
case 'authorizationServer':
const existingAuth = yield this.store({
stores: this._authorizationServerMetadataStores,
storeId,
}).getAsValueData(this.prefix({ namespace, correlationId }));
if (!existingAuth.value || (existingAuth.value && args.overwriteExisting !== false)) {
return yield this.store({
stores: this._authorizationServerMetadataStores,
storeId,
}).set(this.prefix({ namespace, correlationId }), metadata, ttl);
}
return existingAuth;
case 'issuer':
const existingIssuer = yield this.store({
stores: this._issuerMetadataStores,
storeId,
}).getAsValueData(this.prefix({ namespace, correlationId }));
if (!existingIssuer.value || (existingIssuer.value && args.overwriteExisting !== false)) {
return yield this.store({
stores: this._issuerMetadataStores,
storeId,
}).set(this.prefix({ namespace, correlationId }), metadata, ttl);
}
return existingIssuer;
}
return undefined;
});
}
oid4vciStoreRemoveMetadata(args) {
return __awaiter(this, void 0, void 0, function* () {
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;
});
}
oid4vciStoreClearAllMetadata(_a) {
return __awaiter(this, arguments, void 0, function* ({ metadataType, storeId }) {
switch (metadataType) {
case 'authorizationServer':
return yield this.store({
stores: this._authorizationServerMetadataStores,
storeId,
})
.clear()
.then(() => true);
case 'issuer':
return yield 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 !== null && storeId !== void 0 ? storeId : this.defaultStoreId;
}
namespaceStr({ namespace }) {
return namespace !== null && namespace !== void 0 ? namespace : this.defaultNamespace;
}
prefix({ namespace, correlationId }) {
return `${this.namespaceStr({ namespace })}:${correlationId}`;
}
}
exports.OID4VCIStore = OID4VCIStore;
//# sourceMappingURL=OID4VCIStore.js.map