Complete OAuth 2.1 Flow
Auth-Agent implements the OAuth 2.1 Authorization Code Flow with PKCE (Proof Key for Code Exchange). Here’s how it works:Step-by-Step Breakdown
1
1. Generate Login URL
When user clicks sign-in, the SDK generates a PKCE code verifier and challenge:Then constructs the authorization URL with the challenge.
2
2. Redirect to Auth Server
Browser redirects to:
The
state parameter protects against CSRF attacks.3
3. User Authenticates
The Auth-Agent server displays an authentication page where the user enters:
- Agent ID: Their registered agent identifier
- Agent Secret: Their secure password
- Model Name: The AI model being used
4
4. Generate Authorization Code
If authentication succeeds:
- Server creates an auth session
- Stores the PKCE
code_challenge - Generates a one-time
authorization_code - Redirects back to your app
5
5. Exchange Code for Tokens
Your app’s callback handler receives the code and exchanges it for tokens:
The authorization code can only be used once and expires in 10 minutes.
6
6. Verify PKCE
Server verifies the PKCE proof:This proves the same client that started the flow is completing it.
7
8
8. Store Tokens
SDK stores tokens securely:
9
9. Fetch User Info
SDK makes an authenticated request to get user details:Response:
10
10. Schedule Auto-Refresh
SDK schedules automatic token refresh 5 minutes before expiration:
Token Refresh Flow
When the access token is about to expire:Refresh Request
Refresh Response
Refresh tokens are NOT rotated by default. The same refresh token can be used
multiple times until it expires.
Security Features
PKCE (Proof Key for Code Exchange)
PKCE (Proof Key for Code Exchange)
PKCE prevents authorization code interception attacks:
- Attacker cannot use a stolen code without the verifier
- Even if code is intercepted, it’s useless without the PKCE verifier
- Verifier never leaves the client, only challenge is sent
State Parameter
State Parameter
Protects against CSRF (Cross-Site Request Forgery) attacks: - Random state
generated for each request - Stored in sessionStorage - Verified when handling
callback - Prevents attackers from initiating unauthorized auth flows
Secure Token Storage
Secure Token Storage
Tokens are stored securely: -
localStorage: Persistent across browser
sessions - sessionStorage: Cleared when tab/browser closes - memory: Only
in JavaScript memory (most secure but not persistent) Note: Even with
localStorage, tokens are HttpOnly-equivalent because they’re only accessible
to your origin.Token Expiration
Token Expiration
Tokens have limited lifetimes: - Access tokens: 1 hour - Refresh tokens: 30
days - Authorization codes: 10 minutes - Auth sessions: 10 minutes (for
challenge completion) Automatic refresh keeps users logged in without
re-authentication.
Redirect URI Validation
Redirect URI Validation
Prevents open redirect vulnerabilities:
- Redirect URI must exactly match registered URI
- Protocol, domain, port, and path must all match
- No wildcard or pattern matching
Error Scenarios
- Invalid Credentials
- PKCE Verification Failed
- Expired Code
- Redirect URI Mismatch