2017-01-18 14:39:44 +03:00
|
|
|
/*
|
|
|
|
Copyright 2017 Vector Creations Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-01-20 18:12:50 +03:00
|
|
|
import FileSaver from 'file-saver';
|
2019-12-08 15:16:17 +03:00
|
|
|
import React, {createRef} from 'react';
|
2017-12-26 04:03:18 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2017-05-30 17:09:57 +03:00
|
|
|
import { _t } from '../../../languageHandler';
|
2017-01-18 14:39:44 +03:00
|
|
|
|
2019-08-06 16:04:33 +03:00
|
|
|
import { MatrixClient } from 'matrix-js-sdk';
|
2017-01-20 18:12:50 +03:00
|
|
|
import * as MegolmExportEncryption from '../../../utils/MegolmExportEncryption';
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as sdk from '../../../index';
|
2017-01-18 14:39:44 +03:00
|
|
|
|
2017-01-20 18:12:50 +03:00
|
|
|
const PHASE_EDIT = 1;
|
|
|
|
const PHASE_EXPORTING = 2;
|
2017-01-18 14:39:44 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
export default class ExportE2eKeysDialog extends React.Component {
|
|
|
|
static propTypes = {
|
2019-08-06 16:04:33 +03:00
|
|
|
matrixClient: PropTypes.instanceOf(MatrixClient).isRequired,
|
2017-12-26 04:03:18 +03:00
|
|
|
onFinished: PropTypes.func.isRequired,
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-01-20 18:12:50 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2017-01-18 14:39:44 +03:00
|
|
|
|
2017-01-20 18:12:50 +03:00
|
|
|
this._unmounted = false;
|
2019-12-08 15:16:17 +03:00
|
|
|
|
|
|
|
this._passphrase1 = createRef();
|
|
|
|
this._passphrase2 = createRef();
|
2017-01-20 18:12:50 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
this.state = {
|
|
|
|
phase: PHASE_EDIT,
|
|
|
|
errStr: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2017-01-20 18:12:50 +03:00
|
|
|
this._unmounted = true;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-01-20 18:12:50 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_onPassphraseFormSubmit = (ev) => {
|
2017-01-18 14:39:44 +03:00
|
|
|
ev.preventDefault();
|
2017-01-20 18:12:50 +03:00
|
|
|
|
2019-12-08 15:16:17 +03:00
|
|
|
const passphrase = this._passphrase1.current.value;
|
|
|
|
if (passphrase !== this._passphrase2.current.value) {
|
2017-05-30 17:09:57 +03:00
|
|
|
this.setState({errStr: _t('Passphrases must match')});
|
2017-01-20 18:12:50 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!passphrase) {
|
2017-05-30 17:09:57 +03:00
|
|
|
this.setState({errStr: _t('Passphrase must not be empty')});
|
2017-01-20 18:12:50 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._startExport(passphrase);
|
2017-01-18 14:39:44 +03:00
|
|
|
return false;
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-01-18 14:39:44 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_startExport(passphrase) {
|
2017-01-20 18:12:50 +03:00
|
|
|
// extra Promise.resolve() to turn synchronous exceptions into
|
|
|
|
// asynchronous ones.
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
return this.props.matrixClient.exportRoomKeys();
|
|
|
|
}).then((k) => {
|
|
|
|
return MegolmExportEncryption.encryptMegolmKeyFile(
|
2017-01-26 01:15:09 +03:00
|
|
|
JSON.stringify(k), passphrase,
|
2017-01-18 14:39:44 +03:00
|
|
|
);
|
2017-01-20 18:12:50 +03:00
|
|
|
}).then((f) => {
|
|
|
|
const blob = new Blob([f], {
|
|
|
|
type: 'text/plain;charset=us-ascii',
|
|
|
|
});
|
2020-08-03 18:02:26 +03:00
|
|
|
FileSaver.saveAs(blob, 'element-keys.txt');
|
2017-01-20 18:12:50 +03:00
|
|
|
this.props.onFinished(true);
|
|
|
|
}).catch((e) => {
|
2017-06-08 16:21:25 +03:00
|
|
|
console.error("Error exporting e2e keys:", e);
|
2017-01-20 18:12:50 +03:00
|
|
|
if (this._unmounted) {
|
|
|
|
return;
|
|
|
|
}
|
2017-06-08 16:21:25 +03:00
|
|
|
const msg = e.friendlyText || _t('Unknown error');
|
2017-01-20 18:12:50 +03:00
|
|
|
this.setState({
|
2017-06-08 16:21:25 +03:00
|
|
|
errStr: msg,
|
2017-01-20 18:12:50 +03:00
|
|
|
phase: PHASE_EDIT,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
errStr: null,
|
|
|
|
phase: PHASE_EXPORTING,
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2017-01-20 18:12:50 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
_onCancelClick = (ev) => {
|
2017-01-26 01:15:09 +03:00
|
|
|
ev.preventDefault();
|
|
|
|
this.props.onFinished(false);
|
|
|
|
return false;
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2017-01-26 01:15:09 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
render() {
|
2017-01-20 18:12:50 +03:00
|
|
|
const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
|
|
|
|
|
|
|
|
const disableForm = (this.state.phase === PHASE_EXPORTING);
|
2017-01-18 14:39:44 +03:00
|
|
|
|
|
|
|
return (
|
2017-01-20 18:12:50 +03:00
|
|
|
<BaseDialog className='mx_exportE2eKeysDialog'
|
|
|
|
onFinished={this.props.onFinished}
|
2017-05-30 17:09:57 +03:00
|
|
|
title={_t("Export room keys")}
|
2017-01-20 18:12:50 +03:00
|
|
|
>
|
|
|
|
<form onSubmit={this._onPassphraseFormSubmit}>
|
|
|
|
<div className="mx_Dialog_content">
|
|
|
|
<p>
|
2017-05-30 17:09:57 +03:00
|
|
|
{ _t(
|
|
|
|
'This process allows you to export the keys for messages ' +
|
|
|
|
'you have received in encrypted rooms to a local file. You ' +
|
|
|
|
'will then be able to import the file into another Matrix ' +
|
|
|
|
'client in the future, so that client will also be able to ' +
|
2017-06-08 15:12:27 +03:00
|
|
|
'decrypt these messages.',
|
2017-05-30 17:09:57 +03:00
|
|
|
) }
|
2017-01-20 18:12:50 +03:00
|
|
|
</p>
|
|
|
|
<p>
|
2017-05-30 17:09:57 +03:00
|
|
|
{ _t(
|
|
|
|
'The exported file will allow anyone who can read it to decrypt ' +
|
|
|
|
'any encrypted messages that you can see, so you should be ' +
|
|
|
|
'careful to keep it secure. To help with this, you should enter ' +
|
|
|
|
'a passphrase below, which will be used to encrypt the exported ' +
|
|
|
|
'data. It will only be possible to import the data by using the ' +
|
2017-06-08 15:12:27 +03:00
|
|
|
'same passphrase.',
|
2017-05-30 17:09:57 +03:00
|
|
|
) }
|
2017-01-20 18:12:50 +03:00
|
|
|
</p>
|
|
|
|
<div className='error'>
|
2017-09-28 13:21:06 +03:00
|
|
|
{ this.state.errStr }
|
2017-01-20 18:12:50 +03:00
|
|
|
</div>
|
|
|
|
<div className='mx_E2eKeysDialog_inputTable'>
|
|
|
|
<div className='mx_E2eKeysDialog_inputRow'>
|
|
|
|
<div className='mx_E2eKeysDialog_inputLabel'>
|
|
|
|
<label htmlFor='passphrase1'>
|
2017-09-28 13:21:06 +03:00
|
|
|
{ _t("Enter passphrase") }
|
2017-01-20 18:12:50 +03:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<div className='mx_E2eKeysDialog_inputCell'>
|
2019-12-08 15:16:17 +03:00
|
|
|
<input ref={this._passphrase1} id='passphrase1'
|
2017-01-20 18:12:50 +03:00
|
|
|
autoFocus={true} size='64' type='password'
|
|
|
|
disabled={disableForm}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className='mx_E2eKeysDialog_inputRow'>
|
|
|
|
<div className='mx_E2eKeysDialog_inputLabel'>
|
|
|
|
<label htmlFor='passphrase2'>
|
2017-09-28 13:21:06 +03:00
|
|
|
{ _t("Confirm passphrase") }
|
2017-01-20 18:12:50 +03:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
<div className='mx_E2eKeysDialog_inputCell'>
|
2019-12-08 15:16:17 +03:00
|
|
|
<input ref={this._passphrase2} id='passphrase2'
|
2017-01-20 18:12:50 +03:00
|
|
|
size='64' type='password'
|
|
|
|
disabled={disableForm}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div className='mx_Dialog_buttons'>
|
2017-06-01 14:09:07 +03:00
|
|
|
<input className='mx_Dialog_primary' type='submit' value={_t('Export')}
|
2017-01-20 18:12:50 +03:00
|
|
|
disabled={disableForm}
|
|
|
|
/>
|
2017-01-26 01:15:09 +03:00
|
|
|
<button onClick={this._onCancelClick} disabled={disableForm}>
|
2017-09-28 13:21:06 +03:00
|
|
|
{ _t("Cancel") }
|
2017-01-26 01:15:09 +03:00
|
|
|
</button>
|
2017-01-20 18:12:50 +03:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</BaseDialog>
|
2017-01-18 14:39:44 +03:00
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|