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

300 lines
8.0 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';
2020-07-30 03:07:36 +02:00
import { PasswordUtil } from '../../util/';
import { ToastUtils } from '../../session/utils';
import { toast } from 'react-toastify';
import { SessionToast, SessionToastType } from './SessionToast';
import { SessionIconType } from './icon';
import { DefaultTheme, withTheme } from 'styled-components';
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;
}
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,
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);
2020-03-31 08:24:05 +02:00
this.onPaste = this.onPaste.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
2020-01-20 04:36:46 +01:00
? [window.i18n('typeInOldPassword'), window.i18n('enterPassword')]
: [window.i18n('enterPassword'), window.i18n('confirmPassword')];
2020-01-20 04:36:46 +01:00
const confirmButtonColor =
action === PasswordAction.Remove
2020-01-20 04:36:46 +01:00
? SessionButtonColor.Danger
: SessionButtonColor.Primary;
return (
2020-01-20 04:36:46 +01:00
<SessionModal
title={window.i18n(`${action}Password`)}
onOk={() => null}
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}
maxLength={window.CONSTANTS.MAX_PASSWORD_LENGTH}
2020-03-25 04:16:45 +01:00
onPaste={this.onPaste}
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}
maxLength={window.CONSTANTS.MAX_PASSWORD_LENGTH}
2020-03-25 04:16:45 +01:00
onPaste={this.onPaste}
/>
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={async () => this.setPassword(onOk)}
/>
<SessionButton
text={window.i18n('cancel')}
onClick={this.closeDialog}
/>
</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 window.Signal.Data.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" />
</>
)}
</>
);
}
// tslint:disable-next-line: cyclomatic-complexity
2020-04-12 11:11:42 +02:00
private async setPassword(onSuccess?: any) {
const { action } = this.props;
const {
currentPasswordEntered,
currentPasswordConfirmEntered,
} = this.state;
const { Set, Remove, Change } = PasswordAction;
2020-04-01 05:13:51 +02:00
2020-03-31 08:24:05 +02:00
// Trim leading / trailing whitespace for UX
const enteredPassword = (currentPasswordEntered || '').trim();
const enteredPasswordConfirm = (currentPasswordConfirmEntered || '').trim();
2020-01-24 07:10:42 +01:00
// if user did not fill the first password field, we can't do anything
2020-07-30 03:07:36 +02:00
const errorFirstInput = PasswordUtil.validatePassword(
enteredPassword,
window.i18n
);
if (errorFirstInput !== null) {
this.setState({
error: errorFirstInput,
});
return;
}
// if action is Set or Change, we need a valid ConfirmPassword
if (action === Set || action === Change) {
2020-07-30 03:07:36 +02:00
const errorSecondInput = PasswordUtil.validatePassword(
enteredPasswordConfirm,
window.i18n
);
if (errorSecondInput !== null) {
this.setState({
error: errorSecondInput,
});
return;
}
}
2020-01-20 04:36:46 +01:00
// Passwords match or remove password successful
const newPassword = action === Remove ? null : enteredPasswordConfirm;
const oldPassword = action === Set ? null : enteredPassword;
// Check if password match, when setting, changing or removing
let valid;
if (action === Set) {
valid = enteredPassword === enteredPasswordConfirm;
} else {
valid = Boolean(await this.validatePasswordHash(oldPassword));
}
2020-01-20 04:36:46 +01:00
if (!valid) {
let str;
switch (action) {
case Set:
str = window.i18n('setPasswordInvalid');
break;
case Change:
str = window.i18n('changePasswordInvalid');
break;
case Remove:
str = window.i18n('removePasswordInvalid');
break;
default:
throw new Error(`Invalid action ${action}`);
}
this.setState({
error: str,
});
return;
}
2020-01-20 04:36:46 +01:00
await window.setPassword(newPassword, oldPassword);
let title;
let description;
switch (action) {
case Set:
title = window.i18n('setPasswordTitle');
description = window.i18n('setPasswordToastDescription');
break;
case Change:
title = window.i18n('changePasswordTitle');
description = window.i18n('changePasswordToastDescription');
break;
case Remove:
title = window.i18n('removePasswordTitle');
description = window.i18n('removePasswordToastDescription');
break;
default:
throw new Error(`Invalid action ${action}`);
}
if (action !== Remove) {
ToastUtils.pushToastSuccess(
'setPasswordSuccessToast',
title,
description,
SessionIconType.Lock
);
} else {
ToastUtils.pushToastWarning(
'setPasswordSuccessToast',
title,
description
);
}
onSuccess(this.props.action);
this.closeDialog();
}
private closeDialog() {
2020-01-20 04:36:46 +01:00
if (this.props.onClose) {
this.props.onClose();
}
}
2020-01-29 23:34:24 +01:00
2020-03-25 04:16:45 +01:00
private onPaste(event: any) {
const clipboard = event.clipboardData.getData('text');
2020-03-25 04:46:14 +01:00
if (clipboard.length > window.CONSTANTS.MAX_PASSWORD_LENGTH) {
const title = String(
window.i18n(
'pasteLongPasswordToastTitle',
window.CONSTANTS.MAX_PASSWORD_LENGTH
)
);
ToastUtils.pushToastWarning('passwordModal', title);
2020-03-25 04:16:45 +01:00
}
// Prevent pating into input
return false;
}
private async onPasswordInput(event: any) {
2020-01-30 03:24:20 +01:00
if (event.key === 'Enter') {
return this.setPassword(this.props.onOk);
2020-01-29 23:34:24 +01:00
}
this.setState({ currentPasswordEntered: event.target.value });
}
2020-01-29 23:34:24 +01:00
private async onPasswordConfirmInput(event: any) {
if (event.key === 'Enter') {
return this.setPassword(this.props.onOk);
}
this.setState({ currentPasswordConfirmEntered: event.target.value });
2020-01-29 23:34:24 +01:00
}
}
export const SessionPasswordModal = withTheme(SessionPasswordModalInner);