-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmfa
More file actions
executable file
·46 lines (38 loc) · 1.11 KB
/
Copy pathmfa
File metadata and controls
executable file
·46 lines (38 loc) · 1.11 KB
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
#!/usr/bin/env bash
# mfa - Yubikey 2FA code generator
set -euo pipefail
source "$HOME/.config/utils/common.sh"
# check for Yubikey presence
function check_yubikey() {
if ! ykman list | grep -q .; then
log_error "No Yubikey found. Please connect your Yubikey and try again."
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';;
*) log_error "Unrecognized operating system."
esac
}
# generate and optionally copy the 2FA code
function generate_code() {
local account="${1:-}"
if [ -z "$account" ]; then
log_info "Listing available accounts and generating codes..."
ykman oath accounts code
else
log_info "Generating 2FA code for account: $1"
ykman oath accounts code "$1" | tee /dev/tty | awk -F' ' '{print $2}' | tr -d '\n' | ${COPY}
log_info "Code copied to clipboard."
fi
}
check_yubikey
set_copy_command
if [ $# -eq 0 ]; then
generate_code
else
generate_code "$1"
fi