dotfiles

Torpy's handcrafted dootfiles.
Log | Files | Refs | README

otp (1720B)


      1 #!/bin/sh
      2 
      3 # Get a one-time password, or add a OTP secret to your pass-otp store.
      4 
      5 # The assumption of this script is that all otp passwords are stored with the
      6 # suffix `-otp`. This script automatically appends newly added otps as such.
      7 
      8 # For OTP passwords to be generated properly, it is important for the local
      9 # computer to have its time properly synced. This can be done with the command
     10 # below which requires the package `ntp`.
     11 
     12 ifinstalled pass pass-otp || exit 1
     13 
     14 dir="${PASSWORD_STORE_DIR}"
     15 
     16 choice="$({ echo "🆕add" ; echo "🕙sync-time" ; ls "$dir"/*-otp.gpg ;} | sed "s/.*\///;s/-otp.gpg//" | dmenu -p "Pick a 2FA:")"
     17 
     18 case $choice in
     19 	🆕add )
     20 		ifinstalled maim zbar || exit 1
     21 
     22 		temp=$(mktemp -p "$XDG_RUNTIME_DIR" --suffix=.png)
     23 		otp="otp-test-script"
     24 		trap 'rm -f $temp; pass rm -f $otp' HUP INT QUIT TERM PWR EXIT
     25 
     26 		notify-send "Scan the image." "Scan the OTP QR code."
     27 
     28 		maim -s "$temp" || exit 1
     29 		info="$(zbarimg -q "$temp")"
     30 		info="${info#QR-Code:}"
     31 
     32 		if echo "$info" | pass otp insert "$otp"; then
     33 			while true ; do
     34 				export name="$(echo | dmenu -p "Give this One Time Password a one-word name:")"
     35 				echo "$name" | grep -q -- "^[A-z0-9-]\+$" && break
     36 			done
     37 			pass mv "$otp" "$name-otp"
     38 			notify-send "Successfully added." "$name-otp has been created."
     39 		else
     40 			notify-send "No OTP data found." "Try to scan the image again more precisely."
     41 		fi
     42 		;;
     43 	🕙sync-time )
     44 		ifinstalled ntp || exit 1
     45 		notify-send -u low "🕙 Synchronizing Time..." "Synching time with remote NTP servers..."
     46 		updatedata="$(sudo ntpdate pool.ntp.org)" &&
     47 		notify-send -u low "🕙 Synchronizing Time..." "Done. Time changed by ${updatedata#*offset }"
     48 		;;
     49 	*) pass otp -c ${choice}-otp ;;
     50 esac