2017-12-14 20:53:08 +02:00
const React = require ( 'react' ) ;
const { connect } = require ( 'react-redux' ) ;
2017-12-24 10:36:31 +02:00
const Setting = require ( 'lib/models/Setting' ) ;
2017-12-14 21:39:13 +02:00
const EncryptionService = require ( 'lib/services/EncryptionService' ) ;
2017-12-14 20:53:08 +02:00
const { themeStyle } = require ( '../theme.js' ) ;
const { _ } = require ( 'lib/locale.js' ) ;
const { time } = require ( 'lib/time-utils.js' ) ;
2017-12-24 10:36:31 +02:00
const dialogs = require ( './dialogs' ) ;
2017-12-30 21:57:34 +02:00
const shared = require ( 'lib/components/shared/encryption-config-shared.js' ) ;
2018-01-05 19:40:57 +02:00
const { bridge } = require ( 'electron' ) . remote . require ( './bridge' ) ;
2017-12-14 20:53:08 +02:00
2017-12-21 21:06:08 +02:00
class EncryptionConfigScreenComponent extends React . Component {
2017-12-14 21:39:13 +02:00
constructor ( ) {
super ( ) ;
2017-12-30 21:57:34 +02:00
shared . constructor ( this ) ;
2017-12-24 10:36:31 +02:00
}
componentDidMount ( ) {
this . isMounted _ = true ;
}
componentWillUnmount ( ) {
this . isMounted _ = false ;
2017-12-14 21:39:13 +02:00
}
2017-12-24 12:23:51 +02:00
initState ( props ) {
2017-12-30 21:57:34 +02:00
return shared . initState ( this , props ) ;
}
2017-12-24 10:36:31 +02:00
2017-12-30 21:57:34 +02:00
async refreshStats ( ) {
return shared . refreshStats ( this ) ;
2017-12-24 10:36:31 +02:00
}
2019-12-13 03:16:34 +02:00
UNSAFE _componentWillMount ( ) {
2017-12-24 12:23:51 +02:00
this . initState ( this . props ) ;
}
2019-12-13 03:16:34 +02:00
UNSAFE _componentWillReceiveProps ( nextProps ) {
2017-12-24 12:23:51 +02:00
this . initState ( nextProps ) ;
}
2017-12-14 21:39:13 +02:00
async checkPasswords ( ) {
2017-12-30 21:57:34 +02:00
return shared . checkPasswords ( this ) ;
2017-12-14 21:39:13 +02:00
}
2017-12-14 20:53:08 +02:00
renderMasterKey ( mk ) {
2017-12-24 10:36:31 +02:00
const theme = themeStyle ( this . props . theme ) ;
2018-11-08 00:37:13 +02:00
const passwordStyle = {
color : theme . color ,
backgroundColor : theme . backgroundColor ,
border : '1px solid' ,
borderColor : theme . dividerColor ,
2019-07-29 14:13:23 +02:00
} ;
2018-11-08 00:37:13 +02:00
2017-12-14 21:39:13 +02:00
const onSaveClick = ( ) => {
2017-12-30 21:57:34 +02:00
return shared . onSavePasswordClick ( this , mk ) ;
2019-07-29 14:13:23 +02:00
} ;
2017-12-14 21:39:13 +02:00
2019-07-29 14:13:23 +02:00
const onPasswordChange = event => {
2017-12-30 21:57:34 +02:00
return shared . onPasswordChange ( this , mk , event . target . value ) ;
2019-07-29 14:13:23 +02:00
} ;
2017-12-14 21:39:13 +02:00
const password = this . state . passwords [ mk . id ] ? this . state . passwords [ mk . id ] : '' ;
const active = this . props . activeMasterKeyId === mk . id ? '✔' : '' ;
const passwordOk = this . state . passwordChecks [ mk . id ] === true ? '✔' : '❌' ;
2017-12-14 20:53:08 +02:00
return (
< tr key = { mk . id } >
2017-12-24 10:36:31 +02:00
< td style = { theme . textStyle } > { active } < / td >
< td style = { theme . textStyle } > { mk . id } < / td >
< td style = { theme . textStyle } > { mk . source _application } < / td >
< td style = { theme . textStyle } > { time . formatMsToLocal ( mk . created _time ) } < / td >
< td style = { theme . textStyle } > { time . formatMsToLocal ( mk . updated _time ) } < / td >
2019-07-29 14:13:23 +02:00
< td style = { theme . textStyle } >
< input type = "password" style = { passwordStyle } value = { password } onChange = { event => onPasswordChange ( event ) } / > { ' ' }
< button style = { theme . buttonStyle } onClick = { ( ) => onSaveClick ( ) } >
{ _ ( 'Save' ) }
< / button >
< / td >
2017-12-24 10:36:31 +02:00
< td style = { theme . textStyle } > { passwordOk } < / td >
2017-12-14 20:53:08 +02:00
< / tr >
) ;
}
render ( ) {
const theme = themeStyle ( this . props . theme ) ;
2017-12-14 21:39:13 +02:00
const masterKeys = this . state . masterKeys ;
2017-12-24 10:36:31 +02:00
const containerPadding = 10 ;
2017-12-14 20:53:08 +02:00
2018-11-08 00:37:13 +02:00
const containerStyle = Object . assign ( { } , theme . containerStyle , {
2017-12-24 10:36:31 +02:00
padding : containerPadding ,
overflow : 'auto' ,
2018-11-08 00:37:13 +02:00
} ) ;
2017-12-24 10:36:31 +02:00
2017-12-14 20:53:08 +02:00
const mkComps = [ ] ;
2018-02-01 21:01:20 +02:00
let nonExistingMasterKeyIds = this . props . notLoadedMasterKeys . slice ( ) ;
2017-12-14 20:53:08 +02:00
for ( let i = 0 ; i < masterKeys . length ; i ++ ) {
const mk = masterKeys [ i ] ;
mkComps . push ( this . renderMasterKey ( mk ) ) ;
2018-02-01 21:01:20 +02:00
const idx = nonExistingMasterKeyIds . indexOf ( mk . id ) ;
if ( idx >= 0 ) nonExistingMasterKeyIds . splice ( idx , 1 ) ;
2017-12-14 20:53:08 +02:00
}
2017-12-24 10:36:31 +02:00
const onToggleButtonClick = async ( ) => {
const isEnabled = Setting . value ( 'encryption.enabled' ) ;
let answer = null ;
if ( isEnabled ) {
2017-12-31 15:58:50 +02:00
answer = await dialogs . confirm ( _ ( '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-24 10:36:31 +02:00
} else {
2017-12-31 15:58:50 +02:00
answer = await dialogs . prompt ( _ ( '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.' ) , '' , '' , { type : 'password' } ) ;
2017-12-24 10:36:31 +02:00
}
if ( ! answer ) return ;
try {
if ( isEnabled ) {
await EncryptionService . instance ( ) . disableEncryption ( ) ;
} else {
2017-12-24 12:23:51 +02:00
await EncryptionService . instance ( ) . generateMasterKeyAndEnableEncryption ( answer ) ;
2017-12-24 10:36:31 +02:00
}
} catch ( error ) {
await dialogs . alert ( error . message ) ;
}
2019-07-29 14:13:23 +02:00
} ;
2017-12-24 10:36:31 +02:00
2018-01-21 19:01:37 +02:00
const decryptedItemsInfo = < p style = { theme . textStyle } > { shared . decryptedStatText ( this ) } < / p > ;
2019-07-29 14:13:23 +02:00
const toggleButton = (
< button
style = { theme . buttonStyle }
onClick = { ( ) => {
onToggleButtonClick ( ) ;
} }
>
{ this . props . encryptionEnabled ? _ ( 'Disable encryption' ) : _ ( 'Enable encryption' ) }
< / button >
) ;
2017-12-24 10:36:31 +02:00
let masterKeySection = null ;
if ( mkComps . length ) {
masterKeySection = (
< div >
< h1 style = { theme . h1Style } > { _ ( 'Master Keys' ) } < / h1 >
< table >
< tbody >
< tr >
< th style = { theme . textStyle } > { _ ( 'Active' ) } < / th >
< th style = { theme . textStyle } > { _ ( 'ID' ) } < / th >
< th style = { theme . textStyle } > { _ ( 'Source' ) } < / th >
< th style = { theme . textStyle } > { _ ( 'Created' ) } < / th >
< th style = { theme . textStyle } > { _ ( 'Updated' ) } < / th >
< th style = { theme . textStyle } > { _ ( 'Password' ) } < / th >
< th style = { theme . textStyle } > { _ ( 'Password OK' ) } < / th >
< / tr >
{ mkComps }
< / tbody >
< / table >
< p style = { theme . textStyle } > { _ ( 'Note: Only one master key is going to be used for encryption (the one marked as "active"). Any of the keys might be used for decryption, depending on how the notes or notebooks were originally encrypted.' ) } < / p >
< / div >
) ;
}
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 14:13:23 +02:00
rows . push (
< tr key = { id } >
< td style = { theme . textStyle } > { id } < / td >
< / tr >
) ;
2018-02-01 21:01:20 +02:00
}
nonExistingMasterKeySection = (
< div >
< h1 style = { theme . h1Style } > { _ ( 'Missing Master Keys' ) } < / h1 >
< p style = { theme . textStyle } > { _ ( '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.' ) } < / p >
< table >
< tbody >
< tr >
< th style = { theme . textStyle } > { _ ( 'ID' ) } < / th >
< / tr >
2019-07-29 14:13:23 +02:00
{ rows }
2018-02-01 21:01:20 +02:00
< / tbody >
< / table >
< / div >
) ;
}
2017-12-14 20:53:08 +02:00
return (
< div >
2017-12-24 10:36:31 +02:00
< div style = { containerStyle } >
2019-07-29 14:13:23 +02:00
{
< div style = { { backgroundColor : theme . warningBackgroundColor , paddingLeft : 10 , paddingRight : 10 , paddingTop : 2 , paddingBottom : 2 } } >
< p style = { theme . textStyle } >
2019-08-29 18:38:54 +02:00
< span > { _ ( 'For more information about End-To-End Encryption (E2EE) and advice on how to enable it please check the documentation:' ) } < / span > { ' ' }
2019-07-29 14:13:23 +02:00
< a
onClick = { ( ) => {
bridge ( ) . openExternal ( 'https://joplinapp.org/e2ee/' ) ;
} }
href = "#"
2019-11-28 19:09:52 +02:00
style = { theme . urlStyle }
2019-07-29 14:13:23 +02:00
>
https : //joplinapp.org/e2ee/
< / a >
< / p >
< / div >
}
2017-12-24 10:36:31 +02:00
< h1 style = { theme . h1Style } > { _ ( 'Status' ) } < / h1 >
2019-07-29 14:13:23 +02:00
< p style = { theme . textStyle } >
{ _ ( 'Encryption is:' ) } < strong > { this . props . encryptionEnabled ? _ ( 'Enabled' ) : _ ( 'Disabled' ) } < / strong >
< / p >
2017-12-24 10:36:31 +02:00
{ decryptedItemsInfo }
{ toggleButton }
{ masterKeySection }
2018-02-01 21:01:20 +02:00
{ nonExistingMasterKeySection }
2017-12-24 10:36:31 +02:00
< / div >
2017-12-14 20:53:08 +02:00
< / div >
) ;
}
}
2019-07-29 14:13:23 +02:00
const mapStateToProps = state => {
2017-12-14 20:53:08 +02:00
return {
theme : state . settings . theme ,
masterKeys : state . masterKeys ,
2017-12-14 21:39:13 +02:00
passwords : state . settings [ 'encryption.passwordCache' ] ,
encryptionEnabled : state . settings [ 'encryption.enabled' ] ,
activeMasterKeyId : state . settings [ 'encryption.activeMasterKeyId' ] ,
2018-02-01 21:01:20 +02:00
notLoadedMasterKeys : state . notLoadedMasterKeys ,
2017-12-14 20:53:08 +02:00
} ;
} ;
2017-12-21 21:06:08 +02:00
const EncryptionConfigScreen = connect ( mapStateToProps ) ( EncryptionConfigScreenComponent ) ;
2017-12-14 20:53:08 +02:00
2018-11-08 00:37:13 +02:00
module . exports = { EncryptionConfigScreen } ;