oauth2

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrProviderNotFound is returned when a provider is not found
	ErrProviderNotFound = errors.New("OAuth2 provider not found")
	// ErrEmailNotAvailable is returned when the OAuth2 provider does not provide an email address
	ErrEmailNotAvailable = errors.New("OAuth2 provider did not provide an email address")
)
View Source
var (
	// ErrInvalidToken is returned when the OAuth2 token is invalid
	ErrInvalidToken = errors.New("invalid OAuth2 token")

	// ErrEmailNotFound is returned when user email is not found in OAuth2 response
	ErrEmailNotFound = errors.New("user email not found in OAuth2 response")
)

Functions

func GenerateState

func GenerateState() (string, error)

GenerateState generates a random state string for CSRF protection

Types

type CustomProvider

type CustomProvider struct {
	// contains filtered or unexported fields
}

CustomProvider is a generic OAuth2 provider for custom/OIDC-compatible servers

func NewCustomProvider

func NewCustomProvider(
	name string,
	clientID string,
	clientSecret string,
	redirectURL string,
	authURL string,
	tokenURL string,
	userInfoURL string,
	scopes []string,
	insecureSkipVerify bool,
) *CustomProvider

NewCustomProvider creates a new custom OAuth2 provider

func (*CustomProvider) Config

func (p *CustomProvider) Config() *oauth2.Config

Config returns the OAuth2 config

func (*CustomProvider) GetUserEmail

func (p *CustomProvider) GetUserEmail(ctx context.Context, token *oauth2.Token) (string, error)

GetUserEmail retrieves the user's email from the custom provider (deprecated, use GetUserInfo)

func (*CustomProvider) GetUserInfo

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

GetUserInfo retrieves the user's information from the custom provider

func (*CustomProvider) Name

func (p *CustomProvider) Name() string

Name returns the provider name

type GitHubProvider

type GitHubProvider struct {
	// contains filtered or unexported fields
}

GitHubProvider is the OAuth2 provider for GitHub

func NewGitHubProvider

func NewGitHubProvider(id, clientID, clientSecret, redirectURL string, scopes []string, resetScopes bool) *GitHubProvider

NewGitHubProvider creates a new GitHub OAuth2 provider

func (*GitHubProvider) Config

func (p *GitHubProvider) Config() *oauth2.Config

Config returns the OAuth2 config

func (*GitHubProvider) GetUserEmail

func (p *GitHubProvider) GetUserEmail(ctx context.Context, token *oauth2.Token) (string, error)

GetUserEmail retrieves the user's email from GitHub (deprecated, use GetUserInfo)

func (*GitHubProvider) GetUserInfo

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

GetUserInfo retrieves the user's information from GitHub

func (*GitHubProvider) Name

func (p *GitHubProvider) Name() string

Name returns the provider name

type GoogleProvider

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

GoogleProvider is the OAuth2 provider for Google

func NewGoogleProvider

func NewGoogleProvider(id, clientID, clientSecret, redirectURL string, scopes []string, resetScopes bool) *GoogleProvider

NewGoogleProvider creates a new Google OAuth2 provider

func (*GoogleProvider) Config

func (p *GoogleProvider) Config() *oauth2.Config

Config returns the OAuth2 config

func (*GoogleProvider) GetUserEmail

func (p *GoogleProvider) GetUserEmail(ctx context.Context, token *oauth2.Token) (string, error)

GetUserEmail retrieves the user's email from Google (deprecated, use GetUserInfo)

func (*GoogleProvider) GetUserInfo

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

GetUserInfo retrieves the user's information from Google

func (*GoogleProvider) Name

func (p *GoogleProvider) Name() string

Name returns the provider name (ID)

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager manages OAuth2 providers and authentication flow

func NewManager

func NewManager() *Manager

NewManager creates a new OAuth2 manager

func (*Manager) AddProvider

func (m *Manager) AddProvider(provider Provider)

AddProvider adds a provider to the manager

func (*Manager) Exchange

func (m *Manager) Exchange(ctx context.Context, providerName, code string) (*oauth2.Token, error)

Exchange exchanges an authorization code for a token

func (*Manager) ExchangeWithRedirect

