|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +source common.sh |
| 4 | + |
| 5 | +authDir="$HOME/.local/share/nix/auth" |
| 6 | +mkdir -p "$authDir" |
| 7 | + |
| 8 | +printf "protocol=https\nhost=example.org\nusername=alice\npassword=foobar\n" > "$authDir/example.org" |
| 9 | + |
| 10 | +# A matching user name returns the stored password. |
| 11 | +[[ $(printf "protocol=https\nhost=example.org\nusername=alice\n" | nix auth fill) \ |
| 12 | + = $(printf "protocol=https\nhost=example.org\nusername=alice\npassword=foobar\n") ]] |
| 13 | + |
| 14 | +# A request without a user name gets both filled in. |
| 15 | +[[ $(printf "protocol=https\nhost=example.org\n" | nix auth fill) \ |
| 16 | + = $(printf "protocol=https\nhost=example.org\nusername=alice\npassword=foobar\n") ]] |
| 17 | + |
| 18 | +# A request without a protocol is rejected. |
| 19 | +expect 1 bash -c 'printf "host=example.org\n" | nix auth fill' |
| 20 | + |
| 21 | +# A non-matching user name returns nothing. |
| 22 | +[[ -z $(printf "protocol=https\nhost=example.org\nusername=bob\n" | nix auth fill) ]] |
| 23 | + |
| 24 | +# A password with surrounding whitespace is preserved verbatim. |
| 25 | +printf 'protocol=https\nhost=spaces.org\nusername=alice\npassword= sp ace \n' > "$authDir/spaces.org" |
| 26 | +[[ $(printf "protocol=https\nhost=spaces.org\n" | nix auth fill) \ |
| 27 | + = $(printf 'protocol=https\nhost=spaces.org\nusername=alice\npassword= sp ace \n') ]] |
| 28 | + |
| 29 | +# A file without a host is ignored (must not match every host). |
| 30 | +printf "protocol=https\nusername=mallory\npassword=leak\n" > "$authDir/nohost" |
| 31 | +[[ -z $(printf "protocol=https\nhost=anywhere.org\n" | nix auth fill) ]] |
| 32 | +rm "$authDir/nohost" |
| 33 | + |
| 34 | +# Without an askpass helper, an unsatisfiable required request returns nothing. |
| 35 | +unset SSH_ASKPASS |
| 36 | +[[ -z $(printf "protocol=https\nhost=fnord.org\nusername=bob\n" | nix auth fill --require) ]] |
| 37 | + |
| 38 | +# Interactive prompting via $SSH_ASKPASS. |
| 39 | +askpass="$TEST_ROOT/ask-pass" |
| 40 | +cat > "$askpass" <<EOF |
| 41 | +#! $SHELL |
| 42 | +if [[ \$1 =~ Password ]]; then |
| 43 | + printf "foobar" |
| 44 | +elif [[ \$1 =~ Username ]]; then |
| 45 | + printf "alice" |
| 46 | +else |
| 47 | + exit 1 |
| 48 | +fi |
| 49 | +EOF |
| 50 | +chmod +x "$askpass" |
| 51 | +export SSH_ASKPASS="$askpass" |
| 52 | + |
| 53 | +[[ $(printf "protocol=https\nhost=fnord.org\nusername=bob\n" | nix auth fill --require) \ |
| 54 | + = $(printf "protocol=https\nhost=fnord.org\nusername=bob\npassword=foobar\n") ]] |
| 55 | + |
| 56 | +[[ $(printf "protocol=https\nhost=fnord.org\n" | nix auth fill --require) \ |
| 57 | + = $(printf "protocol=https\nhost=fnord.org\nusername=alice\npassword=foobar\n") ]] |
| 58 | + |
| 59 | +# With store-auth, prompted credentials are saved and reused later. |
| 60 | +printf "protocol=https\nhost=stored.org\n" | nix auth fill --require --store-auth > /dev/null |
| 61 | +unset SSH_ASKPASS |
| 62 | +[[ $(printf "protocol=https\nhost=stored.org\n" | nix auth fill) \ |
| 63 | + = $(printf "protocol=https\nhost=stored.org\nusername=alice\npassword=foobar\n") ]] |
| 64 | + |
| 65 | +# Stored credential files must not be world/group readable. |
| 66 | +storedFile=$(echo "$authDir"/auto-stored.org-*) |
| 67 | +perm=$(stat -c %a "$storedFile" 2>/dev/null || stat -f %Lp "$storedFile") |
| 68 | +[[ $perm = 600 ]] |
| 69 | + |
| 70 | +# External credential helpers. |
| 71 | +helper="$TEST_ROOT/auth-helper" |
| 72 | +cat > "$helper" <<EOF |
| 73 | +#! $SHELL |
| 74 | +if [[ \$1 == get ]]; then |
| 75 | + host= |
| 76 | + while read line; do |
| 77 | + [[ \$line == host=* ]] && host=\${line:5} |
| 78 | + done |
| 79 | + [[ \$host == bla.org ]] && printf "username=bob\npassword=xyzzy\n" |
| 80 | +fi |
| 81 | +EOF |
| 82 | +chmod +x "$helper" |
| 83 | + |
| 84 | +[[ $(printf "protocol=https\nhost=bla.org\n" | nix auth fill --auth-sources "builtin:nix $helper") \ |
| 85 | + = $(printf "protocol=https\nhost=bla.org\nusername=bob\npassword=xyzzy\n") ]] |
| 86 | + |
| 87 | +[[ -z $(printf "protocol=https\nhost=bla2.org\n" | nix auth fill --auth-sources "builtin:nix $helper") ]] |
0 commit comments