oauth

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package oauth provides OAuth2 provider configuration and handlers for CoreForge.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrProviderNotConfigured is returned when a provider is not configured.
	ErrProviderNotConfigured = errors.New("oauth provider not configured")
	// ErrInvalidState is returned when the OAuth state is invalid.
	ErrInvalidState = errors.New("invalid oauth state")
	// ErrFailedUserInfo is returned when user info cannot be fetched.
	ErrFailedUserInfo = errors.New("failed to fetch user info")
)

Functions

This section is empty.

Types

type GitHubUser

type GitHubUser struct {
	ID        int    `json:"id"`
	Login     string `json:"login"`
	Name      string `json:"name"`
	Email     string `json:"email"`
	AvatarURL string `json:"avatar_url"`
}

GitHubUser represents a GitHub user profile.

func (*GitHubUser) ToUserInfo

func (u *GitHubUser) ToUserInfo(accessToken, refreshToken string) *UserInfo

ToUserInfo converts a GitHub user to UserInfo.

type GoogleUser

type GoogleUser struct {
	ID            string `json:"id"`
	Email         string `json:"email"`
	VerifiedEmail bool   `json:"verified_email"`
	Name          string `json:"name"`
	GivenName     string `json:"given_name"`
	FamilyName    string `json:"family_name"`
	Picture       string `json:"picture"`
}

GoogleUser represents a Google user profile.

func (*GoogleUser) ToUserInfo

func (u *GoogleUser) ToUserInfo(accessToken, refreshToken string) *UserInfo

ToUserInfo converts a Google user to UserInfo.

type Handler

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

Handler manages OAuth2 authentication flows.

func NewHandler

func NewHandler(stateStore StateStore) *Handler

NewHandler creates a new OAuth handler.

func (*Handler) AuthorizationURL

func (h *Handler) AuthorizationURL(ctx context.Context, provider Provider, redirectURL string) (string, error)

AuthorizationURL generates an OAuth authorization URL.

func (*Handler) GetProvider

func (h *Handler) GetProvider(provider Provider) (*ProviderConfig, bool)

GetProvider returns the configuration for a provider.

func (*Handler) HandleCallback

func (h *Handler) HandleCallback(ctx context.Context, provider Provider, code, state string) (*UserInfo, string, error)

HandleCallback processes the OAuth callback and returns user information.

func (*Handler) RegisterProvider

func (h *Handler) RegisterProvider(cfg *ProviderConfig)

RegisterProvider adds an OAuth provider configuration.

type MemoryStateStore

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

MemoryStateStore is a simple in-memory state store for development. Use a Redis or database-backed store in production.

func NewMemoryStateStore

func NewMemoryStateStore() *MemoryStateStore

NewMemoryStateStore creates a new in-memory state store.

func (*MemoryStateStore) Get

func (s *MemoryStateStore) Get(ctx context.Context, state string) (StateData, error)

Get retrieves and deletes a state value.

func (*MemoryStateStore) Set

func (s *MemoryStateStore) Set(ctx context.Context, state string, data StateData, expiry time.Duration) error

Set stores a state value.

type Provider

type Provider string

Provider represents a supported OAuth2 provider.

const (
	// GitHub is the GitHub OAuth provider.
	GitHub Provider = "github"
	// Google is the Google OAuth provider.
	Google Provider = "google"
)

type ProviderConfig

type ProviderConfig struct {
	// Provider is the provider identifier.
	Provider Provider

	// ClientID is the OAuth2 client ID.
	ClientID string

	// ClientSecret is the OAuth2 client secret.
	ClientSecret string //nolint:gosec // G117: config field, not a hardcoded secret

	// RedirectURL is the OAuth2 callback URL.
	RedirectURL string

	// Scopes are the OAuth2 scopes to request.
	Scopes []string
}

ProviderConfig holds configuration for an OAuth2 provider.

func (*ProviderConfig) OAuth2Config

func (p *ProviderConfig) OAuth2Config() *oauth2.Config

OAuth2Config returns an oauth2.Config for this provider.

type StateData

type StateData struct {
	Provider    Provider `json:"provider"`
	RedirectURL string   `json:"redirect_url,omitempty"`
	Nonce       string   `json:"nonce,omitempty"`
}

StateData holds data associated with an OAuth state.

type StateStore

type StateStore interface {
	// Set stores a state value with expiration.
	Set(ctx context.Context, state string, data StateData, expiry time.Duration) error

	// Get retrieves and deletes a state value.
	Get(ctx context.Context, state string) (StateData, error)
}

StateStore persists OAuth state for CSRF protection.

type UserInfo

type UserInfo struct {
	// ID is the user's ID from the provider.
	ID string

	// Email is the user's email address.
	Email string

	// Name is the user's display name.
	Name string

	// AvatarURL is the URL to the user's avatar image.
	AvatarURL string

	// Provider is the OAuth provider.
	Provider Provider

	// AccessToken is the OAuth access token.
	AccessToken string

	// RefreshToken is the OAuth refresh token (if provided).
	RefreshToken string
}

UserInfo represents user information from an OAuth provider.

Jump to

Keyboard shortcuts

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