Overview
Vega AI supports Google OAuth 2.0 for seamless authentication. Users can sign in with their Google accounts, eliminating the need to create and remember separate credentials.Google OAuth must be enabled in the application configuration. In cloud mode, this is the primary authentication method.
OAuth Flow
The Google OAuth authentication follows the standard OAuth 2.0 authorization code flow:Web Routes
Google OAuth is implemented through web routes (not direct API endpoints):Initiate OAuth Flow
- Generates a random state parameter for CSRF protection
- Stores the state in a secure, HTTP-only cookie
- Redirects to Google’s authorization URL
OAuth Callback
- Validates the state parameter to prevent CSRF attacks
- Exchanges the authorization code for user information
- Creates or fetches the user account
- Generates JWT access and refresh tokens
- Sets authentication cookies
- Redirects to the application dashboard
Implementation Details
The Google OAuth service (seeinternal/auth/services/oauth.go) performs the following:
1. Code Exchange
Exchanges the authorization code for an OAuth token:2. User Info Retrieval
Fetches user information from Google’s UserInfo API:3. User Creation/Lookup
- Looks up the user by email in the local database
- Creates a new user account if not found
- Uses the Google email as the username
- Sets the user role to
STANDARD
4. Token Generation
Generates JWT access and refresh tokens (same format as standard login):Configuration
Google OAuth requires the following environment variables:Google OAuth client ID from Google Cloud Console
Google OAuth client secret from Google Cloud Console
Authorized redirect URI (e.g.,
https://app.vega.ai/auth/google/callback)OAuth scope for user info (default:
https://www.googleapis.com/auth/userinfo.email)Security Features
CSRF Protection
CSRF Protection
The OAuth flow includes state parameter validation:
- A random 32-character state string is generated
- Stored in a secure, HTTP-only cookie with 5-minute expiry
- Validated on callback to ensure the request originated from the app
- Cookie is cleared after validation
Email Verification
Email Verification
Google’s UserInfo API returns an
verified_email flag. Only users with verified Google email addresses can authenticate.Secure Cookies
Secure Cookies
Token Rotation
Token Rotation
Refresh tokens are automatically rotated when used, ensuring that old tokens cannot be reused if compromised.
Integration Example
For web applications, create a “Sign in with Google” button:HTML
JavaScript
Error Handling
The callback endpoint handles various error scenarios:Invalid State
Missing Authorization Code
Authentication Failure
- Failed to exchange authorization code
- Failed to retrieve user information from Google
- Database error when creating/fetching user
- Token generation failure
Account Creation
When a user signs in with Google for the first time:- User Lookup - Checks if a user exists with the Google email
- Account Creation - Creates a new user account if not found:
- Username: Google email address
- Password: Empty (no password-based login)
- Role:
STANDARD - Last Login: Current timestamp
- Token Generation - Generates JWT tokens for the new user
- Redirect - Redirects to the application dashboard
Retrieving Tokens
After successful Google OAuth authentication, JWT tokens are available:Via Cookies (Web)
Tokens are automatically set in HTTP-only cookies:token- Access tokenrefresh_token- Refresh token
Via API (Mobile/Desktop)
For non-web clients, you’ll need to:- Implement a web view to handle the OAuth flow
- Intercept the callback URL
- Extract tokens from the response
- Store tokens securely in the native app
Next Steps
Refresh Token
Learn how to refresh expired access tokens
Standard Login
Alternative authentication with username/password