Browse Source
Rename db.isKeyEncrypted to db.hasBeenDecrypted for clarity
and change its return value, eh.
master
meriadec
7 years ago
No known key found for this signature in database
GPG Key ID: 1D2FC2305E2CB399
4 changed files with
15 additions and
15 deletions
-
src/components/IsUnlocked.js
-
src/helpers/db/db.spec.js
-
src/helpers/db/index.js
-
src/renderer/init.js
|
|
@ -112,9 +112,9 @@ class IsUnlocked extends Component<Props, State> { |
|
|
|
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)) { |
|
|
|
|
|
@ -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' }) |
|
|
|
}) |
|
|
|
|
|
@ -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<boolean> { |
|
|
|
async function hasBeenDecrypted(ns: string, keyPath: string): Promise<boolean> { |
|
|
|
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, |
|
|
|
|
|
@ -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()) |
|
|
|