|
| 1 | +# Authentication Patterns |
| 2 | + |
| 3 | +Login flows, session persistence, OAuth, 2FA, and authenticated browsing. |
| 4 | + |
| 5 | +**Related**: [session-management.md](session-management.md) for state persistence details, [SKILL.md](../SKILL.md) for quick start. |
| 6 | + |
| 7 | +## Contents |
| 8 | + |
| 9 | +- [Basic Login Flow](#basic-login-flow) |
| 10 | +- [Saving Authentication State](#saving-authentication-state) |
| 11 | +- [Restoring Authentication](#restoring-authentication) |
| 12 | +- [OAuth / SSO Flows](#oauth--sso-flows) |
| 13 | +- [Two-Factor Authentication](#two-factor-authentication) |
| 14 | +- [HTTP Basic Auth](#http-basic-auth) |
| 15 | +- [Cookie-Based Auth](#cookie-based-auth) |
| 16 | +- [Token Refresh Handling](#token-refresh-handling) |
| 17 | +- [Security Best Practices](#security-best-practices) |
| 18 | + |
| 19 | +## Basic Login Flow |
| 20 | + |
| 21 | +```bash |
| 22 | +# Navigate to login page |
| 23 | +agent-browser open https://app.example.com/login |
| 24 | +agent-browser wait --load networkidle |
| 25 | + |
| 26 | +# Get form elements |
| 27 | +agent-browser snapshot -i |
| 28 | +# Output: @e1 [input type="email"], @e2 [input type="password"], @e3 [button] "Sign In" |
| 29 | + |
| 30 | +# Fill credentials |
| 31 | +agent-browser fill @e1 "user@example.com" |
| 32 | +agent-browser fill @e2 "password123" |
| 33 | + |
| 34 | +# Submit |
| 35 | +agent-browser click @e3 |
| 36 | +agent-browser wait --load networkidle |
| 37 | + |
| 38 | +# Verify login succeeded |
| 39 | +agent-browser get url # Should be dashboard, not login |
| 40 | +``` |
| 41 | + |
| 42 | +## Saving Authentication State |
| 43 | + |
| 44 | +After logging in, save state for reuse: |
| 45 | + |
| 46 | +```bash |
| 47 | +# Login first (see above) |
| 48 | +agent-browser open https://app.example.com/login |
| 49 | +agent-browser snapshot -i |
| 50 | +agent-browser fill @e1 "user@example.com" |
| 51 | +agent-browser fill @e2 "password123" |
| 52 | +agent-browser click @e3 |
| 53 | +agent-browser wait --url "**/dashboard" |
| 54 | + |
| 55 | +# Save authenticated state |
| 56 | +agent-browser state save ./auth-state.json |
| 57 | +``` |
| 58 | + |
| 59 | +## Restoring Authentication |
| 60 | + |
| 61 | +Skip login by loading saved state: |
| 62 | + |
| 63 | +```bash |
| 64 | +# Load saved auth state |
| 65 | +agent-browser state load ./auth-state.json |
| 66 | + |
| 67 | +# Navigate directly to protected page |
| 68 | +agent-browser open https://app.example.com/dashboard |
| 69 | + |
| 70 | +# Verify authenticated |
| 71 | +agent-browser snapshot -i |
| 72 | +``` |
| 73 | + |
| 74 | +## OAuth / SSO Flows |
| 75 | + |
| 76 | +For OAuth redirects: |
| 77 | + |
| 78 | +```bash |
| 79 | +# Start OAuth flow |
| 80 | +agent-browser open https://app.example.com/auth/google |
| 81 | + |
| 82 | +# Handle redirects automatically |
| 83 | +agent-browser wait --url "**/accounts.google.com**" |
| 84 | +agent-browser snapshot -i |
| 85 | + |
| 86 | +# Fill Google credentials |
| 87 | +agent-browser fill @e1 "user@gmail.com" |
| 88 | +agent-browser click @e2 # Next button |
| 89 | +agent-browser wait 2000 |
| 90 | +agent-browser snapshot -i |
| 91 | +agent-browser fill @e3 "password" |
| 92 | +agent-browser click @e4 # Sign in |
| 93 | + |
| 94 | +# Wait for redirect back |
| 95 | +agent-browser wait --url "**/app.example.com**" |
| 96 | +agent-browser state save ./oauth-state.json |
| 97 | +``` |
| 98 | + |
| 99 | +## Two-Factor Authentication |
| 100 | + |
| 101 | +Handle 2FA with manual intervention: |
| 102 | + |
| 103 | +```bash |
| 104 | +# Login with credentials |
| 105 | +agent-browser open https://app.example.com/login --headed # Show browser |
| 106 | +agent-browser snapshot -i |
| 107 | +agent-browser fill @e1 "user@example.com" |
| 108 | +agent-browser fill @e2 "password123" |
| 109 | +agent-browser click @e3 |
| 110 | + |
| 111 | +# Wait for user to complete 2FA manually |
| 112 | +echo "Complete 2FA in the browser window..." |
| 113 | +agent-browser wait --url "**/dashboard" --timeout 120000 |
| 114 | + |
| 115 | +# Save state after 2FA |
| 116 | +agent-browser state save ./2fa-state.json |
| 117 | +``` |
| 118 | + |
| 119 | +## HTTP Basic Auth |
| 120 | + |
| 121 | +For sites using HTTP Basic Authentication: |
| 122 | + |
| 123 | +```bash |
| 124 | +# Set credentials before navigation |
| 125 | +agent-browser set credentials username password |
| 126 | + |
| 127 | +# Navigate to protected resource |
| 128 | +agent-browser open https://protected.example.com/api |
| 129 | +``` |
| 130 | + |
| 131 | +## Cookie-Based Auth |
| 132 | + |
| 133 | +Manually set authentication cookies: |
| 134 | + |
| 135 | +```bash |
| 136 | +# Set auth cookie |
| 137 | +agent-browser cookies set session_token "abc123xyz" |
| 138 | + |
| 139 | +# Navigate to protected page |
| 140 | +agent-browser open https://app.example.com/dashboard |
| 141 | +``` |
| 142 | + |
| 143 | +## Token Refresh Handling |
| 144 | + |
| 145 | +For sessions with expiring tokens: |
| 146 | + |
| 147 | +```bash |
| 148 | +#!/bin/bash |
| 149 | +# Wrapper that handles token refresh |
| 150 | + |
| 151 | +STATE_FILE="./auth-state.json" |
| 152 | + |
| 153 | +# Try loading existing state |
| 154 | +if [[ -f "$STATE_FILE" ]]; then |
| 155 | + agent-browser state load "$STATE_FILE" |
| 156 | + agent-browser open https://app.example.com/dashboard |
| 157 | + |
| 158 | + # Check if session is still valid |
| 159 | + URL=$(agent-browser get url) |
| 160 | + if [[ "$URL" == *"/login"* ]]; then |
| 161 | + echo "Session expired, re-authenticating..." |
| 162 | + # Perform fresh login |
| 163 | + agent-browser snapshot -i |
| 164 | + agent-browser fill @e1 "$USERNAME" |
| 165 | + agent-browser fill @e2 "$PASSWORD" |
| 166 | + agent-browser click @e3 |
| 167 | + agent-browser wait --url "**/dashboard" |
| 168 | + agent-browser state save "$STATE_FILE" |
| 169 | + fi |
| 170 | +else |
| 171 | + # First-time login |
| 172 | + agent-browser open https://app.example.com/login |
| 173 | + # ... login flow ... |
| 174 | +fi |
| 175 | +``` |
| 176 | + |
| 177 | +## Security Best Practices |
| 178 | + |
| 179 | +1. **Never commit state files** - They contain session tokens |
| 180 | + ```bash |
| 181 | + echo "*.auth-state.json" >> .gitignore |
| 182 | + ``` |
| 183 | + |
| 184 | +2. **Use environment variables for credentials** |
| 185 | + ```bash |
| 186 | + agent-browser fill @e1 "$APP_USERNAME" |
| 187 | + agent-browser fill @e2 "$APP_PASSWORD" |
| 188 | + ``` |
| 189 | + |
| 190 | +3. **Clean up after automation** |
| 191 | + ```bash |
| 192 | + agent-browser cookies clear |
| 193 | + rm -f ./auth-state.json |
| 194 | + ``` |
| 195 | + |
| 196 | +4. **Use short-lived sessions for CI/CD** |
| 197 | + ```bash |
| 198 | + # Don't persist state in CI |
| 199 | + agent-browser open https://app.example.com/login |
| 200 | + # ... login and perform actions ... |
| 201 | + agent-browser close # Session ends, nothing persisted |
| 202 | + ``` |
0 commit comments