Overview
The OIDC package provides OpenID Connect authentication with PKCE (Proof Key for Code Exchange) support and Google Service Account integration. It implements the OAuth 2.0 authorization code flow with enhanced security.Token Source
NewTokenSource
Creates a new OAuth2 token source for interactive authentication with PKCE.Context for the authentication flow
OAuth2 configuration including:
ClientID- OAuth2 client identifierClientSecret- OAuth2 client secretRedirectURL- Callback URL for authorizationEndpoint- Authorization server endpointsScopes- OAuth2 scopes (openid scope added automatically)
Target audience for the token
Returns a token source that implements the OAuth2 TokenSource interface
Google Service Account
NewGoogleServiceAccountTokenSource
Creates a token source using Google Service Account credentials.Context for the token generation
Path to the service account JSON key file
Target audience for the ID token
Returns a token source for the service account
Error if the token source creation fails
CLI Integration
LoginCmd
Creates a Cobra command for interactive login flow.OAuth2 configuration for interactive login
Target audience for the token
Optional path to service account key file. If provided, uses service account authentication instead of interactive flow
Callback function invoked with the token or error
Returns a Cobra command for the login flow
Authentication Flow
The OIDC authentication flow follows these steps:Interactive Flow
- Initialize Token Source: Create a token source with OAuth2 configuration
- Generate PKCE Parameters: Generate code verifier and challenge (S256 method)
- Authorization Request: Open browser with authorization URL including:
- State parameter for CSRF protection
- PKCE code challenge
- Audience parameter
- OpenID scope
- User Authorization: User authenticates and grants consent
- Callback Handling: Local HTTP server receives authorization code
- Token Exchange: Exchange authorization code for tokens with:
- Code verifier for PKCE verification
- Audience parameter
- ID Token Extraction: Extract ID token from response and set as access token
Service Account Flow
- Load Credentials: Read service account JSON key file
- Generate Token: Request ID token with specified audience
- Return Token Source: Return token source for automatic token refresh
Security Features
PKCE (RFC 7636)
- Code Verifier: 32-byte random string encoded with base64url
- Code Challenge: SHA256 hash of verifier, base64url encoded
- Method: S256 (SHA256 hashing)
- Protection: Prevents authorization code interception attacks
State Parameter
- 10-byte random value for CSRF protection
- Validated on callback to ensure request integrity
Secure Random Generation
- Uses
crypto/randfor cryptographically secure random bytes - Charset:
[A-Za-z0-9](62 characters) - Avoids bias through rejection sampling
Error Handling
State parameter received in callback doesn’t match the sent value
ID token not found in the OAuth2 token response
Authorization code missing from callback
State parameter missing from callback
Error returned by the OAuth2 provider with error code and description
Constants
OpenID Connect scope (“openid”)
OAuth2 parameter key for audience
PKCE code challenge method (“S256”)
Length of PKCE code verifier (32 bytes)
Browser Integration
The package automatically opens the system default browser for authorization:- Windows: Uses
cmd /c start - macOS: Uses
opencommand - Linux/BSD: Uses
xdg-open
Callback Response
After successful authentication, users see a success page served from an embedded HTML template (redirect.html). The server automatically closes after receiving the callback.