Documentation
¶
Index ¶
- func DeriveOAuthHMACKey(appSecret string) []byte
- func FetchUserInfo(ctx context.Context, client *http.Client, logger models.Logger, ...) (map[string]any, error)
- func GeneratePKCEVerifier() (string, error)
- func GenerateRandomString(length int) (string, error)
- func GetBoolField(data map[string]any, field string) bool
- func GetIDField(data map[string]any, field string) (string, error)
- func GetStringField(data map[string]any, field string) string
- func ResolveOAuth2Config(cfg types.ProviderConfig, defaults ProviderDefaults) *oauth2.Config
- func ResolveUserInfoURL(cfg types.ProviderConfig, defaultURL string) string
- func SignCookie(payload string, secret []byte) (string, error)
- func SignState(payload StatePayload, secret []byte) (string, error)
- func ValidateCookie(signed string, secret []byte, maxAge time.Duration) (string, error)
- func ValidateRedirectTo(redirectTo string, trustedOrigins []string) (string, error)
- type BaseProvider
- func (p *BaseProvider) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)
- func (p *BaseProvider) GetAuthURL(state string, opts ...oauth2.AuthCodeOption) string
- func (p *BaseProvider) GetConfig() *oauth2.Config
- func (p *BaseProvider) Name() string
- func (p *BaseProvider) RequiresPKCE() bool
- type DiscordOAuth2Provider
- type GenericOAuth2Provider
- type GitHubOAuth2Provider
- type GoogleProvider
- type ProviderDefaults
- type ProviderOptions
- type ProviderRegistry
- type StatePayload
- type StatePurpose
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeriveOAuthHMACKey ¶
DeriveOAuthHMACKey derives an HMAC key from the app secret for OAuth2 operations
func FetchUserInfo ¶
func FetchUserInfo(ctx context.Context, client *http.Client, logger models.Logger, token *oauth2.Token, url string) (map[string]any, error)
FetchUserInfo fetches and parses user info with a bounded response body.
func GeneratePKCEVerifier ¶ added in v1.50.0
GeneratePKCEVerifier generates a PKCE code_verifier.
func GenerateRandomString ¶
GenerateRandomString generates a cryptographically secure random URL-safe string of exactly length characters.
func GetBoolField ¶ added in v1.50.0
GetBoolField safely gets a boolean field from a map.
func GetIDField ¶ added in v1.50.0
GetIDField returns a canonical string representation of an identifier field. Numeric IDs are kept as integers instead of being formatted through float64.
func GetStringField ¶
GetStringField safely gets a string field from a map.
func ResolveOAuth2Config ¶ added in v1.50.0
func ResolveOAuth2Config(cfg types.ProviderConfig, defaults ProviderDefaults) *oauth2.Config
ResolveOAuth2Config applies a provider config on top of built-in defaults.
func ResolveUserInfoURL ¶ added in v1.50.0
func ResolveUserInfoURL(cfg types.ProviderConfig, defaultURL string) string
ResolveUserInfoURL returns the configured user info URL or the provider default.
func SignCookie ¶
SignCookie creates a signed cookie value with format: base64(payload).timestamp.signature
func SignState ¶ added in v1.50.0
func SignState(payload StatePayload, secret []byte) (string, error)
SignState serialises and signs a state payload.
func ValidateCookie ¶
ValidateCookie validates a signed cookie and returns the payload.
Types ¶
type BaseProvider ¶
type BaseProvider struct {
// contains filtered or unexported fields
}
BaseProvider provides common functionality for OAuth2 providers
func NewBaseProvider ¶
func NewBaseProvider(name string, config *oauth2.Config, opts *ProviderOptions) *BaseProvider
NewBaseProvider creates a new base provider
func (*BaseProvider) Exchange ¶
func (p *BaseProvider) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)
Exchange exchanges the authorization code for a token
func (*BaseProvider) GetAuthURL ¶
func (p *BaseProvider) GetAuthURL(state string, opts ...oauth2.AuthCodeOption) string
GetAuthURL returns the authorization URL
func (*BaseProvider) GetConfig ¶
func (p *BaseProvider) GetConfig() *oauth2.Config
GetConfig returns the oauth2 config
func (*BaseProvider) RequiresPKCE ¶
func (p *BaseProvider) RequiresPKCE() bool
RequiresPKCE returns whether the provider requires PKCE
type DiscordOAuth2Provider ¶ added in v1.50.0
type DiscordOAuth2Provider struct {
*BaseProvider
// contains filtered or unexported fields
}
func NewDiscordOAuth2Provider ¶ added in v1.50.0
func NewDiscordOAuth2Provider(cfg types.ProviderConfig, opts *ProviderOptions) *DiscordOAuth2Provider
func (*DiscordOAuth2Provider) GetUserInfo ¶ added in v1.50.0
type GenericOAuth2Provider ¶ added in v1.50.0
type GenericOAuth2Provider struct {
*BaseProvider
// contains filtered or unexported fields
}
GenericOAuth2Provider implements OAuth2Provider for custom OIDC/OAuth2 endpoints
func NewGenericOAuth2Provider ¶ added in v1.50.0
func NewGenericOAuth2Provider(name string, cfg types.ProviderConfig, opts *ProviderOptions) *GenericOAuth2Provider
NewGenericOAuth2Provider creates a new generic OAuth2 provider
func (*GenericOAuth2Provider) GetUserInfo ¶ added in v1.50.0
func (p *GenericOAuth2Provider) GetUserInfo(ctx context.Context, token *oauth2.Token) (*types.UserInfo, error)
GetUserInfo fetches and normalizes user information using custom field mapping
type GitHubOAuth2Provider ¶ added in v1.50.0
type GitHubOAuth2Provider struct {
*BaseProvider
// contains filtered or unexported fields
}
func NewGitHubOAuth2Provider ¶ added in v1.50.0
func NewGitHubOAuth2Provider(cfg types.ProviderConfig, opts *ProviderOptions) *GitHubOAuth2Provider
func (*GitHubOAuth2Provider) GetUserInfo ¶ added in v1.50.0
func (*GitHubOAuth2Provider) RequiresPKCE ¶ added in v1.50.0
func (p *GitHubOAuth2Provider) RequiresPKCE() bool
type GoogleProvider ¶
type GoogleProvider struct {
*BaseProvider
// contains filtered or unexported fields
}
func NewGoogleProvider ¶
func NewGoogleProvider(cfg types.ProviderConfig, opts *ProviderOptions) *GoogleProvider
func (*GoogleProvider) GetUserInfo ¶
type ProviderDefaults ¶ added in v1.50.0
ProviderDefaults describes the built-in behaviour of a provider before config overrides.
type ProviderOptions ¶ added in v1.50.0
ProviderOptions carries optional dependencies shared by providers.
type ProviderRegistry ¶
type ProviderRegistry struct {
// contains filtered or unexported fields
}
ProviderRegistry manages OAuth2 providers
func NewProviderRegistry ¶
func NewProviderRegistry() *ProviderRegistry
NewProviderRegistry creates a new provider registry
func (*ProviderRegistry) Get ¶
func (r *ProviderRegistry) Get(name string) (types.OAuth2Provider, bool)
Get retrieves a provider by name
func (*ProviderRegistry) GetAll ¶
func (r *ProviderRegistry) GetAll() map[string]types.OAuth2Provider
GetAll returns all registered providers
func (*ProviderRegistry) Names ¶ added in v1.50.0
func (r *ProviderRegistry) Names() []string
Names returns the registered provider names in deterministic order.
func (*ProviderRegistry) Register ¶
func (r *ProviderRegistry) Register(name string, provider types.OAuth2Provider) error
Register registers a provider
type StatePayload ¶ added in v1.50.0
type StatePayload struct {
State string `json:"state"`
Provider string `json:"provider"`
UserID string `json:"user_id,omitempty"`
Purpose StatePurpose `json:"purpose"`
}
StatePayload is the signed, provider-bound data stored in the OAuth2 state cookie.
func ParseState ¶ added in v1.50.0
ParseState validates the signature and age of a signed state cookie.
type StatePurpose ¶ added in v1.50.0
type StatePurpose string
StatePurpose identifies why an OAuth2 flow was started.
const ( StatePurposeLogin StatePurpose = "login" StatePurposeLink StatePurpose = "link" )