-
Notifications
You must be signed in to change notification settings - Fork 2
/
mfa
executable file
·48 lines (40 loc) · 1.13 KB
/
mfa
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
#!/usr/bin/env bash
# mfa - Yubikey 2FA code generator
set -euo pipefail
# check for Yubikey presence
function check_yubikey() {
if ! ykman list | grep -q .; then
echo "No Yubikey found. Please connect your Yubikey and try again." >&2
exit 1
fi
}
# detect the operating system and set the copy command
function set_copy_command() {
case "$(uname -s)" in
Linux*) COPY='xclip -selection clipboard';;
Darwin*) COPY='pbcopy';;
*) echo "Error: Unrecognized operating system." >&2; exit 1;;
esac
}
# generate and optionally copy the 2FA code
function generate_code() {
local account="${1:-}"
if [ -z "$account" ]; then
echo "Listing available accounts and generating codes..."
ykman oath accounts code
else
echo -e "Generating 2FA code for account: $1\n"
ykman oath accounts code "$1" | tee /dev/tty | awk '{print $2}' | tr -d '\n' | ${COPY}
echo -e "\nCode copied to clipboard."
fi
}
function main() {
check_yubikey
set_copy_command
if [ $# -eq 0 ]; then
generate_code
else
generate_code "$1"
fi
}
main "$@"