Documentation
¶
Overview ¶
Package dex implements the OAuth provider interface for Dex (https://dexidp.io/). It supports OIDC authentication with Dex-specific optimizations including connector_id support for bypassing the connector selection UI and proper handling of refresh token rotation.
Package dex provides a Dex OAuth provider implementation with OIDC support.
Dex (https://dexidp.io/) is an identity service that uses OpenID Connect to drive authentication for other apps. It acts as a portal to other identity providers through "connectors" like LDAP, SAML, GitHub, GitLab, Google, etc.
Features ¶
This package implements Dex-specific optimizations beyond generic OIDC:
- connector_id Support: Bypass Dex's connector selection UI by specifying a connector
- Groups Claim: Automatically includes the 'groups' scope to retrieve user group memberships
- Refresh Token Rotation: Properly handles Dex's strict refresh token rotation policy
- OIDC Discovery: Dynamically fetches endpoints via OIDC discovery with SSRF protection
Security Features ¶
- SSRF Protection: Validates issuer URLs to block private IPs and localhost
- HTTPS Enforcement: All endpoints must use HTTPS
- Input Validation: Validates connector_id and groups claim for security
- Discovery Caching: Caches OIDC discovery documents with TTL
Example Usage ¶
// Create Dex provider with connector_id to skip selection UI
dexProvider, err := dex.NewProvider(&dex.Config{
IssuerURL: "https://dex.example.com",
ClientID: "my-client-id",
ClientSecret: "my-client-secret",
RedirectURL: "http://localhost:8080/oauth/callback",
ConnectorID: "github", // Optional: skip connector selection
})
if err != nil {
log.Fatal(err)
}
// Use with mcp-oauth server
server, err := mcpoauth.NewServer(&mcpoauth.ServerConfig{
Provider: dexProvider,
// ... other config ...
})
Dex-Specific Configuration ¶
Connector ID:
The connector_id parameter allows bypassing Dex's connector selection screen. This is useful when you have a single connector or want to direct users to a specific authentication method. Reference: https://dexidp.io/docs/configuration/custom-scopes-claims-clients/#authentication-through-connector_id
Groups Claim:
Dex requires the 'groups' scope to return group memberships in the userinfo response. This provider includes 'groups' in the default scopes automatically. Groups are validated for security (max 100 groups, max 256 chars per group name).
Refresh Token Rotation:
Dex implements strict refresh token rotation - each token refresh returns a NEW refresh token and invalidates the old one. This provider handles rotation correctly by returning the complete oauth2.Token with the new refresh token. Reference: https://dexidp.io/docs/configuration/custom-scopes-claims-clients/#refresh-token-rotation
Default Scopes ¶
The provider uses the following default scopes if none are specified:
- openid: Required for OIDC authentication
- profile: User profile information (name, picture, etc.)
- email: User email address
- groups: User group memberships (Dex-specific)
- offline_access: Refresh token support
You can override these by providing custom Scopes in the Config.
Index ¶
- type Config
- type Provider
- func (p *Provider) AuthorizationURL(state string, codeChallenge string, codeChallengeMethod string, ...) string
- func (p *Provider) DefaultScopes() []string
- func (p *Provider) ExchangeCode(ctx context.Context, code string, verifier string) (*oauth2.Token, error)
- func (p *Provider) HealthCheck(ctx context.Context) error
- func (p *Provider) Name() string
- func (p *Provider) RefreshToken(ctx context.Context, refreshToken string) (*oauth2.Token, error)
- func (p *Provider) RevokeToken(ctx context.Context, token string) error
- func (p *Provider) ValidateToken(ctx context.Context, accessToken string) (*providers.UserInfo, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// IssuerURL is the Dex issuer URL (e.g., https://dex.example.com)
IssuerURL string
// ClientID is the OAuth client ID
ClientID string
// ClientSecret is the OAuth client secret
ClientSecret string
// RedirectURL is the OAuth redirect URL
RedirectURL string
// ConnectorID is the optional Dex connector to use (e.g., "github", "ldap")
// When set, bypasses the Dex connector selection UI
ConnectorID string
// Scopes are optional custom scopes (defaults to Dex-optimized scopes if empty)
// Default: ["openid", "profile", "email", "groups", "offline_access"]
Scopes []string
// HTTPClient is an optional custom HTTP client
HTTPClient *http.Client
// RequestTimeout is the timeout for provider API calls (default: 30s)
RequestTimeout time.Duration
// contains filtered or unexported fields
}
Config holds Dex OAuth configuration
type Provider ¶
Provider implements the providers.Provider interface for Dex OAuth. It uses OIDC discovery to fetch endpoints dynamically and supports Dex-specific features like connector_id parameter and groups claim.
func NewProvider ¶
NewProvider creates a new Dex OAuth provider. It performs OIDC discovery to fetch authorization and token endpoints.
func (*Provider) AuthorizationURL ¶
func (p *Provider) AuthorizationURL(state string, codeChallenge string, codeChallengeMethod string, scopes []string) string
AuthorizationURL generates the Dex OAuth authorization URL with PKCE support. If connector_id is configured, it appends the parameter to bypass Dex's connector selection UI. If scopes is empty, the provider's default configured scopes are used.
func (*Provider) DefaultScopes ¶
DefaultScopes returns the provider's configured default scopes. Returns a deep copy to prevent external modification.
func (*Provider) ExchangeCode ¶
func (p *Provider) ExchangeCode(ctx context.Context, code string, verifier string) (*oauth2.Token, error)
ExchangeCode exchanges an authorization code for tokens with PKCE verification. Returns standard oauth2.Token.
func (*Provider) HealthCheck ¶
HealthCheck verifies that the Dex OIDC discovery endpoint is reachable. It performs a lightweight check by fetching the OpenID Connect discovery document.
Security Considerations:
- This method is designed for server-side health monitoring (k8s probes, monitoring systems)
- DO NOT expose the returned error messages directly to untrusted clients
- Error messages may contain HTTP status codes that could leak provider state information
- For public health endpoints, return generic "healthy/unhealthy" status only
func (*Provider) RefreshToken ¶
RefreshToken refreshes an expired token using a refresh token. CRITICAL: Dex implements refresh token rotation - it returns a NEW refresh token on every refresh operation. The oauth2 library automatically captures this new token. Returns standard oauth2.Token with the new refresh token.
func (*Provider) RevokeToken ¶
RevokeToken revokes a token at Dex's revocation endpoint if available. Gracefully degrades if revocation endpoint is not supported.