2020-05-14 22:19:15 +03:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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 React, {PureComponent, RefCallback, RefObject} from "react";
|
|
|
|
import classNames from "classnames";
|
|
|
|
import zxcvbn from "zxcvbn";
|
|
|
|
|
|
|
|
import SdkConfig from "../../../SdkConfig";
|
|
|
|
import withValidation, {IFieldState, IValidationResult} from "../elements/Validation";
|
|
|
|
import {_t, _td} from "../../../languageHandler";
|
2020-11-19 18:10:40 +03:00
|
|
|
import Field, {IInputProps} from "../elements/Field";
|
2020-05-14 22:19:15 +03:00
|
|
|
|
2020-11-19 18:10:40 +03:00
|
|
|
interface IProps extends Omit<IInputProps, "onValidate"> {
|
2020-05-14 22:33:50 +03:00
|
|
|
autoFocus?: boolean;
|
2020-05-14 22:19:15 +03:00
|
|
|
id?: string;
|
|
|
|
className?: string;
|
|
|
|
minScore: 0 | 1 | 2 | 3 | 4;
|
|
|
|
value: string;
|
2020-05-14 22:33:50 +03:00
|
|
|
fieldRef?: RefCallback<Field> | RefObject<Field>;
|
2020-05-14 22:19:15 +03:00
|
|
|
|
|
|
|
label?: string;
|
|
|
|
labelEnterPassword?: string;
|
|
|
|
labelStrongPassword?: string;
|
|
|
|
labelAllowedButUnsafe?: string;
|
|
|
|
|
2020-05-25 18:47:57 +03:00
|
|
|
onChange(ev: React.FormEvent<HTMLElement>);
|
2020-05-14 22:19:15 +03:00
|
|
|
onValidate(result: IValidationResult);
|
|
|
|
}
|
|
|
|
|
2020-09-21 16:35:35 +03:00
|
|
|
class PassphraseField extends PureComponent<IProps> {
|
2020-05-14 22:19:15 +03:00
|
|
|
static defaultProps = {
|
|
|
|
label: _td("Password"),
|
|
|
|
labelEnterPassword: _td("Enter password"),
|
|
|
|
labelStrongPassword: _td("Nice, strong password!"),
|
|
|
|
labelAllowedButUnsafe: _td("Password is allowed, but unsafe"),
|
|
|
|
};
|
|
|
|
|
2020-09-21 16:35:35 +03:00
|
|
|
public readonly validate = withValidation<this, zxcvbn.ZXCVBNResult>({
|
|
|
|
description: function(complexity) {
|
2020-05-14 22:19:15 +03:00
|
|
|
const score = complexity ? complexity.score : 0;
|
|
|
|
return <progress className="mx_PassphraseField_progress" max={4} value={score} />;
|
|
|
|
},
|
2020-09-21 16:35:35 +03:00
|
|
|
deriveData: async ({ value }) => {
|
|
|
|
if (!value) return null;
|
|
|
|
const { scorePassword } = await import('../../../utils/PasswordScorer');
|
|
|
|
return scorePassword(value);
|
|
|
|
},
|
2020-05-14 22:19:15 +03:00
|
|
|
rules: [
|
|
|
|
{
|
|
|
|
key: "required",
|
|
|
|
test: ({ value, allowEmpty }) => allowEmpty || !!value,
|
|
|
|
invalid: () => _t(this.props.labelEnterPassword),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "complexity",
|
2020-09-21 16:35:35 +03:00
|
|
|
test: async function({ value }, complexity) {
|
2020-05-14 22:19:15 +03:00
|
|
|
if (!value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const safe = complexity.score >= this.props.minScore;
|
|
|
|
const allowUnsafe = SdkConfig.get()["dangerously_allow_unsafe_and_insecure_passwords"];
|
|
|
|
return allowUnsafe || safe;
|
|
|
|
},
|
2020-09-21 16:35:35 +03:00
|
|
|
valid: function(complexity) {
|
2020-05-14 22:19:15 +03:00
|
|
|
// Unsafe passwords that are valid are only possible through a
|
|
|
|
// configuration flag. We'll print some helper text to signal
|
|
|
|
// to the user that their password is allowed, but unsafe.
|
2020-09-21 16:35:35 +03:00
|
|
|
if (complexity.score >= this.props.minScore) {
|
2020-05-14 22:19:15 +03:00
|
|
|
return _t(this.props.labelStrongPassword);
|
|
|
|
}
|
|
|
|
return _t(this.props.labelAllowedButUnsafe);
|
|
|
|
},
|
2020-09-21 16:35:35 +03:00
|
|
|
invalid: function(complexity) {
|
2020-05-14 22:19:15 +03:00
|
|
|
if (!complexity) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
const { feedback } = complexity;
|
|
|
|
return feedback.warning || feedback.suggestions[0] || _t("Keep going...");
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
onValidate = async (fieldState: IFieldState) => {
|
|
|
|
const result = await this.validate(fieldState);
|
|
|
|
this.props.onValidate(result);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return <Field
|
|
|
|
id={this.props.id}
|
2020-05-14 22:33:50 +03:00
|
|
|
autoFocus={this.props.autoFocus}
|
2020-05-14 22:19:15 +03:00
|
|
|
className={classNames("mx_PassphraseField", this.props.className)}
|
|
|
|
ref={this.props.fieldRef}
|
|
|
|
type="password"
|
|
|
|
autoComplete="new-password"
|
|
|
|
label={_t(this.props.label)}
|
|
|
|
value={this.props.value}
|
|
|
|
onChange={this.props.onChange}
|
|
|
|
onValidate={this.onValidate}
|
2020-06-18 16:32:43 +03:00
|
|
|
/>;
|
2020-05-14 22:19:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PassphraseField;
|