diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d4edf6c..4798f3b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: - name: Setup Node uses: actions/setup-node@v4 with: - node-version: 18 + node-version: 20 cache: 'pnpm' - run: pnpm install --frozen-lockfile - run: pnpm test-with-emulator @@ -31,7 +31,7 @@ jobs: - name: Setup Node uses: actions/setup-node@v4 with: - node-version: 18 + node-version: 20 cache: 'pnpm' - run: pnpm install --frozen-lockfile - run: pnpm lint-fix diff --git a/package.json b/package.json index 85b02bf..553dba7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "firebase-auth-cloudflare-workers-x509", - "version": "2.0.8", + "version": "2.0.9", "description": "Firebase auth library for Cloudflare Workers (forked from firebase-auth-cloudflare-workers and modified to use X.509 certificates)", "author": "ciscorn", "maintainers": [ @@ -69,5 +69,6 @@ ], "bugs": { "url": "https://github.com/Code-Hex/firebase-auth-cloudflare-workers/issues" - } + }, + "packageManager": "pnpm@9.9.0+sha512.60c18acd138bff695d339be6ad13f7e936eea6745660d4cc4a776d5247c540d0edee1a563695c183a66eb917ef88f2b4feb1fc25f32a7adcadc7aaf3438e99c1" } diff --git a/src/index.ts b/src/index.ts index 3b790b2..15a92f7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,6 +8,7 @@ import { WorkersKVStore } from './key-store'; export { type Credential, ServiceAccountCredential } from './credential'; export { emulatorHost, useEmulator } from './emulator'; export type { KeyStorer }; +export { InMemoryStore } from './key-store'; export type { EmulatorEnv } from './emulator'; export type { FirebaseIdToken } from './token-verifier'; export type { RetryConfig }; diff --git a/src/key-store.ts b/src/key-store.ts index 1cdcf19..74bb847 100644 --- a/src/key-store.ts +++ b/src/key-store.ts @@ -22,3 +22,19 @@ export class WorkersKVStore implements KeyStorer { }); } } + +export class InMemoryStore implements KeyStorer { + private val: string | null = null; + private expireAt: number = 0; + + async get() { + if (Date.now() > this.expireAt) { + this.val = null; + } + return this.val ? JSON.parse(this.val) : null; + } + async put(value: string, expirationTtl: number) { + this.expireAt = Date.now() + expirationTtl * 1000; + this.val = value; + } +} diff --git a/src/version.ts b/src/version.ts index 34e65ae..8c3f849 100644 --- a/src/version.ts +++ b/src/version.ts @@ -1 +1 @@ -export const version = '2.0.7'; +export const version = '2.0.9'; diff --git a/tests/auth.test.ts b/tests/auth.test.ts index c04a438..4bde8c3 100644 --- a/tests/auth.test.ts +++ b/tests/auth.test.ts @@ -4,7 +4,8 @@ import { BaseAuth } from '../src/auth'; import { AuthApiClient } from '../src/auth-api-requests'; import { AuthClientErrorCode, FirebaseAuthError } from '../src/errors'; import type { UserRecord } from '../src/user-record'; -import type { EmulatorEnv, KeyStorer } from './../src/index'; +import { InMemoryStore } from './../src/index'; +import type { EmulatorEnv } from './../src/index'; import { EmulatedSigner, FirebaseTokenGenerator, @@ -34,7 +35,7 @@ describe('createSessionCookie()', () => { const signer = new EmulatedSigner(); const tokenGenerator = new FirebaseTokenGenerator(signer); - const keyStorer = new InMemoryKeyStorer('cache-key'); + const keyStorer = new InMemoryStore(); it('creates a valid Firebase session cookie', async () => { const auth = new BaseAuth(projectId, keyStorer, new NopCredential()); @@ -128,7 +129,7 @@ describe('createSessionCookie()', () => { describe('verifySessionCookie()', () => { const uid = sessionCookieUids[0]; - const keyStorer = new InMemoryKeyStorer('cache-key'); + const keyStorer = new InMemoryStore(); const auth = new BaseAuth(projectId, keyStorer, new NopCredential()); const signer = new EmulatedSigner(); const tokenGenerator = new FirebaseTokenGenerator(signer); @@ -177,7 +178,7 @@ describe('getUser()', () => { admin: true, groupId: '1234', }; - const keyStorer = new InMemoryKeyStorer('cache-key'); + const keyStorer = new InMemoryStore(); const auth = new BaseAuth(projectId, keyStorer, new NopCredential()); const signer = new EmulatedSigner(); const tokenGenerator = new FirebaseTokenGenerator(signer); @@ -231,25 +232,6 @@ function generateRandomString(stringLength: number) { return randomString; } -class InMemoryKeyStorer implements KeyStorer { - private store: Map = new Map(); - private timerId: NodeJS.Timeout | null = null; - - constructor(private readonly cacheKey: string) {} - - public async get(): Promise { - return (this.store.get(this.cacheKey) as ExpectedValue) || null; - } - - public async put(value: string, expirationTtl: number): Promise { - if (this.timerId) { - clearTimeout(this.timerId); - } - this.store.set(this.cacheKey, value); - this.timerId = setTimeout(() => this.store.delete(this.cacheKey), expirationTtl * 1000); - } -} - const FIREBASE_AUTH_DISABLE_USER = new ApiSettings('v1', '/accounts:update', 'POST') // Set response validator. .setResponseValidator((response: any) => {