Browse Source

Rename db.isKeyEncrypted to db.hasBeenDecrypted for clarity

and change its return value, eh.
master
meriadec 7 years ago
parent
commit
813d8ffbeb
No known key found for this signature in database GPG Key ID: 1D2FC2305E2CB399
  1. 4
      src/components/IsUnlocked.js
  2. 10
      src/helpers/db/db.spec.js
  3. 12
      src/helpers/db/index.js
  4. 4
      src/renderer/init.js

4
src/components/IsUnlocked.js

@ -112,9 +112,9 @@ class IsUnlocked extends Component<Props, State> {
const { unlock, fetchAccounts } = this.props const { unlock, fetchAccounts } = this.props
const { inputValue } = this.state const { inputValue } = this.state
const isAccountsEncrypted = await db.isKeyEncrypted('app', 'accounts') const isAccountsDecrypted = await db.hasBeenDecrypted('app', 'accounts')
try { try {
if (isAccountsEncrypted) { if (!isAccountsDecrypted) {
await db.setEncryptionKey('app', 'accounts', inputValue.password) await db.setEncryptionKey('app', 'accounts', inputValue.password)
await fetchAccounts() await fetchAccounts()
} else if (!db.isEncryptionKeyCorrect('app', 'accounts', inputValue.password)) { } else if (!db.isEncryptionKeyCorrect('app', 'accounts', inputValue.password)) {

10
src/helpers/db/db.spec.js

@ -152,17 +152,17 @@ describe('db', () => {
}) })
test('detect if field is encrypted or not', async () => { test('detect if field is encrypted or not', async () => {
let isEncrypted let isDecrypted
await db.setKey('app', 'encryptedField', { some: 'data' }) await db.setKey('app', 'encryptedField', { some: 'data' })
await db.setEncryptionKey('app', 'encryptedField', 'passw0rd') await db.setEncryptionKey('app', 'encryptedField', 'passw0rd')
db.init(dbPath) db.init(dbPath)
const k = await db.getKey('app', 'encryptedField') const k = await db.getKey('app', 'encryptedField')
expect(k).toBe('HNEETQf+9An6saxmA/X8zg==') expect(k).toBe('HNEETQf+9An6saxmA/X8zg==')
isEncrypted = await db.isKeyEncrypted('app', 'encryptedField') isDecrypted = await db.hasBeenDecrypted('app', 'encryptedField')
expect(isEncrypted).toBe(true) expect(isDecrypted).toBe(false)
await db.setEncryptionKey('app', 'encryptedField', 'passw0rd') await db.setEncryptionKey('app', 'encryptedField', 'passw0rd')
isEncrypted = await db.isKeyEncrypted('app', 'encryptedField') isDecrypted = await db.hasBeenDecrypted('app', 'encryptedField')
expect(isEncrypted).toBe(false) expect(isDecrypted).toBe(true)
const value = await db.getKey('app', 'encryptedField') const value = await db.getKey('app', 'encryptedField')
expect(value).toEqual({ some: 'data' }) expect(value).toEqual({ some: 'data' })
}) })

12
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 * /!\ it consider encrypted if it's string and can't JSON.parse, so
* can brings false-positive if bad used * can brings false-positive if bad used
*/ */
async function isKeyEncrypted(ns: string, keyPath: string): Promise<boolean> { async function hasBeenDecrypted(ns: string, keyPath: string): Promise<boolean> {
const v = await getKey(ns, keyPath) const v = await getKey(ns, keyPath)
if (typeof v !== 'string') return false if (typeof v !== 'string') return true
try { try {
JSON.parse(v) JSON.parse(v)
return false
} catch (err) {
return true return true
} catch (err) {
return false
} }
} }
@ -283,7 +283,7 @@ export default {
getKey, getKey,
getNamespace, getNamespace,
setNamespace, setNamespace,
isKeyEncrypted, hasBeenDecrypted,
save, save,
cleanCache, cleanCache,
resetAll, resetAll,

4
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! // FIXME IMO init() really should only be for window. any other case is a hack!
const isMainWindow = remote.getCurrentWindow().name === 'MainWindow' const isMainWindow = remote.getCurrentWindow().name === 'MainWindow'
const isAccountsEncrypted = await db.isKeyEncrypted('app', 'accounts') const isAccountsDecrypted = await db.hasBeenDecrypted('app', 'accounts')
if (isAccountsEncrypted) { if (!isAccountsDecrypted) {
store.dispatch(lock()) store.dispatch(lock())
} else { } else {
await store.dispatch(fetchAccounts()) await store.dispatch(fetchAccounts())

Loading…
Cancel
Save