From 7626453f552c7ff05d2940f46f1a57d5362d1d4e Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Fri, 11 Nov 2022 08:28:25 -0800 Subject: [PATCH] Fall back to memory password storage if keytar is not available Summary: Not in all environments keytar is available, but still it will be useful to be able to login to intern with Flipper, even for temporarily sessions. This solution falls back to in memory storage. Note that the underlying assuption is that multiple different users are not connected to the same Flipper server, as this would share their session! https://fb.workplace.com/groups/flippersupport/permalink/1498550730625580/ Reviewed By: passy Differential Revision: D41218132 fbshipit-source-id: 6e518d742df639bfbdc9a36ed1fa56ecb363a0b0 --- .../flipper-server-core/src/utils/keytar.tsx | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/desktop/flipper-server-core/src/utils/keytar.tsx b/desktop/flipper-server-core/src/utils/keytar.tsx index 4c81233ed..9b569b536 100644 --- a/desktop/flipper-server-core/src/utils/keytar.tsx +++ b/desktop/flipper-server-core/src/utils/keytar.tsx @@ -19,11 +19,17 @@ export type KeytarModule = { }; export class KeytarManager { + private memoryFallback = new Map(); + constructor(private keytar: KeytarModule | undefined) {} public async writeKeychain(service: string, password: string): Promise { if (this.keytar == null) { - throw new Error('Keytar is not available.'); + console.warn( + 'Keytar is not available, using session only memory storage as fallback', + ); + this.memoryFallback.set(service, password); + return; } await this.keytar.deletePassword(service, os.userInfo().username); @@ -31,17 +37,17 @@ export class KeytarManager { } public async unsetKeychain(service: string): Promise { - await this.keytar?.deletePassword(service, os.userInfo().username); + if (this.keytar) { + await this.keytar.deletePassword(service, os.userInfo().username); + } else { + this.memoryFallback.delete(service); + } } public async retrieveToken(service: string): Promise { - if (this.keytar == null) { - throw new Error('Keytar is not available.'); - } - const token = await this.keytar.getPassword( - service, - os.userInfo().username, - ); + const token = this.keytar + ? await this.keytar.getPassword(service, os.userInfo().username) + : this.memoryFallback.get(service); if (!token) { throw new UserNotSignedInError(); }