oauth

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package oauth provides OAuth 2.0 authentication support for external providers.

This package implements OAuth 2.0 authentication flows for multiple identity providers:

  • GitHub
  • Google
  • Generic OIDC (OpenID Connect) providers

Each provider implements a common OAuth interface that handles the authorization code flow. The package supports:

  • Authorization URL generation with PKCE
  • Token exchange
  • User profile fetching
  • Automatic provider discovery via OIDC

OAuth providers are configured in the application config file with client IDs, secrets, and scopes. Users can sign in using any configured provider, and their OAuth ID is linked to their Omnom account.

The Providers map contains factory functions for creating provider instances:

provider := oauth.Providers["github"](oauthConfig)
authURL := provider.GetAuthURL(state)

Example configuration:

oauth:
  github:
    client_id: "your-client-id"
    client_secret: "your-client-secret"
    scopes: ["user:email"]

Index

Constants

This section is empty.

Variables

View Source
var Providers = map[string]oauthProvider{
	"github": GitHubOAuth{
		AuthURL:  "https://github.com/login/oauth/authorize",
		TokenURL: "https://github.com/login/oauth/access_token",
	},
	"google": GoogleOAuth{
		AuthURL:  "https://accounts.google.com/o/oauth2/auth",
		TokenURL: "https://accounts.google.com/o/oauth2/token",
	},
	"oidc": &OIDCOAuth{},
}

Providers maps provider names to their OAuth implementation instances. Supported providers: github, google, oidc.

Functions

This section is empty.

Types

type GitHubOAuth

type GitHubOAuth struct {
	AuthURL  string
	TokenURL string
}

GitHubOAuth implements OAuth 2.0 authentication for GitHub.

func (GitHubOAuth) GetRedirectURL

func (g GitHubOAuth) GetRedirectURL(req *RedirectURIRequest) string

GetRedirectURL constructs the GitHub authorization URL for redirecting users.

func (GitHubOAuth) GetScope

func (g GitHubOAuth) GetScope() (ScopeName, ScopeValue)

GetScope returns the OAuth scopes required for GitHub authentication.

func (GitHubOAuth) GetToken added in v0.5.0

func (g GitHubOAuth) GetToken(ctx context.Context, req *TokenRequest) (*http.Response, error)

GetToken exchanges an authorization code for an access token.

func (GitHubOAuth) GetUserInfo added in v0.5.0

func (g GitHubOAuth) GetUserInfo(ctx context.Context, response TokenResponse) (*UserInfoResponse, error)

GetUserInfo fetches user information from GitHub using the access token.

func (GitHubOAuth) Prepare added in v0.4.0

func (g GitHubOAuth) Prepare(_ context.Context, _ *PrepareRequest) error

Prepare initializes the GitHub OAuth provider. No preparation needed for GitHub.

type GoogleOAuth

type GoogleOAuth struct {
	AuthURL  string
	TokenURL string
}

GoogleOAuth implements OAuth 2.0 authentication for Google.

func (GoogleOAuth) GetRedirectURL

func (g GoogleOAuth) GetRedirectURL(req *RedirectURIRequest) string

GetRedirectURL constructs the Google authorization URL for redirecting users.

func (GoogleOAuth) GetScope

func (g GoogleOAuth) GetScope() (ScopeName, ScopeValue)

GetScope returns the OAuth scopes required for Google authentication.

func (GoogleOAuth) GetToken added in v0.5.0

func (g GoogleOAuth) GetToken(ctx context.Context, req *TokenRequest) (*http.Response, error)

GetToken exchanges an authorization code for an access token.

func (GoogleOAuth) GetUserInfo added in v0.5.0

func (g GoogleOAuth) GetUserInfo(ctx context.Context, response TokenResponse) (*UserInfoResponse, error)

GetUserInfo fetches user information from Google using the access token.

func (GoogleOAuth) Prepare added in v0.4.0

func (g GoogleOAuth) Prepare(_ context.Context, _ *PrepareRequest) error

Prepare initializes the Google OAuth provider. No preparation needed for Google.

