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

235 lines
6 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;
}
export class SessionPasswordModal extends React.Component<Props, State> {
2020-03-31 08:24:05 +02:00
private readonly passwordInput: React.RefObject<HTMLInputElement>;
private readonly passwordInputConfirm: React.RefObject<HTMLInputElement>;
constructor(props: any) {
super(props);
this.state = {
error: 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.onKeyUp = this.onKeyUp.bind(this);
2020-03-25 04:16:45 +01:00
this.onPaste = this.onPaste.bind(this);
2020-03-31 08:24:05 +02:00
this.passwordInput = React.createRef();
this.passwordInputConfirm = React.createRef();
2020-01-29 23:34:24 +01:00
}
public componentDidMount() {
2020-03-31 08:24:05 +02:00
setTimeout(() => this.passwordInput.current?.focus(), 100);
}
public render() {
const { action, onOk } = this.props;
2020-01-20 04:36:46 +01:00
const placeholders =
this.props.action === PasswordAction.Change
? [window.i18n('typeInOldPassword'), window.i18n('enterPassword')]
: [window.i18n('enterPassword'), window.i18n('confirmPassword')];
2020-01-20 04:36:46 +01:00
const confirmButtonColor =
this.props.action === PasswordAction.Remove
? 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"
2020-03-31 08:24:05 +02:00
ref={this.passwordInput}
2020-01-20 04:36:46 +01:00
placeholder={placeholders[0]}
2020-01-30 03:24:20 +01:00
onKeyUp={this.onKeyUp}
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"
2020-03-31 08:24:05 +02:00
ref={this.passwordInputConfirm}
2020-01-20 04:36:46 +01:00
placeholder={placeholders[1]}
2020-01-30 03:24:20 +01:00
onKeyUp={this.onKeyUp}
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" />
</>
)}
</>
);
}
private async setPassword(onSuccess: any) {
2020-03-31 08:24:05 +02:00
// Trim leading / trailing whitespace for UX
2020-03-31 08:32:50 +02:00
const enteredPassword = String(this.passwordInput.current?.value).trim();
const enteredPasswordConfirm = String(this.passwordInputConfirm.current?.value).trim();
if (enteredPassword.length === 0 || enteredPasswordConfirm.length === 0) {
2020-01-24 07:10:42 +01:00
return;
}
2020-03-31 08:24:05 +02:00
// Check passwords entered
2020-01-20 04:36:46 +01:00
if (
enteredPassword.length === 0 ||
(this.props.action === PasswordAction.Change &&
enteredPasswordConfirm.length === 0)
) {
this.setState({
error: window.i18n('noGivenPassword'),
});
return;
}
2020-01-20 04:36:46 +01:00
// Passwords match or remove password successful
const newPassword =
this.props.action === PasswordAction.Remove
? null
: enteredPasswordConfirm;
const oldPassword =
this.props.action === PasswordAction.Set ? null : enteredPassword;
// Check if password match, when setting, changing or removing
2020-01-20 04:36:46 +01:00
const valid =
this.props.action !== PasswordAction.Set
? !!await this.validatePasswordHash(oldPassword)
: enteredPassword === enteredPasswordConfirm;
if (!valid) {
this.setState({
error: window.i18n(`${this.props.action}PasswordInvalid`),
});
return;
}
2020-01-20 04:36:46 +01:00
await window.setPassword(newPassword, oldPassword);
const toastParams = {
title: window.i18n(`${this.props.action}PasswordTitle`),
description: window.i18n(`${this.props.action}PasswordToastDescription`),
type: this.props.action !== PasswordAction.Remove ? 'success' : 'warning',
icon: this.props.action !== PasswordAction.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;
}
2020-01-29 23:34:24 +01:00
private async onKeyUp(event: any) {
const { onOk } = this.props;
2020-01-30 03:24:20 +01:00
if (event.key === 'Enter') {
2020-01-29 23:34:24 +01:00
await this.setPassword(onOk);
}
event.preventDefault();
}
}