This repository has no description
2.7 kB
83 lines
1#!/usr/bin/env bash
2set -euo pipefail
3
4# age-detect: Detects connected age-compatible security keys
5# Returns identity file path and detected key information
6
7# Function to detect connected security keys and their age identities
8detect_age_keys() {
9 local temp_identities
10 temp_identities=$(mktemp)
11
12 # Check for Secure Enclave on macOS (highest priority)
13 if [[ "$(uname)" == "Darwin" ]] && command -v age-plugin-se >/dev/null 2>&1; then
14 echo "AGE-PLUGIN-SE-15FM0MYMDXM39K8EKG0TNG" > "$temp_identities"
15 echo "IDENTITY_FILE=$temp_identities"
16 echo "DETECTED_KEY=se"
17 echo "KEY_TYPE=se"
18 return
19 fi
20
21 # Check for YubiKeys (second priority)
22 if command -v ykman >/dev/null 2>&1; then
23 local yubikey_serial
24 yubikey_serial=$(ykman info 2>/dev/null | grep "Serial number:" | cut -d: -f2 | tr -d ' ' || true)
25 if [[ -n "$yubikey_serial" ]]; then
26 echo "AGE-PLUGIN-YUBIKEY-1M2PLUQYZ242EPUSSHDFWJ" > "$temp_identities"
27 echo "IDENTITY_FILE=$temp_identities"
28 case "$yubikey_serial" in
29 "16679898")
30 echo "DETECTED_KEY=yubi1"
31 ;;
32 "11342136")
33 echo "DETECTED_KEY=yubi2"
34 ;;
35 *)
36 echo "DETECTED_KEY=yubikey-${yubikey_serial}"
37 ;;
38 esac
39 echo "KEY_TYPE=yubikey"
40 return
41 fi
42 fi
43
44 # Check for Solo2 (lowest priority)
45 if command -v fido2-token >/dev/null 2>&1; then
46 if fido2-token -L 2>/dev/null | grep -q "Solo 2"; then
47 # FIDO2-HMAC doesn't use identity file
48 rm -f "$temp_identities"
49 echo "IDENTITY_FILE="
50 echo "DETECTED_KEY=solo2"
51 echo "KEY_TYPE=solo2"
52 return
53 fi
54 fi
55
56 # No keys detected
57 rm -f "$temp_identities"
58 echo "IDENTITY_FILE="
59 echo "DETECTED_KEY="
60 echo "KEY_TYPE="
61}
62
63# Parse command line arguments
64case "${1:-detect}" in
65 detect)
66 detect_age_keys
67 ;;
68 --help|-h)
69 echo "Usage: age-detect [detect]"
70 echo ""
71 echo "Detects the first available age-compatible security key and outputs:"
72 echo " IDENTITY_FILE=<path> - Path to temporary identity file for age (empty for Solo2)"
73 echo " DETECTED_KEY=<name> - Name of the detected key (e.g., yubi1, solo2, se)"
74 echo " KEY_TYPE=<type> - Type of key (yubikey, solo2, se)"
75 echo ""
76 echo "Priority order: se > yubikey > solo2"
77 ;;
78 *)
79 echo "Unknown command: $1" >&2
80 echo "Use --help for usage information" >&2
81 exit 1
82 ;;
83esac