2015-11-26 20:10:36 +03:00
|
|
|
/*
|
2016-01-07 07:06:39 +03:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2015-11-26 20:10:36 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var React = require('react');
|
|
|
|
var MatrixClientPeg = require("../../../MatrixClientPeg");
|
2017-01-25 00:36:55 +03:00
|
|
|
var Modal = require("../../../Modal");
|
2015-12-23 18:38:28 +03:00
|
|
|
var sdk = require("../../../index");
|
2017-01-25 01:41:52 +03:00
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
2015-11-26 20:10:36 +03:00
|
|
|
|
2017-05-12 17:58:44 +03:00
|
|
|
import sessionStore from '../../../stores/SessionStore';
|
2017-05-12 14:02:45 +03:00
|
|
|
|
2015-11-26 20:10:36 +03:00
|
|
|
module.exports = React.createClass({
|
|
|
|
displayName: 'ChangePassword',
|
|
|
|
propTypes: {
|
|
|
|
onFinished: React.PropTypes.func,
|
2015-12-23 18:38:28 +03:00
|
|
|
onError: React.PropTypes.func,
|
|
|
|
onCheckPassword: React.PropTypes.func,
|
|
|
|
rowClassName: React.PropTypes.string,
|
|
|
|
rowLabelClassName: React.PropTypes.string,
|
|
|
|
rowInputClassName: React.PropTypes.string,
|
2017-05-11 19:47:45 +03:00
|
|
|
buttonClassName: React.PropTypes.string,
|
2015-11-26 20:10:36 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
Phases: {
|
|
|
|
Edit: "edit",
|
|
|
|
Uploading: "uploading",
|
2015-12-23 18:38:28 +03:00
|
|
|
Error: "error"
|
2015-11-26 20:10:36 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
return {
|
|
|
|
onFinished: function() {},
|
2015-12-23 18:38:28 +03:00
|
|
|
onError: function() {},
|
|
|
|
onCheckPassword: function(oldPass, newPass, confirmPass) {
|
|
|
|
if (newPass !== confirmPass) {
|
|
|
|
return {
|
|
|
|
error: "New passwords don't match."
|
|
|
|
};
|
|
|
|
} else if (!newPass || newPass.length === 0) {
|
|
|
|
return {
|
|
|
|
error: "Passwords can't be empty"
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2015-11-26 20:10:36 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
return {
|
2017-05-12 14:02:45 +03:00
|
|
|
phase: this.Phases.Edit,
|
|
|
|
cachedPassword: null,
|
2017-01-20 17:22:27 +03:00
|
|
|
};
|
2015-11-26 20:10:36 +03:00
|
|
|
},
|
|
|
|
|
2017-05-12 14:02:45 +03:00
|
|
|
componentWillMount: function() {
|
2017-05-15 16:52:19 +03:00
|
|
|
this._sessionStore = sessionStore;
|
2017-05-16 13:52:51 +03:00
|
|
|
this._sessionStoreToken = this._sessionStore.addListener(
|
2017-05-15 19:17:32 +03:00
|
|
|
this._setStateFromSessionStore,
|
2017-05-16 13:52:51 +03:00
|
|
|
);
|
2017-05-12 14:02:45 +03:00
|
|
|
|
2017-05-15 16:52:19 +03:00
|
|
|
this._setStateFromSessionStore();
|
2017-05-12 14:02:45 +03:00
|
|
|
},
|
|
|
|
|
2017-05-15 19:17:32 +03:00
|
|
|
componentWillUnmount: function() {
|
2017-05-16 13:52:51 +03:00
|
|
|
if (this._sessionStoreToken) {
|
|
|
|
this._sessionStoreToken.remove();
|
2017-05-15 19:17:32 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-05-15 16:52:19 +03:00
|
|
|
_setStateFromSessionStore: function() {
|
2017-05-12 14:02:45 +03:00
|
|
|
this.setState({
|
2017-05-15 16:52:19 +03:00
|
|
|
cachedPassword: this._sessionStore.getCachedPassword(),
|
2017-05-12 14:02:45 +03:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2015-11-26 20:10:36 +03:00
|
|
|
changePassword: function(old_password, new_password) {
|
|
|
|
var cli = MatrixClientPeg.get();
|
|
|
|
|
2017-01-25 00:36:55 +03:00
|
|
|
var QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
|
|
|
|
Modal.createDialog(QuestionDialog, {
|
|
|
|
title: "Warning",
|
|
|
|
description:
|
|
|
|
<div>
|
|
|
|
Changing password will currently reset any end-to-end encryption keys on all devices,
|
2017-04-08 01:34:11 +03:00
|
|
|
making encrypted chat history unreadable, unless you first export your room keys
|
|
|
|
and re-import them afterwards.
|
|
|
|
In future this <a href="https://github.com/vector-im/riot-web/issues/2671">will be improved</a>.
|
2017-01-25 00:36:55 +03:00
|
|
|
</div>,
|
|
|
|
button: "Continue",
|
2017-04-08 01:34:11 +03:00
|
|
|
extraButtons: [
|
|
|
|
<button className="mx_Dialog_primary"
|
|
|
|
onClick={this._onExportE2eKeysClicked}>
|
|
|
|
Export E2E room keys
|
|
|
|
</button>
|
|
|
|
],
|
2017-01-25 00:36:55 +03:00
|
|
|
onFinished: (confirmed) => {
|
|
|
|
if (confirmed) {
|
|
|
|
var authDict = {
|
|
|
|
type: 'm.login.password',
|
|
|
|
user: cli.credentials.userId,
|
|
|
|
password: old_password
|
|
|
|
};
|
2015-11-26 20:10:36 +03:00
|
|
|
|
2017-01-25 00:36:55 +03:00
|
|
|
this.setState({
|
|
|
|
phase: this.Phases.Uploading
|
|
|
|
});
|
2015-11-26 20:10:36 +03:00
|
|
|
|
2017-01-25 00:36:55 +03:00
|
|
|
var self = this;
|
|
|
|
cli.setPassword(authDict, new_password).then(function() {
|
|
|
|
self.props.onFinished();
|
|
|
|
}, function(err) {
|
|
|
|
self.props.onError(err);
|
|
|
|
}).finally(function() {
|
|
|
|
self.setState({
|
|
|
|
phase: self.Phases.Edit
|
|
|
|
});
|
|
|
|
}).done();
|
|
|
|
}
|
|
|
|
},
|
2015-12-23 18:38:28 +03:00
|
|
|
});
|
2015-11-26 20:10:36 +03:00
|
|
|
},
|
|
|
|
|
2017-04-08 01:34:11 +03:00
|
|
|
_onExportE2eKeysClicked: function() {
|
|
|
|
Modal.createDialogAsync(
|
|
|
|
(cb) => {
|
|
|
|
require.ensure(['../../../async-components/views/dialogs/ExportE2eKeysDialog'], () => {
|
|
|
|
cb(require('../../../async-components/views/dialogs/ExportE2eKeysDialog'));
|
|
|
|
}, "e2e-export");
|
|
|
|
}, {
|
|
|
|
matrixClient: MatrixClientPeg.get(),
|
|
|
|
}
|
|
|
|
);
|
2017-05-11 19:47:45 +03:00
|
|
|
},
|
2017-04-08 01:34:11 +03:00
|
|
|
|
2015-11-26 20:10:36 +03:00
|
|
|
onClickChange: function() {
|
2017-05-12 14:02:45 +03:00
|
|
|
var old_password = this.state.cachedPassword || this.refs.old_input.value;
|
2015-11-26 20:10:36 +03:00
|
|
|
var new_password = this.refs.new_input.value;
|
|
|
|
var confirm_password = this.refs.confirm_input.value;
|
2015-12-23 18:38:28 +03:00
|
|
|
var err = this.props.onCheckPassword(
|
|
|
|
old_password, new_password, confirm_password
|
|
|
|
);
|
|
|
|
if (err) {
|
|
|
|
this.props.onError(err);
|
|
|
|
}
|
|
|
|
else {
|
2015-11-26 20:10:36 +03:00
|
|
|
this.changePassword(old_password, new_password);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function() {
|
2017-05-11 19:47:45 +03:00
|
|
|
const rowClassName = this.props.rowClassName;
|
|
|
|
const rowLabelClassName = this.props.rowLabelClassName;
|
|
|
|
const rowInputClassName = this.props.rowInputClassName;
|
|
|
|
const buttonClassName = this.props.buttonClassName;
|
|
|
|
|
|
|
|
let currentPassword = null;
|
2017-05-12 14:02:45 +03:00
|
|
|
if (!this.state.cachedPassword) {
|
2017-05-11 19:47:45 +03:00
|
|
|
currentPassword = <div className={rowClassName}>
|
|
|
|
<div className={rowLabelClassName}>
|
|
|
|
<label htmlFor="passwordold">Current password</label>
|
|
|
|
</div>
|
|
|
|
<div className={rowInputClassName}>
|
|
|
|
<input id="passwordold" type="password" ref="old_input" />
|
|
|
|
</div>
|
|
|
|
</div>;
|
|
|
|
}
|
2015-12-23 18:38:28 +03:00
|
|
|
|
2015-11-26 20:10:36 +03:00
|
|
|
switch (this.state.phase) {
|
|
|
|
case this.Phases.Edit:
|
|
|
|
return (
|
2015-12-23 18:38:28 +03:00
|
|
|
<div className={this.props.className}>
|
2017-05-11 19:47:45 +03:00
|
|
|
{ currentPassword }
|
2015-12-23 18:38:28 +03:00
|
|
|
<div className={rowClassName}>
|
|
|
|
<div className={rowLabelClassName}>
|
|
|
|
<label htmlFor="password1">New password</label>
|
|
|
|
</div>
|
|
|
|
<div className={rowInputClassName}>
|
|
|
|
<input id="password1" type="password" ref="new_input" />
|
|
|
|
</div>
|
2015-11-26 20:10:36 +03:00
|
|
|
</div>
|
2015-12-23 18:38:28 +03:00
|
|
|
<div className={rowClassName}>
|
|
|
|
<div className={rowLabelClassName}>
|
|
|
|
<label htmlFor="password2">Confirm password</label>
|
|
|
|
</div>
|
|
|
|
<div className={rowInputClassName}>
|
|
|
|
<input id="password2" type="password" ref="confirm_input" />
|
|
|
|
</div>
|
|
|
|
</div>
|
2017-01-25 01:41:52 +03:00
|
|
|
<AccessibleButton className={buttonClassName}
|
|
|
|
onClick={this.onClickChange}>
|
2015-12-23 18:38:28 +03:00
|
|
|
Change Password
|
2017-01-13 19:25:26 +03:00
|
|
|
</AccessibleButton>
|
2015-11-26 20:10:36 +03:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
case this.Phases.Uploading:
|
|
|
|
var Loader = sdk.getComponent("elements.Spinner");
|
|
|
|
return (
|
|
|
|
<div className="mx_Dialog_content">
|
|
|
|
<Loader />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|