-
Notifications
You must be signed in to change notification settings - Fork 0
/
totp
executable file
·51 lines (38 loc) · 1.69 KB
/
totp
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
#!/bin/bash
# Header for the table with right padding
printf "%-30s\t%-30s\t%-15s\t%-15s\t%s\n" "Service" "Username" "TOTP Code" "Next Code" "Remainder"
# Read each TOTP entry from the "totp/all" pass entry, sort by "Service" column, and then process
pass show totp/all | sort -t$'\t' -k1,1 | while IFS= read -r line; do
# Extract the entry name
entry_name=$(echo "$line" | grep -oP '(?<=otpauth://totp/)[^?]+')
# Extract the secret key from the line
secret_key=$(echo "$line" | grep -oP '(?<=secret=)[^&]+')
# Split entry name by ":" or "/"
if [[ "$entry_name" == *":"* ]]; then
IFS=':' read -r service username <<< "$entry_name"
elif [[ "$entry_name" == *"/"* ]]; then
IFS='/' read -r service username <<< "$entry_name"
else
service=""
username="$entry_name"
fi
# If service is empty, set it to "-"
if [ -z "$service" ]; then
service="-"
fi
# Pad service and username to 30 characters
service=$(printf '%-30s' "$service")
username=$(printf '%-30s' "$username")
# Generate HOTP using oathtool for current time and next time
codes=$(oathtool -s 30s -d 6 -b --totp "$secret_key" -w 1)
# Extract the current and next codes
current_code=$(echo "$codes" | head -n 1)
next_code=$(echo "$codes" | tail -n 1)
# Calculate the remainder of epoch time divided by 30
remainder=$(( $(date '+%s') % 30 ))
# format topts
topt1=$(printf '%-15s' "$current_code")
topt2=$(printf '%-15s' "$next_code")
# Print the service, username, current TOTP code, and next TOTP code in tabular format
printf "%s\t%s\t%s\t%s\t%s\n" "$service" "$username" "$topt1" "$topt2" "$remainder secs"
done