/** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ import { ActiveAuthorizationHandler } from './types' import { TurnContext } from '../../turnContext' import { Storage } from '../../storage' import { ExceptionHelper } from '@microsoft/agents-activity' import { Errors } from '../../errorHelper' /** * Storage manager for handler state. */ export class HandlerStorage { /** * Creates an instance of the HandlerStorage. * @param storage The storage provider. * @param context The turn context. */ constructor (private storage: Storage, private context: TurnContext) { } /** * Gets the unique key for a handler session. */ public get key (): string { const channelId = this.context.activity.channelId?.trim() const userId = this.context.activity.from?.id?.trim() if (!channelId || !userId) { throw ExceptionHelper.generateException(Error, Errors.ChannelIdAndFromIdRequired) } return `auth/${channelId}/${userId}` } /** * Reads the active handler state from storage. */ public async read (): Promise { const ongoing = await this.storage.read([this.key]) return ongoing?.[this.key] } /** * Writes handler state to storage. */ public write (data: TActiveHandler) : Promise { return this.storage.write({ [this.key]: data }) } /** * Deletes handler state from storage. */ public async delete (): Promise { try { await this.storage.delete([this.key]) } catch (error) { if (error instanceof Error && 'code' in error && error.code === 404) { return } throw error } } }