Documentation
¶
Overview ¶
Package provider defines the contracts and models for OAuth 2.0 / OIDC identity providers.
Index ¶
- type AuthCodeConfig
- type AuthCodeOption
- func WithAccessType(accessType string) AuthCodeOption
- func WithExtraParam(key, value string) AuthCodeOption
- func WithPKCE(challenge, method string) AuthCodeOption
- func WithPrompt(prompt string) AuthCodeOption
- func WithRedirectURI(uri string) AuthCodeOption
- func WithScopes(scopes ...string) AuthCodeOption
- type ExchangeConfig
- type ExchangeOption
- type GenericConfig
- type GenericProvider
- func NewDiscordProvider(cfg ProviderCommonConfig) (*GenericProvider, error)
- func NewGenericProvider(cfg GenericConfig) (*GenericProvider, error)
- func NewGitHubProvider(cfg ProviderCommonConfig) (*GenericProvider, error)
- func NewGitLabProvider(baseURL string, cfg ProviderCommonConfig) (*GenericProvider, error)
- func NewGoogleProvider(cfg ProviderCommonConfig) (*GenericProvider, error)
- func NewMicrosoftProvider(tenant string, cfg ProviderCommonConfig) (*GenericProvider, error)
- func (p *GenericProvider) AuthCodeURL(state string, opts ...AuthCodeOption) string
- func (p *GenericProvider) Exchange(ctx context.Context, code string, opts ...ExchangeOption) (*TokenResponse, error)
- func (p *GenericProvider) FetchProfile(ctx context.Context, tokens *TokenResponse) (*ProviderProfile, error)
- func (p *GenericProvider) Name() string
- func (p *GenericProvider) ValidateRedirectURI(redirectURI string) bool
- type HTTPClient
- type ProfileExtractorFunc
- type Provider
- type ProviderCommonConfig
- type ProviderProfile
- type TokenResponse
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthCodeConfig ¶
type AuthCodeConfig struct {
RedirectURI string
Scopes []string
CodeChallenge string
CodeChallengeMethod string
Prompt string
AccessType string
ExtraParams map[string]string
}
AuthCodeConfig holds parameters for constructing the authorization URL.
type AuthCodeOption ¶
type AuthCodeOption func(*AuthCodeConfig)
AuthCodeOption customizes the authorization code URL request.
func WithAccessType ¶
func WithAccessType(accessType string) AuthCodeOption
WithAccessType sets the access_type parameter (e.g., "offline" for refresh tokens).
func WithExtraParam ¶
func WithExtraParam(key, value string) AuthCodeOption
WithExtraParam adds an arbitrary query parameter to the authorization URL.
func WithPKCE ¶
func WithPKCE(challenge, method string) AuthCodeOption
WithPKCE sets the PKCE code challenge and method in the authorization URL.
func WithPrompt ¶
func WithPrompt(prompt string) AuthCodeOption
WithPrompt sets the prompt parameter (e.g., "consent", "select_account").
func WithRedirectURI ¶
func WithRedirectURI(uri string) AuthCodeOption
WithRedirectURI sets the redirect URI.
func WithScopes ¶
func WithScopes(scopes ...string) AuthCodeOption
WithScopes overrides or appends scopes.
type ExchangeConfig ¶
ExchangeConfig holds parameters for token exchange.
type ExchangeOption ¶
type ExchangeOption func(*ExchangeConfig)
ExchangeOption customizes the token exchange request.
func WithCodeVerifier ¶
func WithCodeVerifier(verifier string) ExchangeOption
WithCodeVerifier passes the PKCE code verifier for token exchange.
func WithExchangeExtraParam ¶
func WithExchangeExtraParam(key, value string) ExchangeOption
WithExchangeExtraParam passes extra form parameters during token exchange.
func WithExchangeRedirectURI ¶
func WithExchangeRedirectURI(uri string) ExchangeOption
WithExchangeRedirectURI specifies the redirect URI used during authorization.
type GenericConfig ¶
type GenericConfig struct {
// Name uniquely identifies the provider (e.g. "google", "github").
Name string
// ClientID is the public OAuth client identifier issued by the provider.
ClientID string
// ClientSecret is the confidential OAuth client secret issued by the provider.
ClientSecret string
// AuthURL is the authorization endpoint URL where users are redirected.
AuthURL string
// TokenURL is the token endpoint URL where codes are exchanged for tokens.
TokenURL string
// UserInfoURL is the endpoint URL where user claims / profile data are fetched.
UserInfoURL string
// Scopes are the default scopes requested during authorization.
Scopes []string
// AllowedRedirectURIs defines valid redirect URIs for this provider.
// Empty list means all non-empty redirect URIs are allowed (not recommended for production).
AllowedRedirectURIs []string
// HTTPClient is the HTTP client used to perform outbound requests to the provider.
// Defaults to &http.Client{Timeout: 15 * time.Second} if nil.
HTTPClient HTTPClient
// ProfileExtractor customizes the parsing of user profile responses.
// If nil, StandardProfileExtractor is used.
ProfileExtractor ProfileExtractorFunc
}
GenericConfig configures a GenericProvider instance.
type GenericProvider ¶
type GenericProvider struct {
// contains filtered or unexported fields
}
GenericProvider is an RFC 6749 and RFC 7636 compliant OAuth 2.0 / OIDC provider implementation.
func NewDiscordProvider ¶
func NewDiscordProvider(cfg ProviderCommonConfig) (*GenericProvider, error)
NewDiscordProvider builds a Discord OAuth 2.0 provider.
func NewGenericProvider ¶
func NewGenericProvider(cfg GenericConfig) (*GenericProvider, error)
NewGenericProvider constructs a validated GenericProvider.
func NewGitHubProvider ¶
func NewGitHubProvider(cfg ProviderCommonConfig) (*GenericProvider, error)
NewGitHubProvider builds a GitHub OAuth provider with automatic email discovery.
func NewGitLabProvider ¶
func NewGitLabProvider(baseURL string, cfg ProviderCommonConfig) (*GenericProvider, error)
NewGitLabProvider builds a GitLab OAuth / OIDC provider.
func NewGoogleProvider ¶
func NewGoogleProvider(cfg ProviderCommonConfig) (*GenericProvider, error)
NewGoogleProvider builds a Google OAuth 2.0 / OpenID Connect provider.
func NewMicrosoftProvider ¶
func NewMicrosoftProvider(tenant string, cfg ProviderCommonConfig) (*GenericProvider, error)
NewMicrosoftProvider builds a Microsoft Entra ID (Azure AD) OAuth 2.0 / OIDC provider.
func (*GenericProvider) AuthCodeURL ¶
func (p *GenericProvider) AuthCodeURL(state string, opts ...AuthCodeOption) string
AuthCodeURL constructs the authorization redirect URL with state and parameters.
func (*GenericProvider) Exchange ¶
func (p *GenericProvider) Exchange(ctx context.Context, code string, opts ...ExchangeOption) (*TokenResponse, error)
Exchange exchanges the authorization code for tokens.
func (*GenericProvider) FetchProfile ¶
func (p *GenericProvider) FetchProfile(ctx context.Context, tokens *TokenResponse) (*ProviderProfile, error)
FetchProfile retrieves and normalizes user profile information from the UserInfo endpoint.
func (*GenericProvider) Name ¶
func (p *GenericProvider) Name() string
Name returns the provider's unique identifier.
func (*GenericProvider) ValidateRedirectURI ¶
func (p *GenericProvider) ValidateRedirectURI(redirectURI string) bool
ValidateRedirectURI checks whether redirectURI is allowed for this provider.
type HTTPClient ¶
HTTPClient abstracts outbound HTTP request execution. It is satisfied by *http.Client.
type ProfileExtractorFunc ¶
type ProfileExtractorFunc func(ctx context.Context, rawBody []byte, token *TokenResponse) (*ProviderProfile, error)
ProfileExtractorFunc is a function that parses raw JSON profile bytes into a ProviderProfile.
func StandardProfileExtractor ¶
func StandardProfileExtractor(providerName string) ProfileExtractorFunc
StandardProfileExtractor returns a default claim parser extracting subject, email, name, avatar.
type Provider ¶
type Provider interface {
// Name returns the unique identifier of the provider (e.g. "google", "github").
Name() string
// AuthCodeURL constructs the authorization redirect URL with the given state and options.
AuthCodeURL(state string, opts ...AuthCodeOption) string
// Exchange sends the authorization code to the provider's token endpoint to acquire tokens.
Exchange(ctx context.Context, code string, opts ...ExchangeOption) (*TokenResponse, error)
// FetchProfile retrieves normalized user identity claims using the provided tokens.
FetchProfile(ctx context.Context, tokens *TokenResponse) (*ProviderProfile, error)
// ValidateRedirectURI checks whether the requested redirect URI is permitted.
ValidateRedirectURI(redirectURI string) bool
}
Provider represents an external OAuth 2.0 / OIDC identity provider. Implementations must be thread-safe.
type ProviderCommonConfig ¶
type ProviderCommonConfig struct {
ClientID string
ClientSecret string
AllowedRedirectURIs []string
HTTPClient HTTPClient
}
Common provider option config struct.
type ProviderProfile ¶
type ProviderProfile struct {
// Provider is the identifier of the identity provider (e.g., "google", "github").
Provider string `json:"provider"`
// ProviderID is the unique, stable identifier of the subject within the provider.
ProviderID string `json:"provider_id"`
// Email is the primary email address reported by the provider.
Email string `json:"email"`
// EmailVerified indicates whether the external provider asserts that the email has been verified.
EmailVerified bool `json:"email_verified"`
// Name is the display name of the user.
Name string `json:"name,omitempty"`
// Username is the handle or screen name of the user, if available.
Username string `json:"username,omitempty"`
// AvatarURL is a URL pointing to the user's avatar image.
AvatarURL string `json:"avatar_url,omitempty"`
// Raw contains the unparsed claims returned by the provider's profile or userinfo endpoint.
Raw map[string]any `json:"raw,omitempty"`
}
ProviderProfile represents normalized user information fetched from an identity provider.
type TokenResponse ¶
type TokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token,omitempty"`
ExpiresIn int64 `json:"expires_in,omitempty"`
ExpiresAt time.Time `json:"expires_at,omitempty"`
Scope string `json:"scope,omitempty"`
IDToken string `json:"id_token,omitempty"`
Raw map[string]any `json:"raw,omitempty"`
}
TokenResponse represents the parsed response from an OAuth token exchange.