diff --git a/plugins/auth-backend/src/identity/FirestoreKeyStore.test.ts b/plugins/auth-backend/src/identity/FirestoreKeyStore.test.ts index b04cf8a6b3..8e30dcc6f8 100644 --- a/plugins/auth-backend/src/identity/FirestoreKeyStore.test.ts +++ b/plugins/auth-backend/src/identity/FirestoreKeyStore.test.ts @@ -24,7 +24,9 @@ import { } from './FirestoreKeyStore'; import { AnyJWK } from './types'; -const data = jest.fn().mockReturnValue('data'); +const data = jest + .fn() + .mockReturnValue({ key: { kid: 'something' }, kid: 'something' }); const toDate = jest.fn().mockReturnValue('date'); const get = jest.fn().mockReturnValue({ docs: [{ data, createTime: { toDate } }], @@ -145,7 +147,7 @@ describe('FirestoreKeyStore', () => { expect(firestoreMock.doc).toHaveBeenCalledWith(key.kid); expect(firestoreMock.set).toHaveBeenCalledWith({ kid: key.kid, - key: JSON.stringify(key), + key, }); }); @@ -180,7 +182,26 @@ describe('FirestoreKeyStore', () => { expect(data).toHaveBeenCalledTimes(1); expect(toDate).toHaveBeenCalledTimes(1); expect(items).toMatchObject({ - items: [{ key: 'data', createdAt: 'date' }], + items: [{ key: { kid: 'something' }, createdAt: 'date' }], + }); + }); + + it('supports older string versions of the keys', async () => { + data.mockReturnValue({ + key: JSON.stringify({ kid: 'something' }), + kid: 'something', + }); + + const keyStore = await FirestoreKeyStore.create(firestoreSettings); + const items = await keyStore.listKeys(); + + expect(setTimeout).toHaveBeenCalledTimes(1); + expect(firestoreMock.collection).toHaveBeenCalledWith(path); + expect(firestoreMock.get).toHaveBeenCalledTimes(1); + expect(data).toHaveBeenCalledTimes(1); + expect(toDate).toHaveBeenCalledTimes(1); + expect(items).toMatchObject({ + items: [{ key: { kid: 'something' }, createdAt: 'date' }], }); }); }); diff --git a/plugins/auth-backend/src/identity/FirestoreKeyStore.ts b/plugins/auth-backend/src/identity/FirestoreKeyStore.ts index b5f4f5dcfb..3b998a4fe9 100644 --- a/plugins/auth-backend/src/identity/FirestoreKeyStore.ts +++ b/plugins/auth-backend/src/identity/FirestoreKeyStore.ts @@ -75,13 +75,10 @@ export class FirestoreKeyStore implements KeyStore { async addKey(key: AnyJWK): Promise { await this.withTimeout( - this.database - .collection(this.path) - .doc(key.kid) - .set({ - kid: key.kid, - key: JSON.stringify(key), - }), + this.database.collection(this.path).doc(key.kid).set({ + kid: key.kid, + key: key, + }), ); } @@ -91,10 +88,14 @@ export class FirestoreKeyStore implements KeyStore { ); return { - items: keys.docs.map(key => ({ - key: key.data() as AnyJWK, - createdAt: key.createTime.toDate(), - })), + items: keys.docs.map(doc => { + const { key } = doc.data(); + + return { + createdAt: doc.createTime.toDate(), + key: typeof key === 'string' ? JSON.parse(key) : key, + }; + }), }; }