Skip to content

Commit

Permalink
InMemoryStore
Browse files Browse the repository at this point in the history
  • Loading branch information
ciscorn committed Sep 27, 2024
1 parent 4719d1c commit be76c44
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down Expand Up @@ -69,5 +69,6 @@
],
"bugs": {
"url": "https://github.com/Code-Hex/firebase-auth-cloudflare-workers/issues"
}
},
"packageManager": "[email protected]+sha512.60c18acd138bff695d339be6ad13f7e936eea6745660d4cc4a776d5247c540d0edee1a563695c183a66eb917ef88f2b4feb1fc25f32a7adcadc7aaf3438e99c1"
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
16 changes: 16 additions & 0 deletions src/key-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/version.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export const version = '2.0.7';
export const version = '2.0.9';
28 changes: 5 additions & 23 deletions tests/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -231,25 +232,6 @@ function generateRandomString(stringLength: number) {
return randomString;
}

class InMemoryKeyStorer implements KeyStorer {
private store: Map<string, unknown> = new Map();
private timerId: NodeJS.Timeout | null = null;

constructor(private readonly cacheKey: string) {}

public async get<ExpectedValue = unknown>(): Promise<ExpectedValue | null> {
return (this.store.get(this.cacheKey) as ExpectedValue) || null;
}

public async put(value: string, expirationTtl: number): Promise<void> {
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) => {
Expand Down

0 comments on commit be76c44

Please sign in to comment.