type GrantType added in v0.4.0

type GrantType string

GrantType represents the OAuth grant type (e.g., "authorization_code").

func (GrantType) String added in v0.4.0

func (gt GrantType) String() string

String returns the string representation of GrantType.

type OIDCOAuth added in v0.4.0

type OIDCOAuth struct {
	AuthURL     string `json:"authorization_endpoint"`
	TokenURL    string `json:"token_endpoint"`
	UserInfoURL string `json:"userinfo_endpoint"`

	Scopes       []ScopeValue   `json:"scopes_supported"`
	ResponseType []ResponseType `json:"response_types_supported"`
	GrantType    []GrantType    `json:"grant_types_supported"`

	ConfigurationURL string
}

OIDCOAuth implements OAuth 2.0 authentication for generic OpenID Connect providers. It automatically discovers provider configuration from a well-known URL.

func (*OIDCOAuth) GetRedirectURL added in v0.4.0

func (o *OIDCOAuth) GetRedirectURL(req *RedirectURIRequest) string

GetRedirectURL constructs the OIDC authorization URL for redirecting users.

func (*OIDCOAuth) GetScope added in v0.4.0

func (o *OIDCOAuth) GetScope() (ScopeName, ScopeValue)

GetScope returns the OAuth scopes required for OIDC authentication.

func (*OIDCOAuth) GetToken added in v0.5.0

func (o *OIDCOAuth) GetToken(ctx context.Context, req *TokenRequest) (*http.Response, error)

GetToken exchanges an authorization code for an access token.

func (*OIDCOAuth) GetUserInfo added in v0.5.0

func (o *OIDCOAuth) GetUserInfo(ctx context.Context, response TokenResponse) (*UserInfoResponse, error)

GetUserInfo fetches user information from the OIDC provider using the access token.

func (*OIDCOAuth) Prepare added in v0.4.0

func (o *OIDCOAuth) Prepare(ctx context.Context, req *PrepareRequest) error

Prepare initializes the OIDC OAuth provider by fetching its configuration. This must be called before using other methods.

type PrepareRequest added in v0.5.0

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

PrepareRequest contains parameters for preparing an OAuth provider.

func NewPrepareRequest added in v0.5.0

func NewPrepareRequest(cURL string) *PrepareRequest

NewPrepareRequest creates a new PrepareRequest with the given configuration URL.

type RedirectURIRequest added in v0.5.0

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

RedirectURIRequest contains parameters for building an OAuth redirect URI.

func NewRedirectURIRequest added in v0.5.0

func NewRedirectURIRequest(clientID string, redirectURI string) *RedirectURIRequest

NewRedirectURIRequest creates a new RedirectURIRequest with the given parameters.

type ResponseType added in v0.4.0

type ResponseType string

ResponseType represents the OAuth response type (e.g., "code").

func (ResponseType) String added in v0.4.0

func (rt ResponseType) String() string

String returns the string representation of ResponseType.

type ScopeName added in v0.4.0

type ScopeName string

ScopeName represents the name of an OAuth scope parameter.

func (ScopeName) String added in v0.4.0

func (sn ScopeName) String() string

String returns the string representation of ScopeName.

type ScopeValue added in v0.4.0

type ScopeValue string

ScopeValue represents the value of an OAuth scope.

func (ScopeValue) String added in v0.4.0

func (sv ScopeValue) String() string

String returns the string representation of ScopeValue.

type TokenRequest added in v0.5.0

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

TokenRequest contains parameters for exchanging an authorization code for a token.

func NewTokenRequest added in v0.5.0

func NewTokenRequest(clientID string, clientSecret string, code string, redirectURI string) *TokenRequest

NewTokenRequest creates a new TokenRequest with the given parameters.

type TokenResponse added in v0.5.0

type TokenResponse []byte

TokenResponse contains the raw token response from an OAuth provider.

type UserInfoResponse added in v0.5.0

type UserInfoResponse struct {
	UID      string
	Email    string
	Username string
}

UserInfoResponse contains user information retrieved from an OAuth provider.

Jump to

Keyboard shortcuts

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