element-web/src/components/views/settings/ChangePassword.js

289 lines
9.9 KiB
JavaScript
Raw Normal View History

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