2017-12-14 20:53:08 +02:00
const React = require ( 'react' ) ;
const { connect } = require ( 'react-redux' ) ;
const MasterKeys = require ( 'lib/models/MasterKey' ) ;
2017-12-14 21:39:13 +02:00
const EncryptionService = require ( 'lib/services/EncryptionService' ) ;
2017-12-14 20:53:08 +02:00
const { Header } = require ( './Header.min.js' ) ;
const { themeStyle } = require ( '../theme.js' ) ;
const { _ } = require ( 'lib/locale.js' ) ;
const { time } = require ( 'lib/time-utils.js' ) ;
2017-12-21 21:06:08 +02:00
class EncryptionConfigScreenComponent extends React . Component {
2017-12-14 20:53:08 +02:00
2017-12-14 21:39:13 +02:00
constructor ( ) {
super ( ) ;
this . state = {
masterKeys : [ ] ,
passwords : { } ,
passwordChecks : { } ,
} ;
}
componentWillMount ( ) {
this . setState ( {
masterKeys : this . props . masterKeys ,
passwords : this . props . passwords ? this . props . passwords : { } ,
2017-12-14 22:21:36 +02:00
} , ( ) => {
this . checkPasswords ( ) ;
2017-12-14 21:39:13 +02:00
} ) ;
}
async checkPasswords ( ) {
const passwordChecks = Object . assign ( { } , this . state . passwordChecks ) ;
for ( let i = 0 ; i < this . state . masterKeys . length ; i ++ ) {
const mk = this . state . masterKeys [ i ] ;
const password = this . state . passwords [ mk . id ] ;
const ok = password ? await EncryptionService . instance ( ) . checkMasterKeyPassword ( mk , password ) : false ;
passwordChecks [ mk . id ] = ok ;
}
this . setState ( { passwordChecks : passwordChecks } ) ;
}
2017-12-14 20:53:08 +02:00
renderMasterKey ( mk ) {
2017-12-14 21:39:13 +02:00
const onSaveClick = ( ) => {
const password = this . state . passwords [ mk . id ] ;
if ( ! password ) {
2017-12-17 21:51:45 +02:00
Setting . deleteObjectKey ( 'encryption.passwordCache' , mk . id ) ;
2017-12-14 21:39:13 +02:00
} else {
2017-12-17 21:51:45 +02:00
Setting . setObjectKey ( 'encryption.passwordCache' , mk . id , password ) ;
2017-12-14 21:39:13 +02:00
}
2017-12-17 21:51:45 +02:00
// const cache = Setting.value('encryption.passwordCache');
// if (!cache) cache = {};
// if (!password) {
// delete cache[mk.id];
// } else {
// cache[mk.id] = password;
// }
// Setting.setValue('encryption.passwordCache', cache);
2017-12-14 21:39:13 +02:00
this . checkPasswords ( ) ;
}
const onPasswordChange = ( event ) => {
const passwords = this . state . passwords ;
passwords [ mk . id ] = event . target . value ;
this . setState ( { passwords : passwords } ) ;
}
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-14 21:39:13 +02:00
< td > { active } < / td >
2017-12-14 20:53:08 +02:00
< td > { mk . id } < / td >
< td > { mk . source _application } < / td >
< td > { time . formatMsToLocal ( mk . created _time ) } < / td >
2017-12-14 21:39:13 +02:00
< td > { time . formatMsToLocal ( mk . updated _time ) } < / td >
< td > < input type = "password" value = { password } onChange = { ( event ) => onPasswordChange ( event ) } / > < button onClick = { ( ) => onSaveClick ( ) } > { _ ( 'Save' ) } < / button > < / td >
< td > { passwordOk } < / td >
2017-12-14 20:53:08 +02:00
< / tr >
) ;
}
render ( ) {
const style = this . props . style ;
const theme = themeStyle ( this . props . theme ) ;
2017-12-14 21:39:13 +02:00
const masterKeys = this . state . masterKeys ;
2017-12-14 20:53:08 +02:00
const headerStyle = {
width : style . width ,
} ;
const mkComps = [ ] ;
for ( let i = 0 ; i < masterKeys . length ; i ++ ) {
const mk = masterKeys [ i ] ;
mkComps . push ( this . renderMasterKey ( mk ) ) ;
}
return (
< div >
< Header style = { headerStyle } / >
< table >
< tbody >
< tr >
2017-12-14 21:39:13 +02:00
< th > { _ ( 'Active' ) } < / th >
< th > { _ ( 'ID' ) } < / th >
< th > { _ ( 'Source' ) } < / th >
< th > { _ ( 'Created' ) } < / th >
< th > { _ ( 'Updated' ) } < / th >
< th > { _ ( 'Password' ) } < / th >
< th > { _ ( 'Password OK' ) } < / th >
2017-12-14 20:53:08 +02:00
< / tr >
{ mkComps }
< / tbody >
< / table >
2017-12-14 21:39:13 +02:00
{ _ ( '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.' ) }
2017-12-14 20:53:08 +02:00
< / div >
) ;
}
}
const mapStateToProps = ( state ) => {
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' ] ,
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
2017-12-21 21:06:08 +02:00
module . exports = { EncryptionConfigScreen } ;