| 1 |
/* |
| 2 |
* Copyright (C) 2020 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
import cockpit from 'cockpit'; |
| 6 |
import React, { useState } from 'react'; |
| 7 |
import { debounce } from 'throttle-debounce'; |
| 8 |
import { Button } from '@patternfly/react-core/dist/esm/components/Button/index.js'; |
| 9 |
import { FormGroup, FormHelperText } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 10 |
import { InputGroup, InputGroupItem } from '@patternfly/react-core/dist/esm/components/InputGroup/index.js'; |
| 11 |
import { HelperText, HelperTextItem } from "@patternfly/react-core/dist/esm/components/HelperText/index.js"; |
| 12 |
import { Popover } from "@patternfly/react-core/dist/esm/components/Popover/index.js"; |
| 13 |
import { Progress, ProgressMeasureLocation, ProgressSize, type ProgressProps } from "@patternfly/react-core/dist/esm/components/Progress/index.js"; |
| 14 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 15 |
import { EyeIcon, EyeSlashIcon, HelpIcon } from '@patternfly/react-icons'; |
| 16 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 17 |
|
| 18 |
import { FormHelper } from "cockpit-components-form-helper"; |
| 19 |
|
| 20 |
import './cockpit-components-password.scss'; |
| 21 |
|
| 22 |
const _ = cockpit.gettext; |
| 23 |
|
| 24 |
interface PasswordQuality { |
| 25 |
value: number; |
| 26 |
message?: string | undefined; |
| 27 |
} |
| 28 |
|
| 29 |
export function password_quality(password: string, force?: boolean): Promise<PasswordQuality> { |
| 30 |
return new Promise((resolve, reject) => { |
| 31 |
cockpit.spawn(['/usr/bin/pwscore'], { err: "message" }) |
| 32 |
.input(password) |
| 33 |
.done(function(content) { |
| 34 |
const quality = parseInt(content, 10); |
| 35 |
if (quality === 0 && !force) |
| 36 |
reject(new Error(_("Password is too weak"))); |
| 37 |
else |
| 38 |
resolve({ value: quality, message: quality === 100 ? _("Excellent password") : undefined }); |
| 39 |
}) |
| 40 |
.fail(function(ex) { |
| 41 |
if (!force) |
| 42 |
reject(new Error(ex.message || _("Password is not acceptable"))); |
| 43 |
else |
| 44 |
resolve({ value: 0 }); |
| 45 |
}); |
| 46 |
}); |
| 47 |
} |
| 48 |
|
| 49 |
const debounced_password_quality = debounce(300, (value: string, callback: (quality: PasswordQuality) => void) => { |
| 50 |
password_quality(value).catch(() => ({ value: 0 })).then(callback); |
| 51 |
}); |
| 52 |
|
| 53 |
export const PasswordFormFields = ({ |
| 54 |
password_label, password_confirm_label, |
| 55 |
password_label_info, |
| 56 |
initial_password, |
| 57 |
error_password, error_password_confirm, |
| 58 |
idPrefix, change |
| 59 |
}: { |
| 60 |
password_label: string, |
| 61 |
password_confirm_label?: string, |
| 62 |
password_label_info?: string, |
| 63 |
initial_password?: string, |
| 64 |
error_password?: string | undefined, |
| 65 |
error_password_confirm?: string | undefined, |
| 66 |
idPrefix: string, |
| 67 |
change: (field: "password" | "password_confirm", value: string) => void, |
| 68 |
}) => { |
| 69 |
const [password, setPassword] = useState(initial_password || ""); |
| 70 |
const [passwordConfirm, setConfirmPassword] = useState(""); |
| 71 |
const [passwordStrength, setPasswordStrength] = useState<number>(-1); |
| 72 |
const [passwordMessage, setPasswordMessage] = useState<string | undefined>(""); |
| 73 |
const [passwordHidden, setPasswordHidden] = useState(true); |
| 74 |
const [passwordConfirmHidden, setPasswordConfirmHidden] = useState(true); |
| 75 |
|
| 76 |
function onPasswordChanged(value: string) { |
| 77 |
setPassword(value); |
| 78 |
change("password", value); |
| 79 |
|
| 80 |
if (value) { |
| 81 |
debounced_password_quality(value, (strength: PasswordQuality) => { |
| 82 |
setPasswordStrength(strength.value); |
| 83 |
setPasswordMessage(strength.message); |
| 84 |
}); |
| 85 |
} else { |
| 86 |
setPasswordStrength(-1); |
| 87 |
setPasswordMessage(""); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
let variant: ProgressProps['variant']; |
| 92 |
let message; |
| 93 |
let messageColor; |
| 94 |
if (passwordStrength > 66) { |
| 95 |
variant = "success"; |
| 96 |
messageColor = "pf-v6-u-success-color-200"; |
| 97 |
message = _("Strong password"); |
| 98 |
} else if (passwordStrength > 33) { |
| 99 |
variant = "warning"; |
| 100 |
messageColor = "pf-v6-u-warning-color-200"; |
| 101 |
message = _("Acceptable password"); |
| 102 |
} else { |
| 103 |
variant = "danger"; |
| 104 |
messageColor = "pf-v6-u-danger-color-200"; |
| 105 |
message = _("Weak password"); |
| 106 |
} |
| 107 |
|
| 108 |
if (!passwordMessage && message) |
| 109 |
setPasswordMessage(message); |
| 110 |
|
| 111 |
let passwordStrengthValue = passwordStrength; |
| 112 |
if (password !== "" && (passwordStrengthValue >= 0 && passwordStrengthValue < 25)) |
| 113 |
passwordStrengthValue = 25; |
| 114 |
|
| 115 |
return ( |
| 116 |
<> |
| 117 |
<FormGroup label={password_label} |
| 118 |
{...password_label_info && { |
| 119 |
labelHelp: <Popover bodyContent={password_label_info}> |
| 120 |
<Button icon={<HelpIcon />} id={`${password_label}-help-popup-button`} variant="plain" aria-label="Help" /> |
| 121 |
</Popover> |
| 122 |
}} |
| 123 |
id={idPrefix + "-pw1-group"} |
| 124 |
fieldId={idPrefix + "-pw1"}> |
| 125 |
<InputGroup> |
| 126 |
<InputGroupItem isFill> |
| 127 |
<TextInput className="check-passwords" type={passwordHidden ? "password" : "text"} id={idPrefix + "-pw1"} |
| 128 |
autoComplete="new-password" value={password} onChange={(_event, value) => onPasswordChanged(value)} |
| 129 |
validated={error_password ? "warning" : "default"} /> |
| 130 |
</InputGroupItem> |
| 131 |
<InputGroupItem> |
| 132 |
<Button |
| 133 |
variant="control" |
| 134 |
onClick={() => setPasswordHidden(!passwordHidden)} |
| 135 |
aria-label={passwordHidden ? _("Show password") : _("Hide password")}> |
| 136 |
{passwordHidden ? <EyeIcon /> : <EyeSlashIcon />} |
| 137 |
</Button> |
| 138 |
</InputGroupItem> |
| 139 |
</InputGroup> |
| 140 |
{passwordStrengthValue >= 0 && <Flex spaceItems={{ default: 'spaceItemsSm' }}> |
| 141 |
<FlexItem> |
| 142 |
<Progress id={idPrefix + "-meter"} |
| 143 |
className={"pf-v6-u-pt-xs ct-password-strength-meter " + variant} |
| 144 |
title={_("password quality")} |
| 145 |
size={ProgressSize.sm} |
| 146 |
measureLocation={ProgressMeasureLocation.none} |
| 147 |
variant={variant} |
| 148 |
value={passwordStrengthValue} /> |
| 149 |
</FlexItem> |
| 150 |
<FlexItem> |
| 151 |
<div id={idPrefix + "-password-meter-message"} className={"pf-v6-c-form__helper-text " + messageColor} aria-live="polite">{passwordMessage}</div> |
| 152 |
</FlexItem> |
| 153 |
</Flex>} |
| 154 |
{error_password && <FormHelperText> |
| 155 |
<HelperText component="ul" aria-live="polite" id="password-error-message"> |
| 156 |
<HelperTextItem variant="warning" component="li"> |
| 157 |
{error_password} |
| 158 |
</HelperTextItem> |
| 159 |
</HelperText> |
| 160 |
</FormHelperText>} |
| 161 |
</FormGroup> |
| 162 |
|
| 163 |
{password_confirm_label && <FormGroup label={password_confirm_label} |
| 164 |
id={idPrefix + "-pw2-group"} |
| 165 |
fieldId={idPrefix + "-pw2"}> |
| 166 |
<InputGroup> |
| 167 |
<InputGroupItem isFill> |
| 168 |
<TextInput type={passwordConfirmHidden ? "password" : "text"} id={idPrefix + "-pw2"} autoComplete="new-password" |
| 169 |
value={passwordConfirm} onChange={(_event, value) => { setConfirmPassword(value); change("password_confirm", value) }} /> |
| 170 |
</InputGroupItem> |
| 171 |
<InputGroupItem> |
| 172 |
<Button |
| 173 |
variant="control" |
| 174 |
onClick={() => setPasswordConfirmHidden(!passwordConfirmHidden)} |
| 175 |
aria-label={passwordConfirmHidden ? _("Show confirmation password") : _("Hide confirmation password")}> |
| 176 |
{passwordConfirmHidden ? <EyeIcon /> : <EyeSlashIcon />} |
| 177 |
</Button> |
| 178 |
</InputGroupItem> |
| 179 |
</InputGroup> |
| 180 |
<FormHelper fieldId={idPrefix + "-pw2"} helperTextInvalid={error_password_confirm} /> |
| 181 |
</FormGroup>} |
| 182 |
</> |
| 183 |
); |
| 184 |
}; |