-
Notifications
You must be signed in to change notification settings - Fork 12
/
Password.jsx
277 lines (250 loc) · 9.44 KB
/
Password.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright (C) 2023 Red Hat, Inc.
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with This program; If not, see <http://www.gnu.org/licenses/>.
*/
import cockpit from "cockpit";
import { debounce } from "throttle-debounce";
import React, { useEffect, useMemo, useState } from "react";
import {
Button,
FormGroup,
FormHelperText,
HelperText,
HelperTextItem,
InputGroup,
InputGroupItem,
TextInput,
} from "@patternfly/react-core";
import {
CheckCircleIcon,
ExclamationCircleIcon,
ExclamationTriangleIcon,
EyeIcon,
EyeSlashIcon
} from "@patternfly/react-icons";
// eslint-disable-next-line camelcase
import { password_quality } from "cockpit-components-password.jsx";
const _ = cockpit.gettext;
export const ruleLength = {
check: (policy, password) => password.length >= policy["min-length"].v,
id: "length",
isError: true,
text: (policy) => cockpit.format(_("Must be at least $0 characters"), policy["min-length"].v),
};
/* Calculate the password quality levels based on the password policy
* If the policy specifies a 'is-strict' rule anything bellow the minimum specified by the policy
* is considered invalid
* @param {int} minQualility - the minimum quality level
* @return {array} - the password strengh levels
*/
const getStrengthLevels = (minQualility, isStrict) => {
const levels = [{
higher_bound: minQualility - 1,
icon: <ExclamationCircleIcon />,
id: "weak",
label: _("Weak"),
lower_bound: 0,
valid: !isStrict,
variant: "error",
}];
if (minQualility <= 69) {
levels.push({
higher_bound: 69,
icon: <ExclamationTriangleIcon />,
id: "medium",
label: _("Medium"),
lower_bound: minQualility,
valid: true,
variant: "warning",
});
}
levels.push({
higher_bound: 100,
icon: <CheckCircleIcon />,
id: "strong",
label: _("Strong"),
lower_bound: Math.max(70, minQualility),
valid: true,
variant: "success",
});
return levels;
};
const getRuleResults = (rules, policy, password) => {
return rules.map(rule => {
return {
id: rule.id,
isError: rule.isError,
isSatisfied: password.length > 0 ? rule.check(policy, password) : null,
text: rule.text(policy, password)
};
});
};
const rulesSatisfied = ruleResults => ruleResults.every(r => r.isSatisfied || !r.isError);
const passwordStrengthLabel = (idPrefix, strength, strengthLevels) => {
const level = strengthLevels.filter(l => l.id === strength)[0];
if (level) {
return (
<HelperText>
<HelperTextItem id={idPrefix + "-password-strength-label"} variant={level.variant} icon={level.icon}>
{level.label}
</HelperTextItem>
</HelperText>
);
}
};
export const PasswordFormFields = ({
confirmPassword,
confirmPasswordLabel,
idPrefix,
password,
passwordLabel,
policy,
rules,
setConfirmPassword,
setIsValid,
setPassword,
}) => {
const [passwordHidden, setPasswordHidden] = useState(true);
const [confirmHidden, setConfirmHidden] = useState(true);
const [checkPassword, setCheckPassword] = useState(password);
const [checkConfirmPassword, setCheckConfirmPassword] = useState(confirmPassword);
const [passwordStrength, setPasswordStrength] = useState("");
useEffect(() => {
debounce(300, () => setCheckPassword(password))();
}, [password, setCheckPassword]);
useEffect(() => {
debounce(300, () => setCheckConfirmPassword(confirmPassword))();
}, [confirmPassword, setCheckConfirmPassword]);
const ruleResults = useMemo(() => {
return getRuleResults(rules, policy, checkPassword);
}, [policy, checkPassword, rules]);
const ruleConfirmMatches = checkPassword.length > 0 ? checkPassword === checkConfirmPassword : null;
const ruleHelperItems = ruleResults.map(rule => {
let variant = rule.isSatisfied === null ? "indeterminate" : rule.isSatisfied ? "success" : "error";
if (!rule.isError) {
if (rule.isSatisfied || rule.isSatisfied === null) {
return null;
}
variant = "warning";
}
return (
<HelperTextItem
key={rule.id}
id={idPrefix + "-password-rule-" + rule.id}
isDynamic
variant={variant}
component="li"
>
{rule.text}
</HelperTextItem>
);
});
const ruleConfirmVariant = ruleConfirmMatches === null ? "indeterminate" : ruleConfirmMatches ? "success" : "error";
const strengthLevels = useMemo(() => {
return policy && getStrengthLevels(policy["min-quality"].v, policy["is-strict"].v);
}, [policy]);
useEffect(() => {
const updatePasswordStrength = async () => {
const _passwordStrength = await getPasswordStrength(checkPassword, strengthLevels);
setPasswordStrength(_passwordStrength);
};
updatePasswordStrength();
}, [checkPassword, strengthLevels]);
useEffect(() => {
setIsValid(
rulesSatisfied(ruleResults) &&
ruleConfirmMatches &&
isValidStrength(passwordStrength, strengthLevels)
);
}, [setIsValid, ruleResults, ruleConfirmMatches, passwordStrength, strengthLevels]);
return (
<>
<FormGroup
label={passwordLabel}
labelInfo={rulesSatisfied(ruleResults) && passwordStrengthLabel(idPrefix, passwordStrength, strengthLevels)}
>
<InputGroup>
<InputGroupItem isFill>
<TextInput
type={passwordHidden ? "password" : "text"}
value={password}
onChange={(_event, val) => setPassword(val)}
id={idPrefix + "-password-field"}
/>
</InputGroupItem>
<InputGroupItem>
<Button
variant="control"
onClick={() => setPasswordHidden(!passwordHidden)}
aria-label={passwordHidden ? _("Show password") : _("Hide password")}
>
{passwordHidden ? <EyeIcon /> : <EyeSlashIcon />}
</Button>
</InputGroupItem>
</InputGroup>
<FormHelperText>
<HelperText component="ul" aria-live="polite" id={idPrefix + "-password-field-helper"}>
{ruleHelperItems}
</HelperText>
</FormHelperText>
</FormGroup>
<FormGroup
label={confirmPasswordLabel}
>
<InputGroup>
<InputGroupItem isFill><TextInput
type={confirmHidden ? "password" : "text"}
value={confirmPassword}
onChange={(_event, val) => setConfirmPassword(val)}
id={idPrefix + "-password-confirm-field"}
/>
</InputGroupItem>
<InputGroupItem>
<Button
variant="control"
onClick={() => setConfirmHidden(!confirmHidden)}
aria-label={confirmHidden ? _("Show confirmed password") : _("Hide confirmed password")}
>
{confirmHidden ? <EyeIcon /> : <EyeSlashIcon />}
</Button>
</InputGroupItem>
</InputGroup>
<FormHelperText>
<HelperText component="ul" aria-live="polite" id="password-confirm-field-helper">
<HelperTextItem
id={idPrefix + "-password-rule-match"}
isDynamic
variant={ruleConfirmVariant}
component="li"
>
{_("Passphrases must match")}
</HelperTextItem>
</HelperText>
</FormHelperText>
</FormGroup>
</>
);
};
const getPasswordStrength = async (password, strengthLevels) => {
// In case of unacceptable password just return 0
const force = true;
const quality = await password_quality(password, force);
const level = strengthLevels.filter(l => l.lower_bound <= quality.value && l.higher_bound >= quality.value)[0];
return level.id;
};
const isValidStrength = (strength, strengthLevels) => {
const level = strengthLevels.filter(l => l.id === strength)[0];
return level ? level.valid : false;
};