2019-07-29 15:43:53 +02:00
const React = require ( 'react' ) ;
2019-07-29 15:58:33 +02:00
const { TextInput , TouchableOpacity , Linking , View , StyleSheet , Text , Button , ScrollView } = require ( 'react-native' ) ;
2017-12-31 15:58:50 +02:00
const EncryptionService = require ( 'lib/services/EncryptionService' ) ;
2017-12-30 21:57:34 +02:00
const { connect } = require ( 'react-redux' ) ;
const { ScreenHeader } = require ( 'lib/components/screen-header.js' ) ;
2020-10-09 19:35:46 +02:00
const { _ } = require ( 'lib/locale' ) ;
2017-12-30 21:57:34 +02:00
const { BaseScreenComponent } = require ( 'lib/components/base-screen.js' ) ;
const { themeStyle } = require ( 'lib/components/global-style.js' ) ;
const { time } = require ( 'lib/time-utils.js' ) ;
const shared = require ( 'lib/components/shared/encryption-config-shared.js' ) ;
2020-06-13 17:20:59 +02:00
const { dialogs } = require ( 'lib/dialogs.js' ) ;
const DialogBox = require ( 'react-native-dialogbox' ) . default ;
2017-12-30 21:57:34 +02:00
class EncryptionConfigScreenComponent extends BaseScreenComponent {
2019-09-13 00:16:42 +02:00
static navigationOptions ( ) {
2017-12-30 21:57:34 +02:00
return { header : null } ;
}
2020-10-09 19:35:46 +02:00
constructor ( props ) {
super ( props ) ;
2017-12-30 21:57:34 +02:00
2017-12-31 15:58:50 +02:00
this . state = {
passwordPromptShow : false ,
passwordPromptAnswer : '' ,
2019-10-09 00:04:25 +02:00
passwordPromptConfirmAnswer : '' ,
2017-12-31 15:58:50 +02:00
} ;
2020-10-09 19:35:46 +02:00
shared . constructor ( this , props ) ;
2017-12-30 21:57:34 +02:00
this . styles _ = { } ;
}
componentWillUnmount ( ) {
this . isMounted _ = false ;
}
initState ( props ) {
return shared . initState ( this , props ) ;
}
async refreshStats ( ) {
return shared . refreshStats ( this ) ;
}
2020-03-13 19:42:50 +02:00
componentDidMount ( ) {
this . isMounted _ = true ;
shared . componentDidMount ( this ) ;
2017-12-30 21:57:34 +02:00
}
2020-03-13 19:42:50 +02:00
componentDidUpdate ( prevProps ) {
shared . componentDidUpdate ( this , prevProps ) ;
2017-12-30 21:57:34 +02:00
}
async checkPasswords ( ) {
return shared . checkPasswords ( this ) ;
}
styles ( ) {
2020-09-15 15:01:07 +02:00
const themeId = this . props . themeId ;
2017-12-30 21:57:34 +02:00
const theme = themeStyle ( themeId ) ;
if ( this . styles _ [ themeId ] ) return this . styles _ [ themeId ] ;
this . styles _ = { } ;
2020-03-14 01:46:14 +02:00
const styles = {
2017-12-30 21:57:34 +02:00
titleText : {
flex : 1 ,
fontWeight : 'bold' ,
flexDirection : 'column' ,
fontSize : theme . fontSize ,
paddingTop : 5 ,
paddingBottom : 5 ,
2017-12-31 15:58:50 +02:00
marginTop : theme . marginTop ,
marginBottom : 5 ,
2018-01-06 11:06:41 +02:00
color : theme . color ,
2017-12-30 21:57:34 +02:00
} ,
normalText : {
flex : 1 ,
fontSize : theme . fontSize ,
2018-01-06 11:06:41 +02:00
color : theme . color ,
2017-12-30 21:57:34 +02:00
} ,
2019-10-09 00:04:25 +02:00
normalTextInput : {
margin : 10 ,
color : theme . color ,
borderWidth : 1 ,
borderColor : theme . dividerColor ,
} ,
2017-12-30 21:57:34 +02:00
container : {
flex : 1 ,
padding : theme . margin ,
} ,
2019-07-29 15:43:53 +02:00
} ;
2017-12-30 21:57:34 +02:00
this . styles _ [ themeId ] = StyleSheet . create ( styles ) ;
return this . styles _ [ themeId ] ;
}
renderMasterKey ( num , mk ) {
2020-09-15 15:01:07 +02:00
const theme = themeStyle ( this . props . themeId ) ;
2017-12-30 21:57:34 +02:00
const onSaveClick = ( ) => {
return shared . onSavePasswordClick ( this , mk ) ;
2019-07-29 15:43:53 +02:00
} ;
2017-12-30 21:57:34 +02:00
2020-05-21 10:14:33 +02:00
const onPasswordChange = text => {
2017-12-30 21:57:34 +02:00
return shared . onPasswordChange ( this , mk , text ) ;
2019-07-29 15:43:53 +02:00
} ;
2017-12-30 21:57:34 +02:00
2020-10-09 19:35:46 +02:00
const password = this . state . passwords [ mk . id ] ? this . state . passwords [ mk . id ] : '' ;
2017-12-30 21:57:34 +02:00
const passwordOk = this . state . passwordChecks [ mk . id ] === true ? '✔' : '❌' ;
2019-07-29 15:43:53 +02:00
const inputStyle = { flex : 1 , marginRight : 10 , color : theme . color } ;
2018-12-15 02:45:35 +02:00
inputStyle . borderBottomWidth = 1 ;
2020-06-10 23:08:59 +02:00
inputStyle . borderBottomColor = theme . dividerColor ;
2019-07-29 15:43:53 +02:00
2017-12-30 21:57:34 +02:00
return (
< View key = { mk . id } >
2019-07-29 15:43:53 +02:00
< Text style = { this . styles ( ) . titleText } > { _ ( 'Master Key %s' , mk . id . substr ( 0 , 6 ) ) } < / T e x t >
2017-12-30 21:57:34 +02:00
< Text style = { this . styles ( ) . normalText } > { _ ( 'Created: %s' , time . formatMsToLocal ( mk . created _time ) ) } < / T e x t >
2019-07-29 15:43:53 +02:00
< View style = { { flexDirection : 'row' , alignItems : 'center' } } >
< Text style = { { flex : 0 , fontSize : theme . fontSize , marginRight : 10 , color : theme . color } } > { _ ( 'Password:' ) } < / T e x t >
2020-05-21 10:14:33 +02:00
< TextInput selectionColor = { theme . textSelectionColor } keyboardAppearance = { theme . keyboardAppearance } secureTextEntry = { true } value = { password } onChangeText = { text => onPasswordChange ( text ) } style = { inputStyle } > < / T e x t I n p u t >
2019-07-29 15:43:53 +02:00
< Text style = { { fontSize : theme . fontSize , marginRight : 10 , color : theme . color } } > { passwordOk } < / T e x t >
2017-12-30 21:57:34 +02:00
< Button title = { _ ( 'Save' ) } onPress = { ( ) => onSaveClick ( ) } > < / B u t t o n >
< / V i e w >
< / V i e w >
) ;
}
2017-12-31 15:58:50 +02:00
passwordPromptComponent ( ) {
2020-09-15 15:01:07 +02:00
const theme = themeStyle ( this . props . themeId ) ;
2017-12-31 15:58:50 +02:00
const onEnableClick = async ( ) => {
try {
const password = this . state . passwordPromptAnswer ;
if ( ! password ) throw new Error ( _ ( 'Password cannot be empty' ) ) ;
2019-10-09 00:04:25 +02:00
const password2 = this . state . passwordPromptConfirmAnswer ;
if ( ! password2 ) throw new Error ( _ ( 'Confirm password cannot be empty' ) ) ;
if ( password !== password2 ) throw new Error ( _ ( 'Passwords do not match!' ) ) ;
2017-12-31 15:58:50 +02:00
await EncryptionService . instance ( ) . generateMasterKeyAndEnableEncryption ( password ) ;
this . setState ( { passwordPromptShow : false } ) ;
} catch ( error ) {
2020-06-13 17:20:59 +02:00
await dialogs . error ( this , error . message ) ;
2017-12-31 15:58:50 +02:00
}
2019-07-29 15:43:53 +02:00
} ;
2017-12-31 15:58:50 +02:00
return (
2019-07-29 15:43:53 +02:00
< View style = { { flex : 1 , borderColor : theme . dividerColor , borderWidth : 1 , padding : 10 , marginTop : 10 , marginBottom : 10 } } >
2019-10-09 00:04:25 +02:00
< Text style = { { fontSize : theme . fontSize , color : theme . color , marginBottom : 10 } } > { _ ( 'Enabling encryption means *all* your notes and attachments are going to be re-synchronised and sent encrypted to the sync target. Do not lose the password as, for security purposes, this will be the *only* way to decrypt the data! To enable encryption, please enter your password below.' ) } < / T e x t >
< Text style = { this . styles ( ) . normalText } > { _ ( 'Password:' ) } < / T e x t >
2019-07-29 15:43:53 +02:00
< TextInput
selectionColor = { theme . textSelectionColor }
2020-04-17 00:35:31 +02:00
keyboardAppearance = { theme . keyboardAppearance }
2019-10-09 00:04:25 +02:00
style = { this . styles ( ) . normalTextInput }
2019-07-29 15:43:53 +02:00
secureTextEntry = { true }
value = { this . state . passwordPromptAnswer }
2020-05-21 10:14:33 +02:00
onChangeText = { text => {
2019-07-29 15:43:53 +02:00
this . setState ( { passwordPromptAnswer : text } ) ;
} }
> < / T e x t I n p u t >
2019-10-09 00:04:25 +02:00
< Text style = { this . styles ( ) . normalText } > { _ ( 'Confirm password:' ) } < / T e x t >
< TextInput
selectionColor = { theme . textSelectionColor }
2020-04-17 00:35:31 +02:00
keyboardAppearance = { theme . keyboardAppearance }
2019-10-09 00:04:25 +02:00
style = { this . styles ( ) . normalTextInput }
secureTextEntry = { true }
value = { this . state . passwordPromptConfirmAnswer }
2020-05-21 10:14:33 +02:00
onChangeText = { text => {
2019-10-09 00:04:25 +02:00
this . setState ( { passwordPromptConfirmAnswer : text } ) ;
} }
> < / T e x t I n p u t >
2019-07-29 15:43:53 +02:00
< View style = { { flexDirection : 'row' } } >
< View style = { { flex : 1 , marginRight : 10 } } >
< Button
title = { _ ( 'Enable' ) }
onPress = { ( ) => {
onEnableClick ( ) ;
} }
> < / B u t t o n >
2017-12-31 15:58:50 +02:00
< / V i e w >
2019-07-29 15:43:53 +02:00
< View style = { { flex : 1 } } >
< Button
title = { _ ( 'Cancel' ) }
onPress = { ( ) => {
this . setState ( { passwordPromptShow : false } ) ;
} }
> < / B u t t o n >
2017-12-31 15:58:50 +02:00
< / V i e w >
< / V i e w >
< / V i e w >
) ;
}
2017-12-30 21:57:34 +02:00
render ( ) {
2020-09-15 15:01:07 +02:00
const theme = themeStyle ( this . props . themeId ) ;
2020-03-13 19:42:50 +02:00
const masterKeys = this . props . masterKeys ;
2017-12-30 21:57:34 +02:00
const decryptedItemsInfo = this . props . encryptionEnabled ? < Text style = { this . styles ( ) . normalText } > { shared . decryptedStatText ( this ) } < / T e x t > : n u l l ;
const mkComps = [ ] ;
2018-02-01 21:01:20 +02:00
2020-03-14 01:46:14 +02:00
const nonExistingMasterKeyIds = this . props . notLoadedMasterKeys . slice ( ) ;
2018-02-01 21:01:20 +02:00
2017-12-30 21:57:34 +02:00
for ( let i = 0 ; i < masterKeys . length ; i ++ ) {
const mk = masterKeys [ i ] ;
2019-07-29 15:43:53 +02:00
mkComps . push ( this . renderMasterKey ( i + 1 , mk ) ) ;
2018-02-01 21:01:20 +02:00
const idx = nonExistingMasterKeyIds . indexOf ( mk . id ) ;
if ( idx >= 0 ) nonExistingMasterKeyIds . splice ( idx , 1 ) ;
2017-12-30 21:57:34 +02:00
}
2017-12-31 15:58:50 +02:00
const onToggleButtonClick = async ( ) => {
if ( this . props . encryptionEnabled ) {
2020-06-13 17:20:59 +02:00
const ok = await dialogs . confirm ( this , _ ( 'Disabling encryption means *all* your notes and attachments are going to be re-synchronised and sent unencrypted to the sync target. Do you wish to continue?' ) ) ;
2017-12-31 15:58:50 +02:00
if ( ! ok ) return ;
try {
await EncryptionService . instance ( ) . disableEncryption ( ) ;
} catch ( error ) {
2020-06-13 17:20:59 +02:00
await dialogs . error ( this , error . message ) ;
2017-12-31 15:58:50 +02:00
}
} else {
this . setState ( {
passwordPromptShow : true ,
passwordPromptAnswer : '' ,
2019-10-09 00:04:25 +02:00
passwordPromptConfirmAnswer : '' ,
2017-12-31 15:58:50 +02:00
} ) ;
return ;
}
} ;
2018-02-01 21:01:20 +02:00
let nonExistingMasterKeySection = null ;
if ( nonExistingMasterKeyIds . length ) {
const rows = [ ] ;
for ( let i = 0 ; i < nonExistingMasterKeyIds . length ; i ++ ) {
const id = nonExistingMasterKeyIds [ i ] ;
2019-07-29 15:43:53 +02:00
rows . push (
< Text style = { this . styles ( ) . normalText } key = { id } >
{ id }
< / T e x t >
) ;
2018-02-01 21:01:20 +02:00
}
nonExistingMasterKeySection = (
< View >
< Text style = { this . styles ( ) . titleText } > { _ ( 'Missing Master Keys' ) } < / T e x t >
< Text style = { this . styles ( ) . normalText } > { _ ( 'The master keys with these IDs are used to encrypt some of your items, however the application does not currently have access to them. It is likely they will eventually be downloaded via synchronisation.' ) } < / T e x t >
2019-07-29 15:43:53 +02:00
< View style = { { marginTop : 10 } } > { rows } < / V i e w >
2018-02-01 21:01:20 +02:00
< / V i e w >
) ;
}
2017-12-31 15:58:50 +02:00
const passwordPromptComp = this . state . passwordPromptShow ? this . passwordPromptComponent ( ) : null ;
2019-07-29 15:43:53 +02:00
const toggleButton = ! this . state . passwordPromptShow ? (
< View style = { { marginTop : 10 } } >
< Button title = { this . props . encryptionEnabled ? _ ( 'Disable encryption' ) : _ ( 'Enable encryption' ) } onPress = { ( ) => onToggleButtonClick ( ) } > < / B u t t o n >
< / V i e w >
) : null ;
2017-12-31 15:58:50 +02:00
2017-12-30 21:57:34 +02:00
return (
2020-09-15 15:01:07 +02:00
< View style = { this . rootStyle ( this . props . themeId ) . root } >
2019-07-29 15:43:53 +02:00
< ScreenHeader title = { _ ( 'Encryption Config' ) } / >
2017-12-30 21:57:34 +02:00
< ScrollView style = { this . styles ( ) . container } >
2019-07-29 15:43:53 +02:00
{
< View style = { { backgroundColor : theme . warningBackgroundColor , paddingTop : 5 , paddingBottom : 5 , paddingLeft : 10 , paddingRight : 10 } } >
2019-08-29 18:38:54 +02:00
< Text > { _ ( 'For more information about End-To-End Encryption (E2EE) and advice on how to enable it please check the documentation:' ) } < / T e x t >
2019-07-29 15:43:53 +02:00
< TouchableOpacity
onPress = { ( ) => {
Linking . openURL ( 'https://joplinapp.org/e2ee/' ) ;
} }
>
< Text > https : //joplinapp.org/e2ee/</Text>
< / T o u c h a b l e O p a c i t y >
< / V i e w >
}
2018-01-05 19:40:57 +02:00
2017-12-30 21:57:34 +02:00
< Text style = { this . styles ( ) . titleText } > { _ ( 'Status' ) } < / T e x t >
< Text style = { this . styles ( ) . normalText } > { _ ( 'Encryption is: %s' , this . props . encryptionEnabled ? _ ( 'Enabled' ) : _ ( 'Disabled' ) ) } < / T e x t >
{ decryptedItemsInfo }
2017-12-31 15:58:50 +02:00
{ toggleButton }
{ passwordPromptComp }
2017-12-30 21:57:34 +02:00
{ mkComps }
2018-02-01 21:01:20 +02:00
{ nonExistingMasterKeySection }
2019-07-29 15:43:53 +02:00
< View style = { { flex : 1 , height : 20 } } > < / V i e w >
2017-12-30 21:57:34 +02:00
< / S c r o l l V i e w >
2020-06-13 17:20:59 +02:00
< DialogBox
ref = { dialogbox => {
this . dialogbox = dialogbox ;
} }
/ >
2017-12-30 21:57:34 +02:00
< / V i e w >
) ;
}
}
2020-05-21 10:14:33 +02:00
const EncryptionConfigScreen = connect ( state => {
2019-07-29 15:43:53 +02:00
return {
2020-09-15 15:01:07 +02:00
themeId : state . settings . theme ,
2019-07-29 15:43:53 +02:00
masterKeys : state . masterKeys ,
passwords : state . settings [ 'encryption.passwordCache' ] ,
encryptionEnabled : state . settings [ 'encryption.enabled' ] ,
activeMasterKeyId : state . settings [ 'encryption.activeMasterKeyId' ] ,
notLoadedMasterKeys : state . notLoadedMasterKeys ,
} ;
} ) ( EncryptionConfigScreenComponent ) ;
module . exports = { EncryptionConfigScreen } ;