Overview
AgenticPal uses Google OAuth2 for authentication, allowing the agent to access user’s Google Calendar, Gmail, and Tasks. Sessions are managed with HttpOnly cookies and stored in Redis.Authentication Flow
OAuth Scopes
The API requests the following Google OAuth scopes:https://www.googleapis.com/auth/calendar - Full access to Google Calendarhttps://mail.google.com/ - Full access to Gmailhttps://www.googleapis.com/auth/tasks - Full access to Google Taskshttps://www.googleapis.com/auth/userinfo.email - Access user’s email addressopenid - OpenID Connect authenticationEndpoints
GET /auth/google/login
Initiate the Google OAuth2 login flow. Query Parameters:Optional URL to redirect to after successful authentication. Defaults to
FRONTEND_URL environment variable.The Google OAuth authorization URL to redirect the user to
- Sets a
session_idHttpOnly cookie - Generates a CSRF state token stored in Redis (10-minute TTL)
- Forces consent prompt to ensure refresh token is granted
- Session cookie expires in 7 days
GET /auth/google/callback
Handle the OAuth callback from Google. Query Parameters:Authorization code from Google
CSRF protection state token
Error code if authorization was denied
- Success:
302 Redirectto frontend URL with session cookie - Error:
400 Bad Requestor500 Internal Server Error
- Validates CSRF state parameter
- Exchanges authorization code for access and refresh tokens
- Retrieves user email from Google API
- Stores credentials in Redis with 7-day TTL
- Sets HttpOnly session cookie
- Redirects to frontend URL
GET /auth/session
Get information about the current session. Headers:Session cookie:
session_id=...Unique session identifier
User’s Google email address (if authenticated)
Whether the session has valid Google credentials
ISO 8601 timestamp of session creation
- Automatically refreshes expired tokens if refresh token is available
- Updates
last_accessedtimestamp on each request - Extends session TTL on access
POST /auth/refresh
Manually refresh Google OAuth credentials. Headers:Session cookie:
session_id=...POST /auth/logout
Log out the current user and clear session. Headers:Session cookie:
session_id=...- Deletes session from Redis
- Deletes stored credentials
- Clears session cookie
Session Management
Session Storage
Sessions are stored in Redis (db=0) with the following structure: Session Data:Session Lifecycle
Cookie Configuration
Session cookies are configured as follows:Prevents JavaScript access (XSS protection)
Enabled in production (HTTPS only)
CSRF protection
Cookie domain (configurable via
COOKIE_DOMAIN)Cookie expiration in seconds (7 days)
Security Considerations
CSRF Protection
CSRF Protection
OAuth state parameter provides CSRF protection during login flow. State tokens are:
- Randomly generated (cryptographically secure)
- Stored in Redis with 10-minute TTL
- Validated on callback
- Deleted after use
XSS Protection
XSS Protection
- Session cookies are HttpOnly (not accessible via JavaScript)
- Credentials never exposed to frontend
- All credential operations happen server-side
Token Refresh
Token Refresh
- Access tokens automatically refreshed when expired
- Refresh tokens stored securely in Redis
- Consent prompt forced to ensure refresh token is granted
Session Security
Session Security
- Sessions use cryptographically random IDs (32-byte URL-safe tokens)
- Redis persistence with configurable TTL
- Automatic expiration after inactivity
- Session secret required in production
Configuration
Required Setup
Google Cloud Project
Create a project in Google Cloud Console and enable Calendar, Gmail, and Tasks APIs
Environment Variables
Path to Google OAuth credentials JSON file
OAuth callback URL (must match Google Cloud Console configuration)
Frontend application URL for post-authentication redirects
Cookie domain (omit for localhost, set for production domains)
Secret key for session signing (generate with
openssl rand -hex 32)Session expiration time in seconds (default: 7 days)
OAuth state token expiration in seconds (default: 10 minutes)
Environment mode:
development or productionExample: Complete Authentication Flow
Next Steps
Chat Endpoints
Send messages and interact with the agent
API Overview
Learn about the API architecture