- Add rbw integration to retrieve credentials from Bitwarden vault - Automate email/password entry with Selenium WebDriver - Handle 2FA by falling back to manual completion - Add clipboard support for Wayland (wl-copy) and X11 (xclip) - Add CLI flags: --entry, --no-copy, --manual - Add DESIGN.md documenting the implementation approach
4.2 KiB
4.2 KiB
Design: Automated Google Login with rbw
Overview
Automate the Google login flow for cookie extraction, using rbw (Bitwarden CLI) for credential lookup.
Current State
selenium_cookie_extractor_json.pyopens Chrome incognito, navigates to chat.google.com- User manually logs in, presses Enter
- Script extracts COMPASS, SSID, SID, OSID, HSID cookies and outputs JSON
Proposed Changes
1. rbw Integration
def check_rbw_unlocked() -> bool:
"""Check if rbw vault is unlocked."""
result = subprocess.run(['rbw', 'unlocked'], capture_output=True)
return result.returncode == 0
def prompt_rbw_unlock():
"""Prompt user to unlock rbw vault."""
print("rbw vault is locked. Please unlock it.")
subprocess.run(['rbw', 'unlock'], check=True)
def get_google_credentials(entry_name: str = "google.com") -> tuple[str, str]:
"""Get username and password from rbw."""
username = subprocess.run(
['rbw', 'get', '-f', 'username', entry_name],
capture_output=True, text=True, check=True
).stdout.strip()
password = subprocess.run(
['rbw', 'get', entry_name],
capture_output=True, text=True, check=True
).stdout.strip()
return username, password
2. Automated Login Flow
Google's login has multiple steps:
- Email page: Enter email, click Next
- Password page: Enter password, click Next
- Potential 2FA: May require manual intervention
def automate_login(driver, username: str, password: str):
"""Automate Google login flow."""
driver.get("https://accounts.google.com/signin")
# Enter email
email_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "identifierId"))
)
email_input.send_keys(username)
email_input.send_keys(Keys.RETURN)
# Wait for password page and enter password
password_input = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "Passwd"))
)
password_input.send_keys(password)
password_input.send_keys(Keys.RETURN)
# Wait for successful login (presence of chat page or 2FA prompt)
# If 2FA required, prompt user to complete it manually
3. Clipboard Support
Add wl-clipboard and xclip to nix dependencies. Try Wayland first, fall back to X11:
def copy_to_clipboard(text: str) -> bool:
"""Copy text to clipboard. Tries wl-copy (Wayland) first, falls back to xclip (X11)."""
try:
subprocess.run(['wl-copy'], input=text.encode(), check=True)
return True
except FileNotFoundError:
pass
# Fall back to xclip (X11)
process = subprocess.Popen(['xclip', '-selection', 'clipboard'], stdin=subprocess.PIPE)
process.communicate(text.encode())
return process.returncode == 0
4. CLI Interface
Usage: gcr [OPTIONS]
Options:
--copy, -c Copy cookie JSON to clipboard (default: true)
--entry NAME rbw entry name (default: "google.com")
--no-auto Skip auto-login, use manual flow
--help Show this message
Implementation Steps
- Add new dependencies to
flake.nix:xclip - Add selenium wait helpers:
WebDriverWait,expected_conditions - Implement rbw functions: check unlocked, prompt unlock, get credentials
- Implement automated login: email step, password step, 2FA detection
- Implement clipboard copy
- Add CLI argument parsing with argparse
- Update main flow to use automation by default
Edge Cases
- rbw vault locked: Prompt to unlock, fail gracefully if user cancels
- Wrong credentials in rbw: Let login fail, user can retry manually
- 2FA required: Detect 2FA page, prompt user to complete manually, then continue
- Login timeout: Add reasonable timeouts with clear error messages
- Multiple Google accounts: Use
--entryflag to specify which rbw entry
Security Considerations
- Credentials are retrieved from rbw, never stored in script
- Incognito mode prevents cookie persistence
- Browser is closed promptly after extraction
Testing Plan
- Test with unlocked vault
- Test with locked vault (unlock prompt)
- Test with invalid rbw entry name
- Test full login flow (requires real credentials)
- Test 2FA flow (manual completion)
- Test clipboard copy