Overview
Salt provides a complete OpenID Connect (OIDC) authentication implementation with built-in support for PKCE (Proof Key for Code Exchange), browser-based authentication flows, and Google Service Account integration.Key Features
- PKCE Flow: RFC 7636 compliant implementation for enhanced security
- Browser Authentication: Automatic browser-based OAuth2 flows
- Service Account Support: Google Service Account token generation
- Token Management: Automatic token exchange and ID token extraction
Token Source
NewTokenSource
Creates a new OIDC token source with PKCE support.ctx- Context for the authentication flowconf- OAuth2 configuration with client credentialsaudience- OIDC audience claim value
oauth2.TokenSource that handles the complete authentication flow
Example: Basic OIDC Authentication
Google Service Account
NewGoogleServiceAccountTokenSource
Creates a token source using Google Service Account credentials.ctx- Context for token generationkeyFile- Path to service account JSON key fileaud- Target audience for the token
Example: Service Account Authentication
CLI Integration
LoginCmd
Provides a Cobra command for CLI-based authentication.cfg- OAuth2 configurationaud- OIDC audiencekeyFilePath- Optional service account key file pathonTokenOrErr- Callback function to handle token or error
Example: CLI Login Command
Authentication Flow
The OIDC implementation follows these steps:1. PKCE Parameter Generation
2. Authorization Request
Opens browser to provider’s authorization endpoint with:- State parameter for CSRF protection
- PKCE challenge parameters
- Audience claim
- OpenID scope
3. Callback Handling
Starts local HTTP server to receive the authorization code:- Validates state parameter
- Extracts authorization code
- Displays success page in browser
4. Token Exchange
Exchanges authorization code for tokens:- Includes PKCE code verifier
- Receives access token, refresh token, and ID token
- Returns ID token as the primary access token
Configuration
OAuth2 Config Structure
Environment Variables
Security Features
PKCE (RFC 7636)
Prevents authorization code interception attacks:- Uses SHA256 code challenge method
- Generates cryptographically random verifiers
- No base64 padding for URL safety
State Parameter
Protects against CSRF attacks:- Random 10-byte state value
- Validated on callback
- Authentication fails if state mismatches
Secure Token Handling
Fromsource_oidc.go:76-80:
Error Handling
Best Practices
- Use HTTPS in Production: Always use HTTPS redirect URLs in production
- Secure Credentials: Never hardcode client secrets; use environment variables
- Token Refresh: Implement token refresh logic for long-running applications
- Timeout Handling: Set appropriate context timeouts for authentication flows
- Error Recovery: Provide clear error messages and recovery options
References
- Source:
~/workspace/source/auth/oidc/source_oidc.go - RFC 7636: PKCE specification
- OpenID Connect: Core 1.0 specification