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