session-desktop/ts/components/session/SessionPasswordModal.tsx

324 lines
9 KiB
TypeScript
Raw Normal View History

import React from 'react';
import { SessionModal } from './SessionModal';
2020-01-20 04:36:46 +01:00
import { SessionButton, SessionButtonColor } from './SessionButton';
import { missingCaseError, PasswordUtil } from '../../util/';
import { ToastUtils } from '../../session/utils';
import { SessionIconType } from './icon';
import { DefaultTheme, withTheme } from 'styled-components';
2021-02-15 05:16:38 +01:00
import { getPasswordHash } from '../../data/data';
export enum PasswordAction {
Set = 'set',
Change = 'change',
2020-01-20 04:36:46 +01:00
Remove = 'remove',
}
interface Props {
action: PasswordAction;
onOk: any;
onClose: any;
theme: DefaultTheme;
}
interface State {
error: string | null;
currentPasswordEntered: string | null;
currentPasswordConfirmEntered: string | null;
currentPasswordRetypeEntered: string | null;
}
class SessionPasswordModalInner extends React.Component<Props, State> {
private passportInput: HTMLInputElement | null = null;
2020-03-31 08:24:05 +02:00
constructor(props: any) {
super(props);
this.state = {
error: null,
currentPasswordEntered: null,
currentPasswordConfirmEntered: null,
2021-05-04 04:45:56 +02:00
currentPasswordRetypeEntered: null,
2020-01-20 04:36:46 +01:00
};
this.showError = this.showError.bind(this);
this.setPassword = this.setPassword.bind(this);
this.closeDialog = this.closeDialog.bind(this);
2020-01-29 23:34:24 +01:00
this.onPasswordInput = this.onPasswordInput.bind(this);
this.onPasswordConfirmInput = this.onPasswordConfirmInput.bind(this);
this.onPasswordRetypeInput = this.onPasswordRetypeInput.bind(this);
2020-01-29 23:34:24 +01:00
}
public componentDidMount() {
2020-03-31 08:52:32 +02:00
setTimeout(() => {
// tslint:disable-next-line: no-unused-expression
this.passportInput && this.passportInput.focus();
}, 1);
}
public render() {
const { action, onOk } = this.props;
2020-01-20 04:36:46 +01:00
const placeholders =
action === PasswordAction.Change
2021-05-04 04:45:56 +02:00
? [
window.i18n('typeInOldPassword'),
window.i18n('enterPassword'),
window.i18n('confirmPassword'),
]
2020-01-20 04:36:46 +01:00
: [window.i18n('enterPassword'), window.i18n('confirmPassword')];
2020-01-20 04:36:46 +01:00
const confirmButtonColor =
2021-04-22 10:03:58 +02:00
action === PasswordAction.Remove ? SessionButtonColor.Danger : SessionButtonColor.Primary;
return (
2020-01-20 04:36:46 +01:00
<SessionModal
title={window.i18n(`${action}Password`)}
onClose={this.closeDialog}
theme={this.props.theme}
2020-01-20 04:36:46 +01:00
>
<div className="spacer-sm" />
<div className="session-modal__input-group">
<input
type="password"
id="password-modal-input"
ref={input => {
this.passportInput = input;
}}
2020-01-20 04:36:46 +01:00
placeholder={placeholders[0]}
onKeyUp={this.onPasswordInput}
2020-01-20 04:36:46 +01:00
/>
{action !== PasswordAction.Remove && (
<input
type="password"
2020-01-20 04:36:46 +01:00
id="password-modal-input-confirm"
placeholder={placeholders[1]}
onKeyUp={this.onPasswordConfirmInput}
/>
2020-01-20 04:36:46 +01:00
)}
{action === PasswordAction.Change && (
<input
type="password"
id="password-modal-input-reconfirm"
placeholder={placeholders[2]}
onKeyUp={this.onPasswordRetypeInput}
/>
)}
2020-01-20 04:36:46 +01:00
</div>
<div className="spacer-sm" />
{this.showError()}
<div className="session-modal__button-group">
<SessionButton
text={window.i18n('ok')}
buttonColor={confirmButtonColor}
onClick={this.setPassword}
2020-01-20 04:36:46 +01:00
/>
2021-04-22 10:03:58 +02:00
<SessionButton text={window.i18n('cancel')} onClick={this.closeDialog} />
2020-01-20 04:36:46 +01:00
</div>
</SessionModal>
);
}
2020-01-20 04:36:46 +01:00
public async validatePasswordHash(password: string | null) {
// Check if the password matches the hash we have stored
const hash = await getPasswordHash();
2020-07-30 03:07:36 +02:00
if (hash && !PasswordUtil.matchesHash(password, hash)) {
2020-01-20 04:36:46 +01:00
return false;
}
2020-01-20 04:36:46 +01:00
return true;
}
private showError() {
const message = this.state.error;
2020-01-20 04:36:46 +01:00
return (
<>
{message && (
<>
<div className="session-label warning">{message}</div>
<div className="spacer-lg" />
</>
)}
</>
);
}
/**
* Returns false and set the state error field in the input is not a valid password
* or returns true
*/
private validatePassword(firstPassword: string) {
// if user did not fill the first password field, we can't do anything
2021-04-22 10:03:58 +02:00
const errorFirstInput = PasswordUtil.validatePassword(firstPassword, window.i18n);
if (errorFirstInput !== null) {
this.setState({
error: errorFirstInput,
});
return false;
}
return true;
}
2021-04-22 10:03:58 +02:00
private async handleActionSet(enteredPassword: string, enteredPasswordConfirm: string) {
// be sure both password are valid
if (!this.validatePassword(enteredPassword)) {
return;
}
// no need to validate second password. we just need to check that enteredPassword is valid, and that both password matches
if (enteredPassword !== enteredPasswordConfirm) {
this.setState({
error: window.i18n('setPasswordInvalid'),
});
return;
}
await window.setPassword(enteredPassword, null);
ToastUtils.pushToastSuccess(
'setPasswordSuccessToast',
window.i18n('setPasswordTitle'),
window.i18n('setPasswordToastDescription'),
SessionIconType.Lock
);
this.props.onOk(this.props.action);
this.closeDialog();
}
2021-05-04 04:45:56 +02:00
private async handleActionChange(
oldPassword: string,
newPassword: string,
newConfirmedPassword: string
) {
// We don't validate oldPassword on change: this is validate on the validatePasswordHash below
// we only validate the newPassword here
if (!this.validatePassword(newPassword)) {
return;
}
// Check the retyped password matches the new password
if (newPassword !== newConfirmedPassword) {
this.setState({
2021-05-04 04:45:56 +02:00
error: window.i18n('passwordsDoNotMatch'),
});
return;
}
const isValidWithStoredInDB = Boolean(await this.validatePasswordHash(oldPassword));
if (!isValidWithStoredInDB) {
this.setState({
error: window.i18n('changePasswordInvalid'),
});
return;
}
await window.setPassword(newPassword, oldPassword);
ToastUtils.pushToastSuccess(
'setPasswordSuccessToast',
window.i18n('changePasswordTitle'),
window.i18n('changePasswordToastDescription'),
SessionIconType.Lock
);
this.props.onOk(this.props.action);
this.closeDialog();
}
private async handleActionRemove(oldPassword: string) {
// We don't validate oldPassword on change: this is validate on the validatePasswordHash below
2021-04-22 10:03:58 +02:00
const isValidWithStoredInDB = Boolean(await this.validatePasswordHash(oldPassword));
if (!isValidWithStoredInDB) {
this.setState({
error: window.i18n('removePasswordInvalid'),
});
return;
2020-01-20 04:36:46 +01:00
}
await window.setPassword(null, oldPassword);
ToastUtils.pushToastWarning(
'setPasswordSuccessToast',
window.i18n('removePasswordTitle'),
window.i18n('removePasswordToastDescription')
);
this.props.onOk(this.props.action);
this.closeDialog();
2020-01-20 04:36:46 +01:00
}
2020-01-29 23:34:24 +01:00
// tslint:disable-next-line: cyclomatic-complexity
private async setPassword() {
const { action } = this.props;
const {
currentPasswordEntered,
currentPasswordConfirmEntered,
2021-05-04 04:45:56 +02:00
currentPasswordRetypeEntered,
} = this.state;
const { Set, Remove, Change } = PasswordAction;
// Trim leading / trailing whitespace for UX
const firstPasswordEntered = (currentPasswordEntered || '').trim();
const secondPasswordEntered = (currentPasswordConfirmEntered || '').trim();
const thirdPasswordEntered = (currentPasswordRetypeEntered || '').trim();
switch (action) {
case Set: {
await this.handleActionSet(firstPasswordEntered, secondPasswordEntered);
return;
}
case Change: {
await this.handleActionChange(
firstPasswordEntered,
secondPasswordEntered,
thirdPasswordEntered
);
return;
}
case Remove: {
await this.handleActionRemove(firstPasswordEntered);
return;
}
default:
throw missingCaseError(action);
2020-03-25 04:16:45 +01:00
}
}
2020-03-25 04:16:45 +01:00
private closeDialog() {
if (this.props.onClose) {
this.props.onClose();
}
2020-03-25 04:16:45 +01:00
}
private async onPasswordInput(event: any) {
2020-01-30 03:24:20 +01:00
if (event.key === 'Enter') {
return this.setPassword();
2020-01-29 23:34:24 +01:00
}
const currentPasswordEntered = event.target.value;
this.setState({ currentPasswordEntered });
}
2020-01-29 23:34:24 +01:00
private async onPasswordConfirmInput(event: any) {
if (event.key === 'Enter') {
return this.setPassword();
}
const currentPasswordConfirmEntered = event.target.value;
this.setState({ currentPasswordConfirmEntered });
2020-01-29 23:34:24 +01:00
}
private async onPasswordRetypeInput(event: any) {
if (event.key === 'Enter') {
return this.setPassword();
}
const currentPasswordRetypeEntered = event.target.value;
this.setState({ currentPasswordRetypeEntered });
}
}
export const SessionPasswordModal = withTheme(SessionPasswordModalInner);