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

260 lines
6.8 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';
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;
}
interface State {
error: string | null;
currentPasswordEntered: string | null;
currentPasswordConfirmEntered: string | null;
}
export class SessionPasswordModal 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}
>
<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();
if (hash && !window.passwordUtil.matchesHash(password, hash)) {
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
const errorFirstInput = window.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) {
const errorSecondInput = window.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) {
this.setState({
error: window.i18n(`${action}PasswordInvalid`),
});
return;
}
2020-01-20 04:36:46 +01:00
await window.setPassword(newPassword, oldPassword);
const toastParams = {
title: window.i18n(`${action}PasswordTitle`),
description: window.i18n(`${action}PasswordToastDescription`),
type: action !== Remove ? 'success' : 'warning',
icon: action !== Remove ? 'lock' : undefined,
2020-01-20 04:36:46 +01:00
};
window.pushToast({
2020-01-20 04:36:46 +01:00
id: 'set-password-success-toast',
...toastParams,
});
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
)
);
2020-03-25 04:16:45 +01:00
window.pushToast({
title,
type: 'warning',
});
}
// 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
}
}