Skip to content

Commit b0d238e

Browse files
committed
Prompt password until retyped password matches
1 parent 0d19651 commit b0d238e

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

src/pw

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -285,20 +285,20 @@ pw::plugin() {
285285
}
286286

287287
pw::prompt_password() {
288-
local password
288+
local password=""
289289
if [[ -p /dev/stdin ]]; then
290290
IFS= read -r password
291291
else
292-
IFS= read -rsp "Enter password for '${PW_NAME}' (leave empty to generate password):"$'\n' password
293-
if [[ -n "${password}" ]]; then
294-
local retype
295-
IFS= read -rsp "Retype password for '${PW_NAME}':"$'\n' retype
296-
if [[ "${retype}" != "${password}" ]]; then
297-
pw::exit "Error: the entered passwords do not match."
292+
while [[ -z "${password}" ]]; do
293+
IFS= read -rsp "Enter password for '${PW_NAME}' (leave empty to generate password):"$'\n' password
294+
if [[ -n "${password}" ]]; then
295+
local retype
296+
IFS= read -rsp "Retype password for '${PW_NAME}':"$'\n' retype
297+
[[ "${retype}" == "${password}" ]] || password=""
298+
else
299+
PW_PRINT=1 password="$(pw::gen)"
298300
fi
299-
else
300-
PW_PRINT=1 password="$(pw::gen)"
301-
fi
301+
done
302302
fi
303303
printf "%s" "${password}"
304304
}

test/pw-plugins.bats

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,16 @@ EOF
9292
}
9393

9494
# bats test_tags=tag:manual_test
95-
@test "add prompts password and fails if retyped password does not match" {
96-
_skip_manual_test "'test 1' and 'test 2'"
95+
@test "add prompts password until retyped password matches" {
96+
_skip_manual_test "'test 1' and 'test 2', then 'test 3' twice"
9797
run pw add "${NAME_A}"
98-
assert_failure
98+
assert_success
9999
cat << EOF | assert_output -
100100
Enter password for '${NAME_A}' (leave empty to generate password):
101101
Retype password for '${NAME_A}':
102-
Error: the entered passwords do not match.
102+
Enter password for '${NAME_A}' (leave empty to generate password):
103+
Retype password for '${NAME_A}':
104+
test add <> <> <${PW_KEYCHAIN}> <test 3> <${NAME_A}> <> <> <>
103105
EOF
104106
}
105107

@@ -193,14 +195,16 @@ EOF
193195
}
194196

195197
# bats test_tags=tag:manual_test
196-
@test "edit prompts password and fails if retyped password does not match" {
197-
_skip_manual_test "'test 1' and 'test 2'"
198+
@test "edit prompts password until retyped password matches" {
199+
_skip_manual_test "'test 1' and 'test 2', then 'test 3' twice"
198200
run pw edit "${NAME_A}"
199-
assert_failure
201+
assert_success
200202
cat << EOF | assert_output -
201203
Enter password for '${NAME_A}' (leave empty to generate password):
202204
Retype password for '${NAME_A}':
203-
Error: the entered passwords do not match.
205+
Enter password for '${NAME_A}' (leave empty to generate password):
206+
Retype password for '${NAME_A}':
207+
test edit <> <> <${PW_KEYCHAIN}> <test 3> <${NAME_A}> <> <>
204208
EOF
205209
}
206210

0 commit comments

Comments
 (0)