func (m *Manager) ExchangeWithRedirect(ctx context.Context, providerName, code, redirectURL string) (*oauth2.Token, error)

ExchangeWithRedirect exchanges an authorization code for a token using a custom redirect URL. This is required when the redirect URL used in the authorization request differs from the provider's configured redirect URL (e.g., in Docker environments with port mapping).

func (*Manager) GetAuthURL

func (m *Manager) GetAuthURL(providerName, state string) (string, error)

GetAuthURL generates an authorization URL for a provider

func (*Manager) GetAuthURLWithHost deprecated

func (m *Manager) GetAuthURLWithHost(providerName, state, hostOrBaseURL, authPathPrefix string) (string, error)

GetAuthURLWithHost generates an authorization URL for a provider with a custom redirect URL based on the request host or base URL. This is useful for dynamic port mapping (e.g., Docker environments). The hostOrBaseURL parameter can be:

  • A full base URL (e.g., "http://localhost:4182")
  • A host with port (e.g., "localhost:4182")
  • Just a host (e.g., "example.com")

Deprecated: Use GetAuthURLWithRedirect instead which returns both auth URL and redirect URL

func (*Manager) GetAuthURLWithRedirect

func (m *Manager) GetAuthURLWithRedirect(providerName, state, hostOrBaseURL, authPathPrefix string) (string, string, error)

GetAuthURLWithRedirect generates an authorization URL and returns both the auth URL and redirect URL. This is useful for dynamic port mapping (e.g., Docker environments). The hostOrBaseURL parameter can be:

  • A full base URL (e.g., "http://localhost:4182")
  • A host with port (e.g., "localhost:4182")
  • Just a host (e.g., "example.com")

Returns: (authURL, redirectURL, error)

func (*Manager) GetProvider

func (m *Manager) GetProvider(name string) (Provider, error)

GetProvider retrieves a provider by name

func (*Manager) GetProviders

func (m *Manager) GetProviders() []Provider

GetProviders returns all providers

func (*Manager) GetUserEmail

func (m *Manager) GetUserEmail(ctx context.Context, providerName string, token *oauth2.Token) (string, error)

GetUserEmail retrieves the user's email using a token (deprecated, use GetUserInfo)

func (*Manager) GetUserInfo

func (m *Manager) GetUserInfo(ctx context.Context, providerName string, token *oauth2.Token) (*UserInfo, error)

GetUserInfo retrieves the user's information using a token

type MicrosoftProvider

type MicrosoftProvider struct {
	// contains filtered or unexported fields
}

MicrosoftProvider is the OAuth2 provider for Microsoft (Azure AD)

func NewMicrosoftProvider

func NewMicrosoftProvider(id, clientID, clientSecret, redirectURL string, scopes []string, resetScopes bool) *MicrosoftProvider

NewMicrosoftProvider creates a new Microsoft OAuth2 provider

func (*MicrosoftProvider) Config

func (p *MicrosoftProvider) Config() *oauth2.Config

Config returns the OAuth2 config

func (*MicrosoftProvider) GetUserEmail

func (p *MicrosoftProvider) GetUserEmail(ctx context.Context, token *oauth2.Token) (string, error)

GetUserEmail retrieves the user's email from Microsoft Graph API (deprecated, use GetUserInfo)

func (*MicrosoftProvider) GetUserInfo

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

GetUserInfo retrieves the user's information from Microsoft Graph API

func (*MicrosoftProvider) Name

func (p *MicrosoftProvider) Name() string

Name returns the provider name (ID)

type Provider

type Provider interface {
	Name() string
	Config() *oauth2.Config
	GetUserInfo(ctx context.Context, token *oauth2.Token) (*UserInfo, error)
	// Deprecated: Use GetUserInfo instead
	GetUserEmail(ctx context.Context, token *oauth2.Token) (string, error)
}

Provider is an interface for OAuth2 providers

type UserInfo

type UserInfo struct {
	Email string                 // User's email address
	Name  string                 // User's display name (optional)
	Extra map[string]interface{} // Additional data from OAuth2 provider (for custom forwarding)
}

UserInfo represents user information from OAuth2 provider

Jump to

Keyboard shortcuts

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