From 813d8ffbebc01bfdc410275597319f3046e2c8dc Mon Sep 17 00:00:00 2001 From: meriadec Date: Fri, 27 Jul 2018 18:24:25 +0200 Subject: [PATCH] Rename db.isKeyEncrypted to db.hasBeenDecrypted for clarity and change its return value, eh. --- src/components/IsUnlocked.js | 4 ++-- src/helpers/db/db.spec.js | 10 +++++----- src/helpers/db/index.js | 12 ++++++------ src/renderer/init.js | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/components/IsUnlocked.js b/src/components/IsUnlocked.js index 9356974a..4cabf8cb 100644 --- a/src/components/IsUnlocked.js +++ b/src/components/IsUnlocked.js @@ -112,9 +112,9 @@ class IsUnlocked extends Component { const { unlock, fetchAccounts } = this.props const { inputValue } = this.state - const isAccountsEncrypted = await db.isKeyEncrypted('app', 'accounts') + const isAccountsDecrypted = await db.hasBeenDecrypted('app', 'accounts') try { - if (isAccountsEncrypted) { + if (!isAccountsDecrypted) { await db.setEncryptionKey('app', 'accounts', inputValue.password) await fetchAccounts() } else if (!db.isEncryptionKeyCorrect('app', 'accounts', inputValue.password)) { diff --git a/src/helpers/db/db.spec.js b/src/helpers/db/db.spec.js index 83632255..79260117 100644 --- a/src/helpers/db/db.spec.js +++ b/src/helpers/db/db.spec.js @@ -152,17 +152,17 @@ describe('db', () => { }) test('detect if field is encrypted or not', async () => { - let isEncrypted + let isDecrypted await db.setKey('app', 'encryptedField', { some: 'data' }) await db.setEncryptionKey('app', 'encryptedField', 'passw0rd') db.init(dbPath) const k = await db.getKey('app', 'encryptedField') expect(k).toBe('HNEETQf+9An6saxmA/X8zg==') - isEncrypted = await db.isKeyEncrypted('app', 'encryptedField') - expect(isEncrypted).toBe(true) + isDecrypted = await db.hasBeenDecrypted('app', 'encryptedField') + expect(isDecrypted).toBe(false) await db.setEncryptionKey('app', 'encryptedField', 'passw0rd') - isEncrypted = await db.isKeyEncrypted('app', 'encryptedField') - expect(isEncrypted).toBe(false) + isDecrypted = await db.hasBeenDecrypted('app', 'encryptedField') + expect(isDecrypted).toBe(true) const value = await db.getKey('app', 'encryptedField') expect(value).toEqual({ some: 'data' }) }) diff --git a/src/helpers/db/index.js b/src/helpers/db/index.js index 3be639b0..2f2ed528 100644 --- a/src/helpers/db/index.js +++ b/src/helpers/db/index.js @@ -173,19 +173,19 @@ async function setNamespace(ns: string, value: any) { } /** - * Check if a key is encrypted + * Check if a key has been decrypted * * /!\ it consider encrypted if it's string and can't JSON.parse, so * can brings false-positive if bad used */ -async function isKeyEncrypted(ns: string, keyPath: string): Promise { +async function hasBeenDecrypted(ns: string, keyPath: string): Promise { const v = await getKey(ns, keyPath) - if (typeof v !== 'string') return false + if (typeof v !== 'string') return true try { JSON.parse(v) - return false - } catch (err) { return true + } catch (err) { + return false } } @@ -283,7 +283,7 @@ export default { getKey, getNamespace, setNamespace, - isKeyEncrypted, + hasBeenDecrypted, save, cleanCache, resetAll, diff --git a/src/renderer/init.js b/src/renderer/init.js index 23ddee15..52fd35f7 100644 --- a/src/renderer/init.js +++ b/src/renderer/init.js @@ -71,8 +71,8 @@ async function init() { // FIXME IMO init() really should only be for window. any other case is a hack! const isMainWindow = remote.getCurrentWindow().name === 'MainWindow' - const isAccountsEncrypted = await db.isKeyEncrypted('app', 'accounts') - if (isAccountsEncrypted) { + const isAccountsDecrypted = await db.hasBeenDecrypted('app', 'accounts') + if (!isAccountsDecrypted) { store.dispatch(lock()) } else { await store.dispatch(fetchAccounts())