bw-login.sh 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/bin/bash
  2. # filepath: /Users/kevin_heinicke/repos/apollo/bw-login.sh
  3. # Bitwarden CLI Login Manager with macOS Keychain Integration
  4. # Usage: ./bw-login.sh
  5. set -e
  6. KEYCHAIN_SERVICE="bitwarden-cli"
  7. KEYCHAIN_ACCOUNT="session-key"
  8. # Function to check if already logged in
  9. check_login_status() {
  10. bw status | grep -q '"status":"unlocked"'
  11. }
  12. # Function to get session key from keychain
  13. get_session_from_keychain() {
  14. security find-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT" -w 2>/dev/null || echo ""
  15. }
  16. # Function to store session key in keychain
  17. store_session_in_keychain() {
  18. local session_key="$1"
  19. # Delete existing entry if it exists
  20. security delete-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT" 2>/dev/null || true
  21. # Add new entry
  22. security add-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT" -w "$session_key"
  23. }
  24. # Function to validate session key
  25. validate_session() {
  26. local session_key="$1"
  27. BW_SESSION="$session_key" bw status | grep -q '"status":"unlocked"'
  28. }
  29. # Main logic
  30. main() {
  31. echo "Checking Bitwarden CLI login status..."
  32. # Check if already logged in and unlocked
  33. if check_login_status; then
  34. echo "✅ Already logged in and unlocked"
  35. return 0
  36. fi
  37. # Try to get session from keychain
  38. stored_session=$(get_session_from_keychain)
  39. if [ -n "$stored_session" ]; then
  40. echo "Found stored session, validating..."
  41. if validate_session "$stored_session"; then
  42. echo "✅ Using stored session from keychain"
  43. export BW_SESSION="$stored_session"
  44. echo "Session exported as BW_SESSION environment variable"
  45. return 0
  46. else
  47. echo "⚠️ Stored session is invalid, removing from keychain"
  48. security delete-generic-password -s "$KEYCHAIN_SERVICE" -a "$KEYCHAIN_ACCOUNT" 2>/dev/null || true
  49. fi
  50. fi
  51. # Need to login
  52. echo "Logging in to Bitwarden..."
  53. # Check if we need to login to the server first
  54. if bw status | grep -q '"status":"unauthenticated"'; then
  55. echo "Please enter your Bitwarden credentials:"
  56. bw login
  57. fi
  58. # Unlock the vault
  59. echo "Unlocking vault..."
  60. session_key=$(bw unlock --raw)
  61. if [ $? -eq 0 ] && [ -n "$session_key" ]; then
  62. echo "✅ Successfully logged in"
  63. # Store session in keychain
  64. store_session_in_keychain "$session_key"
  65. echo "✅ Session stored in macOS Keychain"
  66. # Export session for current shell
  67. export BW_SESSION="$session_key"
  68. echo "✅ Session exported as BW_SESSION environment variable"
  69. echo ""
  70. echo "To use this session in your current shell, run:"
  71. echo "export BW_SESSION=\"$session_key\""
  72. else
  73. echo "❌ Failed to login or unlock vault"
  74. exit 1
  75. fi
  76. }
  77. main "$@"