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 { 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() {
+4
View File
@@ -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'
}
+24 -12
View File
@@ -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 {
<View>
<TextInput
onChangeText={val => this.setState({password: val})}
style={{
borderWidth: 1,
borderColor: 'grey',
margin: 5
}}
/>
<TouchableOpacity
style={styles.settingsButton}
onPress={async () => {
requestHash(this.state.password)
}}
style={styles.settingsButton}>
>
<AppText style={styles.settingsButtonText}>
{labels.export.button}
{ passwordPrompt.title }
</AppText>
</TouchableOpacity>
{this.state.wrongPassword && <AppText>Wrong PAssword!</AppText>}
+11 -8
View File
@@ -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()
}
+5
View File
@@ -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
+8 -12
View File
@@ -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
}))
}
})
-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": {
"bcryptjs": "^2.4.3"
},
"dependencies": {},
"main": "main.js"
}