UNPKG

@bitblit/ratchet-warden-server

Version:

Typescript library to simplify using simplewebauthn and secondary auth methods over GraphQL

99 lines 4.04 kB
import { WardenUtils } from '@bitblit/ratchet-warden-common/common/util/warden-utils'; import { S3CacheRatchet } from '@bitblit/ratchet-aws/s3/s3-cache-ratchet'; import { ErrorRatchet } from '@bitblit/ratchet-common/lang/error-ratchet'; import { StringRatchet } from '@bitblit/ratchet-common/lang/string-ratchet'; export class WardenS3SingleFileStorageProvider { s3; options; ratchet; constructor(s3, options) { this.s3 = s3; this.options = options; this.ratchet = new S3CacheRatchet(this.s3, this.options.bucket); } async listUserSummaries() { const allData = (await this.fetchDataFile()).entries; const rval = allData.map((d) => WardenUtils.stripWardenEntryToSummary(d)); return rval; } async fetchDataFile() { let data = await this.ratchet.fetchCacheFileAsObject(this.options.dataFileKey); data = data || { entries: [], challenges: [], }; return data; } async storeDataFile(file) { let rval = null; if (file) { rval = await this.ratchet.writeObjectToCacheFile(this.options.dataFileKey, file); } return rval; } async fetchCurrentUserChallenge(userId, relyingPartyId) { const data = await this.fetchDataFile(); const entry = (data.challenges || []).find((d) => d.userId === userId && d.relyingPartyId === relyingPartyId); if (!entry) { ErrorRatchet.throwFormattedErr('fetchCurrentUserChallenge: Could not find user %s', userId); } return entry.challenge; } async findEntryByContact(contact) { let rval = null; if (contact?.type && StringRatchet.trimToNull(contact?.value)) { const data = await this.fetchDataFile(); rval = (data.entries || []).find((d) => !!(d.contactMethods || []).find((x) => x.type === contact.type && x.value === contact.value)); } return rval; } async findEntryByThirdPartyId(thirdParty, thirdPartyId) { let rval = null; if (StringRatchet.trimToNull(thirdParty) && StringRatchet.trimToNull(thirdPartyId)) { const data = await this.fetchDataFile(); rval = (data.entries || []).find((d) => !!(d.thirdPartyAuthenticators || []).find((x) => x.thirdParty === thirdParty && x.thirdPartyId === thirdPartyId)); } return rval; } async findEntryById(userId) { let rval = null; if (StringRatchet.trimToNull(userId)) { const data = await this.fetchDataFile(); rval = (data.entries || []).find((d) => d.userId === userId); } return rval; } async removeEntry(userId) { const data = await this.fetchDataFile(); data.entries = (data.entries || []).filter((d) => d.userId !== userId); await this.storeDataFile(data); return true; } async saveEntry(entry) { let rval = null; if (entry && entry.userId) { const now = Date.now(); entry.createdEpochMS = entry.createdEpochMS || now; entry.updatedEpochMS = now; const data = await this.fetchDataFile(); data.entries = (data.entries || []).filter((d) => d.userId !== entry.userId); data.entries.push(entry); await this.storeDataFile(data); rval = await this.findEntryById(entry.userId); } return rval; } async updateUserChallenge(userId, relyingPartyId, challenge) { const data = await this.fetchDataFile(); data.challenges = (data.challenges || []).filter((d) => d.userId !== userId || d.relyingPartyId !== relyingPartyId); data.challenges.push({ userId: userId, relyingPartyId: relyingPartyId, challenge: challenge, updatedEpochMS: Date.now(), }); await this.storeDataFile(data); return true; } } //# sourceMappingURL=warden-s3-single-file-storage-provider.js.map