This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

superconfig / tools / pass-otp / flake-module.nix
3.6 kB 106 lines
1{ ... }: 2{ 3 perSystem = 4 { pkgs, lib, ... }: 5 { 6 packages.pass-otp = 7 (pkgs.writeShellApplication { 8 name = "pass-otp"; 9 runtimeInputs = [ 10 pkgs.oath-toolkit 11 ] 12 ++ lib.optionals (!pkgs.stdenv.isDarwin) [ 13 pkgs.xclip 14 pkgs.wl-clipboard 15 ]; 16 text = '' 17 set -euo pipefail 18 # Tool-agnostic OTP generator that accepts secret via stdin or args 19 20 show_usage() { 21 echo "Usage: pass-otp [--clip,-c] <secret>" 22 echo " echo <secret> | pass-otp [--clip,-c]" 23 echo "" 24 echo "Accepts OTP secret as argument or via stdin" 25 echo "Secret can be:" 26 echo " - Raw base32 secret" 27 echo " - otpauth:// URL" 28 echo " - Multi-line text with secret on line 2+" 29 } 30 31 clip=false 32 secret_input="" 33 34 # Parse arguments 35 while [[ $# -gt 0 ]]; do 36 case "$1" in 37 "--clip"|"-c") 38 clip=true 39 shift 40 ;; 41 "--help"|"-h") 42 show_usage 43 exit 0 44 ;; 45 *) 46 secret_input="$1" 47 shift 48 ;; 49 esac 50 done 51 52 # If no argument provided, read from stdin 53 if [[ -z "$secret_input" ]]; then 54 secret_input=$(cat) 55 fi 56 57 if [[ -z "$secret_input" ]]; then 58 echo "Error: No OTP secret provided" 59 show_usage 60 exit 1 61 fi 62 63 # Extract TOTP secret from input 64 if echo "$secret_input" | grep -q "^otpauth://"; then 65 # Extract secret from otpauth URL 66 secret_key=$(echo "$secret_input" | sed -n 's/.*secret=\([^&]*\).*/\1/p') 67 elif echo "$secret_input" | wc -l | grep -q "^1$"; then 68 # Single line - assume it's the raw secret 69 secret_key=$(echo "$secret_input" | tr -d ' ') 70 else 71 # Multi-line - assume secret is on line 2 or later 72 secret_key=$(echo "$secret_input" | tail -n +2 | head -n 1 | tr -d ' ') 73 fi 74 75 if [[ -z "$secret_key" ]]; then 76 echo "Error: Could not extract OTP secret from input" 77 exit 1 78 fi 79 80 # Generate OTP code 81 otp_code=$(oathtool --totp --base32 "$secret_key") 82 83 # Output or copy to clipboard 84 if [[ "$clip" == "true" ]]; then 85 if [[ "$(uname)" == "Darwin" ]]; then 86 echo -n "$otp_code" | pbcopy 87 echo "Copied OTP code to clipboard." 88 elif [[ -n "''${WAYLAND_DISPLAY:-}" ]] && command -v wl-copy >/dev/null 2>&1; then 89 echo -n "$otp_code" | wl-copy 90 echo "Copied OTP code to clipboard (Wayland)." 91 elif [[ -n "''${DISPLAY:-}" ]] && command -v xclip >/dev/null 2>&1; then 92 echo -n "$otp_code" | xclip -selection clipboard 93 echo "Copied OTP code to clipboard (X11)." 94 else 95 echo "Error: No clipboard tool found (tried pbcopy, wl-copy, xclip)" 96 echo "$otp_code" 97 exit 1 98 fi 99 else 100 echo "$otp_code" 101 fi 102 ''; 103 }).overrideAttrs 104 { passthru.usage = builtins.readFile ./usage.kdl; }; 105 }; 106}