import { type PersistKey, type Storage } from '@logto/client'; import { type CookieSerializeOptions } from 'cookie'; import { PromiseQueue } from './promise-queue.js'; import { wrapSession, unwrapSession, type SessionData } from './session.js'; // eslint-disable-next-line @typescript-eslint/ban-types type Nullable = T | null; export type CookieConfigBase = { cookieKey?: string; isSecure?: boolean; getCookie: (name: string) => Promise | string | undefined; setCookie: ( name: string, value: string, options: CookieSerializeOptions & { path: string } ) => Promise | void; }; export type SessionWrapper = { wrap: (data: SessionData, key: string, currentValue?: string) => Promise; unwrap: (value: string, key: string) => Promise; }; export type CookieConfig = CookieConfigBase & ( | { /** Required when using default session wrapper */ encryptionKey: string; sessionWrapper?: never; } | { /** Optional when custom sessionWrapper is provided */ encryptionKey?: string; /** Custom session wrapper can be used to implement external storage solutions */ sessionWrapper: SessionWrapper; } ); /** * A storage that persists data in cookies with encryption. */ export class CookieStorage implements Storage { protected get cookieOptions() { return Object.freeze({ httpOnly: true, path: '/', sameSite: 'lax', secure: this.config.isSecure ?? false, maxAge: 14 * 24 * 3600, // 14 days } satisfies Readonly); } protected get cookieKey() { return this.config.cookieKey ?? 'logtoCookies'; } get data() { return this.sessionData; } protected sessionData: SessionData = {}; protected sessionValue = ''; protected saveQueue = new PromiseQueue(); /** * Handles the wrapping and unwrapping of session data. * Can be provided via config or defaults to using wrapSession/unwrapSession functions. * Users can implement custom storage solutions by providing their own sessionWrapper. */ protected sessionWrapper: SessionWrapper; constructor(public config: CookieConfig) { if (!config.sessionWrapper && !config.encryptionKey) { throw new TypeError( 'Either `sessionWrapper` or `encryptionKey` must be provided for `CookieStorage`' ); } this.sessionWrapper = config.sessionWrapper ?? { wrap: wrapSession, unwrap: unwrapSession, }; } async init() { const { encryptionKey = '' } = this.config; const sessionValue = (await this.config.getCookie(this.cookieKey)) ?? ''; this.sessionValue = sessionValue; this.sessionData = await this.sessionWrapper.unwrap(sessionValue, encryptionKey); } async getItem(key: PersistKey): Promise> { return this.sessionData[key] ?? null; } async setItem(key: PersistKey, value: string): Promise { this.sessionData[key] = value; await this.save(); } async removeItem(key: PersistKey): Promise { // eslint-disable-next-line @silverhand/fp/no-delete, @typescript-eslint/no-dynamic-delete delete this.sessionData[key]; await this.save(); } async destroy() { this.sessionData = {}; await this.save(); } protected async save() { return this.saveQueue.enqueue(async () => this.write()); } protected async write(data = this.sessionData) { const { encryptionKey = '' } = this.config; const value = await this.sessionWrapper.wrap(data, encryptionKey, this.sessionValue); this.sessionValue = value; await this.config.setCookie(this.cookieKey, value, this.cookieOptions); } }