feat: namespaced keychain (#428)

* feat: namespaced keychain

* fix: update device interface

* fix: update WebApplication

* chore(deps): update snjs

* fix: Bridge interface

* chore(deps): update snjs

Co-authored-by: Johnny Almonte <johnny243@users.noreply.github.com>
This commit is contained in:
Johnny A
2020-09-03 05:19:21 -07:00
committed by GitHub
parent 57aa942c91
commit 568fb14f3a
5 changed files with 36 additions and 14 deletions

View File

@@ -8,12 +8,14 @@ export interface Bridge {
const KEYCHAIN_STORAGE_KEY = 'keychain'; const KEYCHAIN_STORAGE_KEY = 'keychain';
export class BrowserBridge implements Bridge { export class BrowserBridge implements Bridge {
async getKeychainValue(): Promise<unknown> { async getKeychainValue(): Promise<unknown> {
const value = localStorage.getItem(KEYCHAIN_STORAGE_KEY); const value = localStorage.getItem(KEYCHAIN_STORAGE_KEY);
if (value) { if (value) {
return JSON.parse(value); return JSON.parse(value);
} }
} }
async setKeychainValue(value: any): Promise<void> { async setKeychainValue(value: any): Promise<void> {
localStorage.setItem(KEYCHAIN_STORAGE_KEY, JSON.stringify(value)); localStorage.setItem(KEYCHAIN_STORAGE_KEY, JSON.stringify(value));
} }

View File

@@ -59,9 +59,7 @@ export class WebApplication extends SNApplication {
defaultSyncServerHost: string, defaultSyncServerHost: string,
bridge: Bridge, bridge: Bridge,
) { ) {
const namespace = '';
const deviceInterface = new WebDeviceInterface( const deviceInterface = new WebDeviceInterface(
namespace,
$timeout, $timeout,
bridge bridge
); );
@@ -71,7 +69,7 @@ export class WebApplication extends SNApplication {
deviceInterface, deviceInterface,
new SNWebCrypto(), new SNWebCrypto(),
new AlertService(), new AlertService(),
namespace, undefined,
undefined, undefined,
undefined, undefined,
defaultSyncServerHost defaultSyncServerHost

View File

@@ -7,12 +7,10 @@ export class WebDeviceInterface extends DeviceInterface {
private database: Database private database: Database
constructor( constructor(
namespace: string,
timeout: any, timeout: any,
private bridge: Bridge private bridge: Bridge
) { ) {
super( super(
namespace,
timeout || setTimeout.bind(getGlobalScope()), timeout || setTimeout.bind(getGlobalScope()),
setInterval.bind(getGlobalScope()) setInterval.bind(getGlobalScope())
); );
@@ -100,15 +98,39 @@ export class WebDeviceInterface extends DeviceInterface {
return this.database.clearAllPayloads(); return this.database.clearAllPayloads();
} }
getKeychainValue(): Promise<unknown> { async getNamespacedKeychainValue() {
const keychain = await this.getRawKeychainValue();
if (!keychain) {
return;
}
return keychain[this.namespace!.identifier];
}
async setNamespacedKeychainValue(value: any) {
let keychain = await this.getRawKeychainValue();
if (!keychain) {
keychain = {};
}
this.bridge.setKeychainValue({
...keychain,
[this.namespace!.identifier]: value
});
}
async clearNamespacedKeychainValue() {
const keychain = await this.getRawKeychainValue();
if (!keychain) {
return;
}
delete keychain[this.namespace!.identifier];
this.bridge.setKeychainValue(keychain);
}
getRawKeychainValue(): Promise<any> {
return this.bridge.getKeychainValue(); return this.bridge.getKeychainValue();
} }
setKeychainValue(value: any) { clearRawKeychainValue() {
return this.bridge.setKeychainValue(value);
}
clearKeychainValue() {
return this.bridge.clearKeychainValue(); return this.bridge.clearKeychainValue();
} }

4
package-lock.json generated
View File

@@ -10956,8 +10956,8 @@
"from": "github:standardnotes/sncrypto#4a080efeb646dbf9ca3dffdfcfa9d081b4dc6de0" "from": "github:standardnotes/sncrypto#4a080efeb646dbf9ca3dffdfcfa9d081b4dc6de0"
}, },
"snjs": { "snjs": {
"version": "github:standardnotes/snjs#7c983dc558ea7777a4957b44efa3362aaf18d3c9", "version": "github:standardnotes/snjs#cb83f0dfacd6b0a9ac4b51cb32f2534f2a23d232",
"from": "github:standardnotes/snjs#7c983dc558ea7777a4957b44efa3362aaf18d3c9" "from": "github:standardnotes/snjs#cb83f0dfacd6b0a9ac4b51cb32f2534f2a23d232"
}, },
"sockjs": { "sockjs": {
"version": "0.3.20", "version": "0.3.20",

View File

@@ -67,6 +67,6 @@
}, },
"dependencies": { "dependencies": {
"sncrypto": "github:standardnotes/sncrypto#4a080efeb646dbf9ca3dffdfcfa9d081b4dc6de0", "sncrypto": "github:standardnotes/sncrypto#4a080efeb646dbf9ca3dffdfcfa9d081b4dc6de0",
"snjs": "github:standardnotes/snjs#7c983dc558ea7777a4957b44efa3362aaf18d3c9" "snjs": "github:standardnotes/snjs#cb83f0dfacd6b0a9ac4b51cb32f2534f2a23d232"
} }
} }