/** * @module identity/web-device-identity-service-v1 * * Browser/IndexedDB concrete implementation of DeviceIdentityServiceBase_V1. * Encapsulates client-side delegate secret persistence, site handle configuration, * and user-agent device tagging. */ import { DeviceIdentityServiceBase_V1 } from "@ibgib/core-gib/dist/keystone/device/device-identity-service-base-v1.mjs"; import { KEYSTONE_ROLE_AUTHOR_DELEGATE } from "@ibgib/core-gib/dist/keystone/delegate/delegate-constants.mjs"; import { generateDefaultDeviceTag } from "../util/device-helper.mjs"; import { getDeviceDelegateSecret, setDeviceDelegateSecret, getDeviceAuthorDelegateSecret, setDeviceAuthorDelegateSecret, clearDeviceDelegateSecret, } from "./device-delegate-helper.mjs"; import { storageGet, storagePut, storageDelete } from "../storage/storage-helpers.web.mjs"; import { getGlobalDbName, getGlobalStoreName } from "../helpers.web.mjs"; export const WEB_DEVICE_SITE_HANDLE_KEY = 'ibgib_web_device_site_handle'; export class WebDeviceIdentityService_V1 extends DeviceIdentityServiceBase_V1 { protected override lc: string = `[${WebDeviceIdentityService_V1.name}]`; private activeDelegateTjps: Set = new Set(); public async persistDelegateSecret(opts: { delegateTjpAddr: string; delegateSecret: string; role: string; }): Promise { this.activeDelegateTjps.add(opts.delegateTjpAddr); if (opts.role === KEYSTONE_ROLE_AUTHOR_DELEGATE || opts.role === 'author-delegate' || opts.role === 'author_delegate') { await setDeviceAuthorDelegateSecret({ delegateTjpAddr: opts.delegateTjpAddr, delegateSecret: opts.delegateSecret, persistToStorage: true, }); } else { await setDeviceDelegateSecret({ delegateTjpAddr: opts.delegateTjpAddr, delegateSecret: opts.delegateSecret, persistToStorage: true, }); } } public async getPersistedDelegateSecret(opts: { delegateTjpAddr: string; role: string; }): Promise { if (opts.role === KEYSTONE_ROLE_AUTHOR_DELEGATE || opts.role === 'author-delegate' || opts.role === 'author_delegate') { return await getDeviceAuthorDelegateSecret({ delegateTjpAddr: opts.delegateTjpAddr }); } else { return await getDeviceDelegateSecret({ delegateTjpAddr: opts.delegateTjpAddr }); } } public async clearPersistedDelegateSecrets(): Promise { for (const tjp of this.activeDelegateTjps) { await clearDeviceDelegateSecret({ delegateTjpAddr: tjp }); } this.activeDelegateTjps.clear(); } public async persistSiteHandle(opts: { siteUsername: string }): Promise { const dbName = getGlobalDbName(); const storeName = getGlobalStoreName(); await storagePut({ dbName, storeName, key: WEB_DEVICE_SITE_HANDLE_KEY, value: opts.siteUsername }); } public async getPersistedSiteHandle(): Promise { const dbName = getGlobalDbName(); const storeName = getGlobalStoreName(); return await storageGet({ dbName, storeName, key: WEB_DEVICE_SITE_HANDLE_KEY }); } protected getDefaultDeviceTag(): string { return generateDefaultDeviceTag(); } }