From 34648a3d89b23e62439e45585ef8efc30e766266 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Wed, 12 Sep 2018 08:51:36 +0200 Subject: [PATCH] Take user password and try to open db --- components/app.js | 11 ------ components/labels.js | 4 +++ components/password-prompt.js | 36 ++++++++++++------- db/index.js | 19 +++++----- local-storage/index.js | 5 +++ nodejs-assets/nodejs-project/main.js | 20 +++++------ .../nodejs-project/package-lock.json | 11 ------ nodejs-assets/nodejs-project/package.json | 4 +-- 8 files changed, 53 insertions(+), 57 deletions(-) delete mode 100644 nodejs-assets/nodejs-project/package-lock.json diff --git a/components/app.js b/components/app.js index ba7e2c7..ba58437 100644 --- a/components/app.js +++ b/components/app.js @@ -1,6 +1,5 @@ import React, { Component } from 'react' import { View, BackHandler } from 'react-native' -import nodejs from 'nodejs-mobile-react-native' import Header from './header' import Menu from './menu' import Home from './home' @@ -12,7 +11,6 @@ import Settings from './settings' import Stats from './stats' import {headerTitles as titles} from './labels' import setupNotifications from '../lib/notifications' -import { encrypt } from '../db' const isSymptomView = name => Object.keys(symptomViews).indexOf(name) > -1 @@ -24,15 +22,6 @@ export default class App extends Component { } this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonPress) setupNotifications(this.navigate) - nodejs.start('main.js') - nodejs.channel.addListener( - 'message', - msg => { - msg = JSON.parse(msg) - encrypt(msg.message) - }, - this - ) } componentWillUnmount() { diff --git a/components/labels.js b/components/labels.js index c61a939..5f1e749 100644 --- a/components/labels.js +++ b/components/labels.js @@ -90,4 +90,8 @@ export const bleedingPrediction = { predictionStarted1DayLeft: 'Your period is likely to start today or tomorrow.', predictionStartedNoDaysLeft: 'Your period is likely to start today.', predictionInPast: (startDate, endDate) => `Based on your documented data, your period was likely to start between ${startDate} and ${endDate}.` +} + +export const passwordPrompt = { + title: 'Log in' } \ No newline at end of file diff --git a/components/password-prompt.js b/components/password-prompt.js index c7d0644..4453af6 100644 --- a/components/password-prompt.js +++ b/components/password-prompt.js @@ -1,11 +1,11 @@ import React, { Component } from 'react' import { View, TextInput, TouchableOpacity } from 'react-native' import nodejs from 'nodejs-mobile-react-native' -import AppText from './app-text' +import { AppText } from './app-text' import { hasEncryptionObservable } from '../local-storage' import styles from '../styles' -import labels from './labels' -import { openDbConnection } from '../db' +import { passwordPrompt } from './labels' +import { openDbConnection, requestHash } from '../db' import App from './app' export default class PasswordPrompt extends Component { @@ -16,18 +16,24 @@ export default class PasswordPrompt extends Component { if (hasEncryption) { this.setState({showPasswordPrompt: true}) } else { - openDbConnection() + openDbConnection('something-wrong') this.setState({showApp: true}) } }) + nodejs.start('main.js') nodejs.channel.addListener( 'message', - msg => { + async msg => { msg = JSON.parse(msg) - if (msg.type === 'password-check-result') { - if (msg.message) { - this.setState({showApp: true}) - } else { + if (msg.type === 'sha512') { + const key = new Int8Array(64) + for (let i = 0; i < msg.message.length; i++) { + key[i] = msg.message.charCodeAt(i) + } + try { + await openDbConnection(key) + } catch(err) { + console.log(err) this.setState({wrongPassword: true}) } } @@ -47,14 +53,20 @@ export default class PasswordPrompt extends Component { this.setState({password: val})} + style={{ + borderWidth: 1, + borderColor: 'grey', + margin: 5 + }} /> { - + requestHash(this.state.password) }} - style={styles.settingsButton}> + > - {labels.export.button} + { passwordPrompt.title } {this.state.wrongPassword && Wrong PAssword!} diff --git a/db/index.js b/db/index.js index 5560251..d68d46b 100644 --- a/db/index.js +++ b/db/index.js @@ -11,17 +11,17 @@ import { longAndComplicatedCycleWithCervix, cycleWithTempAndNoCervixShift } from './fixtures' -import { hasEncryptionObservable } from '../local-storage' +import { saveEncryptionFlag } from '../local-storage' import dbSchema from './schema' let db const realmConfig = { - dbSchema + schema: dbSchema } -export function openDbConnection(key) { - realmConfig.encyptionKey = key - db = new Realm(realmConfig) +export async function openDbConnection(key) { + realmConfig.encryptionKey = key + db = await Realm.open(realmConfig) } function getBleedingDaysSortedByDate() { @@ -167,8 +167,9 @@ function tryToImportWithoutDelete(cycleDays) { } function requestHash(pw) { + console.log('requesting hash') nodejs.channel.send(JSON.stringify({ - type: 'request-hash', + type: 'request-SHA512', message: pw || 'mypassword' })) } @@ -179,6 +180,8 @@ async function encrypt(hash) { dir.pop() dir.push('copied.realm') const copyPath = dir.join('/') + const exists = await fs.exists(copyPath) + if (exists) await fs.unlink(copyPath) db.writeCopyTo(copyPath) db.close() await fs.unlink(oldPath) @@ -187,9 +190,9 @@ async function encrypt(hash) { if (isNaN(code)) code = 0 return code }) - realmConfig.enryptionKey = key + realmConfig.encryptionKey = key db = new Realm(realmConfig) - await hasEncryptionObservable.set(true) + await saveEncryptionFlag(true) restart.Restart() } diff --git a/local-storage/index.js b/local-storage/index.js index 5fffb42..d41d4d7 100644 --- a/local-storage/index.js +++ b/local-storage/index.js @@ -37,6 +37,11 @@ export async function saveTempReminder(reminder) { export const hasEncryptionObservable = Observable() setObvWithInitValue('hasEncryption', hasEncryptionObservable, false) +export async function saveEncryptionFlag(bool) { + await AsyncStorage.setItem('hasEncryption', JSON.stringify(bool)) + hasEncryptionObservable.set(bool) +} + async function setObvWithInitValue(key, obv, defaultValue) { const result = await AsyncStorage.getItem(key) let value diff --git a/nodejs-assets/nodejs-project/main.js b/nodejs-assets/nodejs-project/main.js index 465e56c..f986f42 100644 --- a/nodejs-assets/nodejs-project/main.js +++ b/nodejs-assets/nodejs-project/main.js @@ -1,22 +1,18 @@ // too see stdout / stderr from this process, run // adb logcat | grep "NODEJS-MOBILE" const rnBridge = require('rn-bridge') -const bcryptjs = require('bcryptjs') +const crypto = require('crypto') rnBridge.channel.on('message', (msg) => { msg = JSON.parse(msg) - if (msg.type === 'request-password-hash') { - const hash = bcryptjs.hashSync(msg.message, 10) + if (msg.type === 'request-SHA512') { + const hash = crypto.createHash('sha256') + hash.update(msg.message) + const result = hash.digest('hex') + console.log(result) rnBridge.channel.send(JSON.stringify({ - type: 'hash', - message: hash - })) - } else if (msg.type === 'request-SHA512') { - // do the thing - } else if (msg.type === 'check-password') { - rnBridge.channel.send(JSON.stringify({ - type: 'password-check-result', - message: bcryptjs.compareSync(msg.message.password, msg.message.hash) + type: 'sha512', + message: result })) } }) \ No newline at end of file diff --git a/nodejs-assets/nodejs-project/package-lock.json b/nodejs-assets/nodejs-project/package-lock.json deleted file mode 100644 index 4a3b908..0000000 --- a/nodejs-assets/nodejs-project/package-lock.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "requires": true, - "lockfileVersion": 1, - "dependencies": { - "bcryptjs": { - "version": "2.4.3", - "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz", - "integrity": "sha1-mrVie5PmBiH/fNrF2pczAn3x0Ms=" - } - } -} diff --git a/nodejs-assets/nodejs-project/package.json b/nodejs-assets/nodejs-project/package.json index a51d26f..3310508 100644 --- a/nodejs-assets/nodejs-project/package.json +++ b/nodejs-assets/nodejs-project/package.json @@ -1,6 +1,4 @@ { - "dependencies": { - "bcryptjs": "^2.4.3" - }, + "dependencies": {}, "main": "main.js" }