Take user password and try to open db

This commit is contained in:
Julia Friesel
2018-09-12 08:51:36 +02:00
parent 3d61459f30
commit 34648a3d89
8 changed files with 53 additions and 57 deletions
-11
View File
@@ -1,6 +1,5 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { View, BackHandler } from 'react-native' import { View, BackHandler } from 'react-native'
import nodejs from 'nodejs-mobile-react-native'
import Header from './header' import Header from './header'
import Menu from './menu' import Menu from './menu'
import Home from './home' import Home from './home'
@@ -12,7 +11,6 @@ import Settings from './settings'
import Stats from './stats' import Stats from './stats'
import {headerTitles as titles} from './labels' import {headerTitles as titles} from './labels'
import setupNotifications from '../lib/notifications' import setupNotifications from '../lib/notifications'
import { encrypt } from '../db'
const isSymptomView = name => Object.keys(symptomViews).indexOf(name) > -1 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) this.backHandler = BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonPress)
setupNotifications(this.navigate) setupNotifications(this.navigate)
nodejs.start('main.js')
nodejs.channel.addListener(
'message',
msg => {
msg = JSON.parse(msg)
encrypt(msg.message)
},
this
)
} }
componentWillUnmount() { componentWillUnmount() {
+4
View File
@@ -91,3 +91,7 @@ export const bleedingPrediction = {
predictionStartedNoDaysLeft: 'Your period is likely to start today.', 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}.` predictionInPast: (startDate, endDate) => `Based on your documented data, your period was likely to start between ${startDate} and ${endDate}.`
} }
export const passwordPrompt = {
title: 'Log in'
}
+24 -12
View File
@@ -1,11 +1,11 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { View, TextInput, TouchableOpacity } from 'react-native' import { View, TextInput, TouchableOpacity } from 'react-native'
import nodejs from 'nodejs-mobile-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 { hasEncryptionObservable } from '../local-storage'
import styles from '../styles' import styles from '../styles'
import labels from './labels' import { passwordPrompt } from './labels'
import { openDbConnection } from '../db' import { openDbConnection, requestHash } from '../db'
import App from './app' import App from './app'
export default class PasswordPrompt extends Component { export default class PasswordPrompt extends Component {
@@ -16,18 +16,24 @@ export default class PasswordPrompt extends Component {
if (hasEncryption) { if (hasEncryption) {
this.setState({showPasswordPrompt: true}) this.setState({showPasswordPrompt: true})
} else { } else {
openDbConnection() openDbConnection('something-wrong')
this.setState({showApp: true}) this.setState({showApp: true})
} }
}) })
nodejs.start('main.js')
nodejs.channel.addListener( nodejs.channel.addListener(
'message', 'message',
msg => { async msg => {
msg = JSON.parse(msg) msg = JSON.parse(msg)
if (msg.type === 'password-check-result') { if (msg.type === 'sha512') {
if (msg.message) { const key = new Int8Array(64)
this.setState({showApp: true}) for (let i = 0; i < msg.message.length; i++) {
} else { key[i] = msg.message.charCodeAt(i)
}
try {
await openDbConnection(key)
} catch(err) {
console.log(err)
this.setState({wrongPassword: true}) this.setState({wrongPassword: true})
} }
} }
@@ -47,14 +53,20 @@ export default class PasswordPrompt extends Component {
<View> <View>
<TextInput <TextInput
onChangeText={val => this.setState({password: val})} onChangeText={val => this.setState({password: val})}
style={{
borderWidth: 1,
borderColor: 'grey',
margin: 5
}}
/> />
<TouchableOpacity <TouchableOpacity
style={styles.settingsButton}
onPress={async () => { onPress={async () => {
requestHash(this.state.password)
}} }}
style={styles.settingsButton}> >
<AppText style={styles.settingsButtonText}> <AppText style={styles.settingsButtonText}>
{labels.export.button} { passwordPrompt.title }
</AppText> </AppText>
</TouchableOpacity> </TouchableOpacity>
{this.state.wrongPassword && <AppText>Wrong PAssword!</AppText>} {this.state.wrongPassword && <AppText>Wrong PAssword!</AppText>}
+11 -8
View File
@@ -11,17 +11,17 @@ import {
longAndComplicatedCycleWithCervix, longAndComplicatedCycleWithCervix,
cycleWithTempAndNoCervixShift cycleWithTempAndNoCervixShift
} from './fixtures' } from './fixtures'
import { hasEncryptionObservable } from '../local-storage' import { saveEncryptionFlag } from '../local-storage'
import dbSchema from './schema' import dbSchema from './schema'
let db let db
const realmConfig = { const realmConfig = {
dbSchema schema: dbSchema
} }
export function openDbConnection(key) { export async function openDbConnection(key) {
realmConfig.encyptionKey = key realmConfig.encryptionKey = key
db = new Realm(realmConfig) db = await Realm.open(realmConfig)
} }
function getBleedingDaysSortedByDate() { function getBleedingDaysSortedByDate() {
@@ -167,8 +167,9 @@ function tryToImportWithoutDelete(cycleDays) {
} }
function requestHash(pw) { function requestHash(pw) {
console.log('requesting hash')
nodejs.channel.send(JSON.stringify({ nodejs.channel.send(JSON.stringify({
type: 'request-hash', type: 'request-SHA512',
message: pw || 'mypassword' message: pw || 'mypassword'
})) }))
} }
@@ -179,6 +180,8 @@ async function encrypt(hash) {
dir.pop() dir.pop()
dir.push('copied.realm') dir.push('copied.realm')
const copyPath = dir.join('/') const copyPath = dir.join('/')
const exists = await fs.exists(copyPath)
if (exists) await fs.unlink(copyPath)
db.writeCopyTo(copyPath) db.writeCopyTo(copyPath)
db.close() db.close()
await fs.unlink(oldPath) await fs.unlink(oldPath)
@@ -187,9 +190,9 @@ async function encrypt(hash) {
if (isNaN(code)) code = 0 if (isNaN(code)) code = 0
return code return code
}) })
realmConfig.enryptionKey = key realmConfig.encryptionKey = key
db = new Realm(realmConfig) db = new Realm(realmConfig)
await hasEncryptionObservable.set(true) await saveEncryptionFlag(true)
restart.Restart() restart.Restart()
} }
+5
View File
@@ -37,6 +37,11 @@ export async function saveTempReminder(reminder) {
export const hasEncryptionObservable = Observable() export const hasEncryptionObservable = Observable()
setObvWithInitValue('hasEncryption', hasEncryptionObservable, false) 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) { async function setObvWithInitValue(key, obv, defaultValue) {
const result = await AsyncStorage.getItem(key) const result = await AsyncStorage.getItem(key)
let value let value
+8 -12
View File
@@ -1,22 +1,18 @@
// too see stdout / stderr from this process, run // too see stdout / stderr from this process, run
// adb logcat | grep "NODEJS-MOBILE" // adb logcat | grep "NODEJS-MOBILE"
const rnBridge = require('rn-bridge') const rnBridge = require('rn-bridge')
const bcryptjs = require('bcryptjs') const crypto = require('crypto')
rnBridge.channel.on('message', (msg) => { rnBridge.channel.on('message', (msg) => {
msg = JSON.parse(msg) msg = JSON.parse(msg)
if (msg.type === 'request-password-hash') { if (msg.type === 'request-SHA512') {
const hash = bcryptjs.hashSync(msg.message, 10) const hash = crypto.createHash('sha256')
hash.update(msg.message)
const result = hash.digest('hex')
console.log(result)
rnBridge.channel.send(JSON.stringify({ rnBridge.channel.send(JSON.stringify({
type: 'hash', type: 'sha512',
message: hash message: result
}))
} 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)
})) }))
} }
}) })
-11
View File
@@ -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="
}
}
}
+1 -3
View File
@@ -1,6 +1,4 @@
{ {
"dependencies": { "dependencies": {},
"bcryptjs": "^2.4.3"
},
"main": "main.js" "main": "main.js"
} }