services

package
v1.50.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 5, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeriveOAuthHMACKey

func DeriveOAuthHMACKey(appSecret string) []byte

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

func GeneratePKCEVerifier() (string, error)

GeneratePKCEVerifier generates a PKCE code_verifier.

func GenerateRandomString

func GenerateRandomString(length int) (string, error)

GenerateRandomString generates a cryptographically secure random URL-safe string of exactly length characters.

func GetBoolField added in v1.50.0

func GetBoolField(data map[string]any, field string) bool

GetBoolField safely gets a boolean field from a map.

func GetIDField added in v1.50.0

func GetIDField(data map[string]any, field string) (string, error)

GetIDField returns a canonical string representation of an identifier field. Numeric IDs are kept as integers instead of being formatted through float64.

func GetStringField

func GetStringField(data map[string]any, field string) string

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

func SignCookie(payload string, secret []byte) (string, error)

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

func ValidateCookie(signed string, secret []byte, maxAge time.Duration) (string, error)

ValidateCookie validates a signed cookie and returns the payload.

func ValidateProviderName added in v1.50.1

func ValidateProviderName(name string) error

ValidateProviderName enforces a path-safe provider identifier.

func ValidateRedirectTo

func ValidateRedirectTo(redirectTo string, trustedOrigins []string) (string, error)

ValidateRedirectTo validates a redirect destination and returns its normalised form. Relative paths and absolute URLs on trusted origins are allowed.

func ValidateRedirectURL added in v1.50.1

func ValidateRedirectURL(raw string) error

ValidateRedirectURL validates an OAuth2 provider redirect URL registered with the provider. Redirect URIs must be absolute, exact, and use HTTPS outside localhost. Wildcards, fragments, credentials, and relative paths are rejected.

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, pkce bool) *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) Name

func (p *BaseProvider) Name() string

Name returns the provider name

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

func (p *DiscordOAuth2Provider) GetUserInfo(ctx context.Context, token *oauth2.Token) (*types.UserInfo, error)

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 (p *GitHubOAuth2Provider) GetUserInfo(ctx context.Context, token *oauth2.Token) (*types.UserInfo, error)

type GoogleProvider

type GoogleProvider struct {
	*BaseProvider
	// contains filtered or unexported fields
}

func NewGoogleProvider

func NewGoogleProvider(cfg types.ProviderConfig, opts *ProviderOptions) *GoogleProvider

func (*GoogleProvider) GetUserInfo

func (p *GoogleProvider) GetUserInfo(ctx context.Context, token *oauth2.Token) (*types.UserInfo, error)

type ProviderDefaults added in v1.50.0

type ProviderDefaults struct {
	Endpoint    oauth2.Endpoint
	Scopes      []string
	UserInfoURL string
}

ProviderDefaults describes the built-in behaviour of a provider before config overrides.

type ProviderOptions added in v1.50.0

type ProviderOptions struct {
	HTTPClient *http.Client
	Logger     models.Logger
}

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

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

func ParseState(signed string, secret []byte, maxAge time.Duration) (*StatePayload, error)

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"
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL