2015-11-26 17:10:36 +00:00
|
|
|
/*
|
2016-01-07 04:06:39 +00:00
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2019-01-22 13:28:33 -07:00
|
|
|
Copyright 2018-2019 New Vector Ltd
|
2015-11-26 17:10:36 +00: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.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-22 13:09:40 -07:00
|
|
|
import Field from "../elements/Field";
|
2019-09-06 18:37:43 +01:00
|
|
|
import React from 'react';
|
2017-12-26 14:03:18 +13:00
|
|
|
import PropTypes from 'prop-types';
|
2019-09-06 18:37:43 +01:00
|
|
|
import createReactClass from 'create-react-class';
|
2019-12-20 14:13:46 -07:00
|
|
|
import {MatrixClientPeg} from "../../../MatrixClientPeg";
|
2020-05-13 20:41:41 -06:00
|
|
|
import dis from "../../../dispatcher/dispatcher";
|
2017-01-24 22:41:52 +00:00
|
|
|
import AccessibleButton from '../elements/AccessibleButton';
|
2017-05-25 11:39:08 +01:00
|
|
|
import { _t } from '../../../languageHandler';
|
2019-12-20 14:41:07 -07:00
|
|
|
import * as sdk from "../../../index";
|
|
|
|
|
import Modal from "../../../Modal";
|
2015-11-26 17:10:36 +00:00
|
|
|
|
2017-05-12 15:58:44 +01:00
|
|
|
import sessionStore from '../../../stores/SessionStore';
|
2017-05-12 12:02:45 +01:00
|
|
|
|
2019-12-19 17:45:24 -07:00
|
|
|
export default createReactClass({
|
2015-11-26 17:10:36 +00:00
|
|
|
displayName: 'ChangePassword',
|
2019-03-12 16:17:21 +00:00
|
|
|
|
2015-11-26 17:10:36 +00:00
|
|
|
propTypes: {
|
2017-12-26 14:03:18 +13:00
|
|
|
onFinished: PropTypes.func,
|
|
|
|
|
onError: PropTypes.func,
|
|
|
|
|
onCheckPassword: PropTypes.func,
|
|
|
|
|
rowClassName: PropTypes.string,
|
|
|
|
|
buttonClassName: PropTypes.string,
|
2019-01-22 13:09:40 -07:00
|
|
|
buttonKind: PropTypes.string,
|
2017-12-26 14:03:18 +13:00
|
|
|
confirm: PropTypes.bool,
|
2017-05-22 14:46:49 +01:00
|
|
|
// Whether to autoFocus the new password input
|
2017-12-26 14:03:18 +13:00
|
|
|
autoFocusNewPasswordInput: PropTypes.bool,
|
2015-11-26 17:10:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
Phases: {
|
|
|
|
|
Edit: "edit",
|
|
|
|
|
Uploading: "uploading",
|
2017-10-11 17:56:17 +01:00
|
|
|
Error: "error",
|
2015-11-26 17:10:36 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getDefaultProps: function() {
|
|
|
|
|
return {
|
|
|
|
|
onFinished: function() {},
|
2015-12-23 15:38:28 +00:00
|
|
|
onError: function() {},
|
|
|
|
|
onCheckPassword: function(oldPass, newPass, confirmPass) {
|
|
|
|
|
if (newPass !== confirmPass) {
|
|
|
|
|
return {
|
2017-10-11 17:56:17 +01:00
|
|
|
error: _t("New passwords don't match"),
|
2015-12-23 15:38:28 +00:00
|
|
|
};
|
|
|
|
|
} else if (!newPass || newPass.length === 0) {
|
|
|
|
|
return {
|
2017-10-11 17:56:17 +01:00
|
|
|
error: _t("Passwords can't be empty"),
|
2015-12-23 15:38:28 +00:00
|
|
|
};
|
|
|
|
|
}
|
2017-05-16 12:45:14 +01:00
|
|
|
},
|
|
|
|
|
confirm: true,
|
2015-11-26 17:10:36 +00:00
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
getInitialState: function() {
|
|
|
|
|
return {
|
2017-05-12 12:02:45 +01:00
|
|
|
phase: this.Phases.Edit,
|
|
|
|
|
cachedPassword: null,
|
2019-03-12 16:17:21 +00:00
|
|
|
oldPassword: "",
|
|
|
|
|
newPassword: "",
|
|
|
|
|
newPasswordConfirm: "",
|
2017-01-20 14:22:27 +00:00
|
|
|
};
|
2015-11-26 17:10:36 +00:00
|
|
|
},
|
|
|
|
|
|
2020-03-31 14:14:17 -06:00
|
|
|
componentDidMount: function() {
|
2017-05-15 14:52:19 +01:00
|
|
|
this._sessionStore = sessionStore;
|
2017-05-16 11:52:51 +01:00
|
|
|
this._sessionStoreToken = this._sessionStore.addListener(
|
2017-05-15 17:17:32 +01:00
|
|
|
this._setStateFromSessionStore,
|
2017-05-16 11:52:51 +01:00
|
|
|
);
|
2015-11-26 17:10:36 +00:00
|
|
|
|
2017-05-15 14:52:19 +01:00
|
|
|
this._setStateFromSessionStore();
|
2017-05-12 12:02:45 +01:00
|
|
|
},
|
|
|
|
|
|
2017-05-15 17:17:32 +01:00
|
|
|
componentWillUnmount: function() {
|
2017-05-16 11:52:51 +01:00
|
|
|
if (this._sessionStoreToken) {
|
|
|
|
|
this._sessionStoreToken.remove();
|
2017-05-15 17:17:32 +01:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2017-05-15 14:52:19 +01:00
|
|
|
_setStateFromSessionStore: function() {
|
2017-05-12 12:02:45 +01:00
|
|
|
this.setState({
|
2017-05-15 14:52:19 +01:00
|
|
|
cachedPassword: this._sessionStore.getCachedPassword(),
|
2017-05-12 12:02:45 +01:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2017-05-16 14:13:22 +01:00
|
|
|
changePassword: function(oldPassword, newPassword) {
|
2017-05-16 11:45:01 +01:00
|
|
|
const cli = MatrixClientPeg.get();
|
2015-11-26 17:10:36 +00:00
|
|
|
|
2017-05-16 12:45:14 +01:00
|
|
|
if (!this.props.confirm) {
|
2017-05-16 11:45:01 +01:00
|
|
|
this._changePassword(cli, oldPassword, newPassword);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QuestionDialog = sdk.getComponent("dialogs.QuestionDialog");
|
2017-07-27 17:19:18 +01:00
|
|
|
Modal.createTrackedDialog('Change Password', '', QuestionDialog, {
|
2017-05-25 18:20:48 +01:00
|
|
|
title: _t("Warning!"),
|
2017-01-24 22:36:55 +01:00
|
|
|
description:
|
|
|
|
|
<div>
|
2017-05-23 15:16:31 +01:00
|
|
|
{ _t(
|
2020-01-29 15:48:25 +00:00
|
|
|
'Changing password will currently reset any end-to-end encryption keys on all sessions, ' +
|
2017-05-23 15:16:31 +01:00
|
|
|
'making encrypted chat history unreadable, unless you first export your room keys ' +
|
|
|
|
|
'and re-import them afterwards. ' +
|
2017-10-11 17:56:17 +01:00
|
|
|
'In future this will be improved.',
|
2019-01-24 16:19:18 +00:00
|
|
|
) }
|
|
|
|
|
{' '}
|
2020-02-23 22:14:29 +00:00
|
|
|
<a href="https://github.com/vector-im/riot-web/issues/2671" target="_blank" rel="noreferrer noopener">
|
2019-01-24 16:19:18 +00:00
|
|
|
https://github.com/vector-im/riot-web/issues/2671
|
|
|
|
|
</a>
|
2017-01-24 22:36:55 +01:00
|
|
|
</div>,
|
2017-05-23 15:16:31 +01:00
|
|
|
button: _t("Continue"),
|
2017-04-07 23:34:11 +01:00
|
|
|
extraButtons: [
|
|
|
|
|
<button className="mx_Dialog_primary"
|
|
|
|
|
onClick={this._onExportE2eKeysClicked}>
|
2017-05-23 15:16:31 +01:00
|
|
|
{ _t('Export E2E room keys') }
|
2017-10-11 17:56:17 +01:00
|
|
|
</button>,
|
2017-04-07 23:34:11 +01:00
|
|
|
],
|
2017-01-24 22:36:55 +01:00
|
|
|
onFinished: (confirmed) => {
|
|
|
|
|
if (confirmed) {
|
2017-05-16 11:45:01 +01:00
|
|
|
this._changePassword(cli, oldPassword, newPassword);
|
2017-01-24 22:36:55 +01:00
|
|
|
}
|
|
|
|
|
},
|
2015-12-23 15:38:28 +00:00
|
|
|
});
|
2015-11-26 17:10:36 +00:00
|
|
|
},
|
|
|
|
|
|
2017-05-16 11:45:01 +01:00
|
|
|
_changePassword: function(cli, oldPassword, newPassword) {
|
|
|
|
|
const authDict = {
|
|
|
|
|
type: 'm.login.password',
|
2020-05-29 08:23:59 -06:00
|
|
|
identifier: {
|
|
|
|
|
type: 'm.id.user',
|
|
|
|
|
user: cli.credentials.userId,
|
|
|
|
|
},
|
|
|
|
|
// TODO: Remove `user` once servers support proper UIA
|
|
|
|
|
// See https://github.com/matrix-org/synapse/issues/5665
|
2017-05-16 11:45:01 +01:00
|
|
|
user: cli.credentials.userId,
|
|
|
|
|
password: oldPassword,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
|
phase: this.Phases.Uploading,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
cli.setPassword(authDict, newPassword).then(() => {
|
2018-06-18 18:14:48 +01:00
|
|
|
// Notify SessionStore that the user's password was changed
|
|
|
|
|
dis.dispatch({action: 'password_changed'});
|
|
|
|
|
|
2017-06-14 09:31:16 +01:00
|
|
|
if (this.props.shouldAskForEmail) {
|
|
|
|
|
return this._optionallySetEmail().then((confirmed) => {
|
|
|
|
|
this.props.onFinished({
|
|
|
|
|
didSetEmail: confirmed,
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.props.onFinished();
|
|
|
|
|
}
|
2017-05-16 11:45:01 +01:00
|
|
|
}, (err) => {
|
|
|
|
|
this.props.onError(err);
|
|
|
|
|
}).finally(() => {
|
|
|
|
|
this.setState({
|
|
|
|
|
phase: this.Phases.Edit,
|
2019-03-12 16:17:21 +00:00
|
|
|
oldPassword: "",
|
|
|
|
|
newPassword: "",
|
|
|
|
|
newPasswordConfirm: "",
|
2017-05-16 11:45:01 +01:00
|
|
|
});
|
2019-11-18 10:03:05 +00:00
|
|
|
});
|
2017-05-16 11:45:01 +01:00
|
|
|
},
|
|
|
|
|
|
2017-06-14 09:31:16 +01:00
|
|
|
_optionallySetEmail: function() {
|
|
|
|
|
// Ask for an email otherwise the user has no way to reset their password
|
|
|
|
|
const SetEmailDialog = sdk.getComponent("dialogs.SetEmailDialog");
|
2019-11-12 11:40:38 +00:00
|
|
|
const modal = Modal.createTrackedDialog('Do you want to set an email address?', '', SetEmailDialog, {
|
2017-06-14 09:31:16 +01:00
|
|
|
title: _t('Do you want to set an email address?'),
|
|
|
|
|
});
|
2019-11-12 11:40:38 +00:00
|
|
|
return modal.finished.then(([confirmed]) => confirmed);
|
2017-06-14 09:31:16 +01:00
|
|
|
},
|
|
|
|
|
|
2017-04-07 23:34:11 +01:00
|
|
|
_onExportE2eKeysClicked: function() {
|
2018-11-21 16:56:44 +00:00
|
|
|
Modal.createTrackedDialogAsync('Export E2E Keys', 'Change Password',
|
|
|
|
|
import('../../../async-components/views/dialogs/ExportE2eKeysDialog'),
|
|
|
|
|
{
|
|
|
|
|
matrixClient: MatrixClientPeg.get(),
|
|
|
|
|
},
|
|
|
|
|
);
|
2017-05-16 11:45:01 +01:00
|
|
|
},
|
2017-04-07 23:34:11 +01:00
|
|
|
|
2019-03-12 16:17:21 +00:00
|
|
|
onChangeOldPassword(ev) {
|
|
|
|
|
this.setState({
|
|
|
|
|
oldPassword: ev.target.value,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onChangeNewPassword(ev) {
|
|
|
|
|
this.setState({
|
|
|
|
|
newPassword: ev.target.value,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onChangeNewPasswordConfirm(ev) {
|
|
|
|
|
this.setState({
|
|
|
|
|
newPasswordConfirm: ev.target.value,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
2017-10-30 16:28:27 +00:00
|
|
|
onClickChange: function(ev) {
|
|
|
|
|
ev.preventDefault();
|
2019-03-12 16:17:21 +00:00
|
|
|
const oldPassword = this.state.cachedPassword || this.state.oldPassword;
|
|
|
|
|
const newPassword = this.state.newPassword;
|
|
|
|
|
const confirmPassword = this.state.newPasswordConfirm;
|
2017-05-16 11:45:01 +01:00
|
|
|
const err = this.props.onCheckPassword(
|
|
|
|
|
oldPassword, newPassword, confirmPassword,
|
2015-12-23 15:38:28 +00:00
|
|
|
);
|
|
|
|
|
if (err) {
|
|
|
|
|
this.props.onError(err);
|
2017-05-16 11:45:01 +01:00
|
|
|
} else {
|
|
|
|
|
this.changePassword(oldPassword, newPassword);
|
2015-11-26 17:10:36 +00:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
render: function() {
|
2019-01-22 13:09:40 -07:00
|
|
|
// TODO: Live validation on `new pw == confirm pw`
|
|
|
|
|
|
2017-05-11 17:47:45 +01:00
|
|
|
const rowClassName = this.props.rowClassName;
|
|
|
|
|
const buttonClassName = this.props.buttonClassName;
|
|
|
|
|
|
|
|
|
|
let currentPassword = null;
|
2017-05-12 12:02:45 +01:00
|
|
|
if (!this.state.cachedPassword) {
|
2019-01-22 13:09:40 -07:00
|
|
|
currentPassword = (
|
|
|
|
|
<div className={rowClassName}>
|
2020-03-29 22:59:15 +01:00
|
|
|
<Field
|
2019-03-12 16:17:21 +00:00
|
|
|
type="password"
|
|
|
|
|
label={_t('Current password')}
|
|
|
|
|
value={this.state.oldPassword}
|
|
|
|
|
onChange={this.onChangeOldPassword}
|
|
|
|
|
/>
|
2017-05-11 17:47:45 +01:00
|
|
|
</div>
|
2019-01-22 13:09:40 -07:00
|
|
|
);
|
2017-05-11 17:47:45 +01:00
|
|
|
}
|
2015-12-23 15:38:28 +00:00
|
|
|
|
2015-11-26 17:10:36 +00:00
|
|
|
switch (this.state.phase) {
|
|
|
|
|
case this.Phases.Edit:
|
2017-05-25 15:27:54 +01:00
|
|
|
const passwordLabel = this.state.cachedPassword ?
|
2017-05-28 22:58:18 +01:00
|
|
|
_t('Password') : _t('New Password');
|
2015-11-26 17:10:36 +00:00
|
|
|
return (
|
2017-06-14 14:50:48 +01:00
|
|
|
<form className={this.props.className} onSubmit={this.onClickChange}>
|
2017-05-11 17:47:45 +01:00
|
|
|
{ currentPassword }
|
2015-12-23 15:38:28 +00:00
|
|
|
<div className={rowClassName}>
|
2020-01-29 13:38:50 +00:00
|
|
|
<Field
|
2019-03-12 16:17:21 +00:00
|
|
|
type="password"
|
|
|
|
|
label={passwordLabel}
|
|
|
|
|
value={this.state.newPassword}
|
|
|
|
|
autoFocus={this.props.autoFocusNewPasswordInput}
|
|
|
|
|
onChange={this.onChangeNewPassword}
|
2020-01-29 13:38:50 +00:00
|
|
|
autoComplete="new-password"
|
2019-03-12 16:17:21 +00:00
|
|
|
/>
|
2015-12-23 15:38:28 +00:00
|
|
|
</div>
|
|
|
|
|
<div className={rowClassName}>
|
2020-01-29 13:38:50 +00:00
|
|
|
<Field
|
2019-03-12 16:17:21 +00:00
|
|
|
type="password"
|
|
|
|
|
label={_t("Confirm password")}
|
|
|
|
|
value={this.state.newPasswordConfirm}
|
|
|
|
|
onChange={this.onChangeNewPasswordConfirm}
|
2020-01-29 13:38:50 +00:00
|
|
|
autoComplete="new-password"
|
2019-03-12 16:17:21 +00:00
|
|
|
/>
|
2015-12-23 15:38:28 +00:00
|
|
|
</div>
|
2019-01-22 13:09:40 -07:00
|
|
|
<AccessibleButton className={buttonClassName} kind={this.props.buttonKind} onClick={this.onClickChange}>
|
2017-05-23 15:16:31 +01:00
|
|
|
{ _t('Change Password') }
|
2017-01-13 18:25:26 +02:00
|
|
|
</AccessibleButton>
|
2017-06-14 14:50:48 +01:00
|
|
|
</form>
|
2015-11-26 17:10:36 +00:00
|
|
|
);
|
|
|
|
|
case this.Phases.Uploading:
|
|
|
|
|
var Loader = sdk.getComponent("elements.Spinner");
|
|
|
|
|
return (
|
|
|
|
|
<div className="mx_Dialog_content">
|
|
|
|
|
<Loader />
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-10-11 17:56:17 +01:00
|
|
|
},
|
2015-11-26 17:10:36 +00:00
|
|
|
});
|