Documentation
¶
Index ¶
- Constants
- func GenerateChallenge(verifier string) string
- func GenerateVerifier() (string, error)
- type AuthURLOption
- type Authenticator
- func (a *Authenticator) ApplyMetadata(meta *Metadata)
- func (a *Authenticator) AuthorizeRequest(token *oauth2.Token, req *http.Request)
- func (a *Authenticator) Discover(ctx context.Context) (bool, error)
- func (a *Authenticator) Exchange(ctx context.Context, code, state, verifier string) (*oauth2.Token, error)
- func (a *Authenticator) GetAuthURL(opts ...AuthURLOption) (string, string, string, error)
- func (a *Authenticator) GetMetadata() *Metadata
- func (a *Authenticator) RefreshToken(ctx context.Context, token *oauth2.Token) (*oauth2.Token, error)
- type Config
- type LocalFlow
- type LocalFlowOption
- type Metadata
- type Option
- type Result
Constants ¶
const ( PathOpenIDConfig = "/.well-known/openid-configuration" PathOAuthAuthServer = "/.well-known/oauth-authorization-server" PathOAuthProtectedRoute = "/.well-known/oauth-protected-resource" )
Well-known paths
const ( HeaderAuthorization = "Authorization" HeaderWWWAuthenticate = "WWW-Authenticate" HeaderXDiscoveryURL = "X-Discovery-URL" )
Header names
const ( ParamCode = "code" ParamState = "state" ParamError = "error" ParamResource = "resource" ParamCodeChallenge = "code_challenge" ParamCodeChallengeMethod = "code_challenge_method" ParamCodeVerifier = "code_verifier" )
OAuth2 parameter names
const ( CmdWindows = "rundll32" ArgsWindows = "url.dll,FileProtocolHandler" CmdDarwin = "open" CmdLinux = "xdg-open" )
Browser commands
const ( DefaultPort = 8080 DefaultCallbackPath = "/callback" BearerPrefix = "Bearer " )
Default values
const (
MethodS256 = "S256"
)
PKCE methods
Variables ¶
This section is empty.
Functions ¶
func GenerateChallenge ¶
GenerateChallenge creates a PKCE code challenge from a verifier using the S256 method. S256(verifier) = BASE64URL-ENCODE(SHA256(ASCII(verifier)))
func GenerateVerifier ¶
GenerateVerifier creates a cryptographically strong random string to be used as a PKCE code verifier. The verifier must be between 43 and 128 characters long.
Types ¶
type AuthURLOption ¶
type AuthURLOption func(*authURLOptions)
AuthURLOption defines a functional option for GetAuthURL.
func WithPKCE ¶
func WithPKCE(enabled bool) AuthURLOption
WithPKCE enables or disables PKCE (enabled by default).
func WithState ¶
func WithState(state string) AuthURLOption
WithState sets a custom state for the authorization URL.
func WithVerifier ¶
func WithVerifier(verifier string) AuthURLOption
WithVerifier sets a custom code verifier for the authorization URL.
type Authenticator ¶
type Authenticator struct {
// contains filtered or unexported fields
}
Authenticator handles the OAuth2 flow including discovery, URL generation, and token exchange.
func NewAuthenticator ¶
func NewAuthenticator(cfg Config, opts ...Option) (*Authenticator, error)
NewAuthenticator creates a new Authenticator instance.
func (*Authenticator) ApplyMetadata ¶
func (a *Authenticator) ApplyMetadata(meta *Metadata)
ApplyMetadata updates the internal oauth2.Config with endpoints and scopes from discovery.
func (*Authenticator) AuthorizeRequest ¶
func (a *Authenticator) AuthorizeRequest(token *oauth2.Token, req *http.Request)
AuthorizeRequest injects the token as a Bearer token in the request header.
func (*Authenticator) Discover ¶
func (a *Authenticator) Discover(ctx context.Context) (bool, error)
Discover attempts to find OAuth2 metadata. It returns true if authentication is required, false otherwise.
func (*Authenticator) Exchange ¶
func (a *Authenticator) Exchange(ctx context.Context, code, state, verifier string) (*oauth2.Token, error)
Exchange exchanges an authorization code for a token.
func (*Authenticator) GetAuthURL ¶
func (a *Authenticator) GetAuthURL(opts ...AuthURLOption) (string, string, string, error)
GetAuthURL generates the authorization URL. It returns the URL, the state, and the code verifier if PKCE was used.
func (*Authenticator) GetMetadata ¶
func (a *Authenticator) GetMetadata() *Metadata
GetMetadata returns the retrieved metadata.
func (*Authenticator) RefreshToken ¶
func (a *Authenticator) RefreshToken(ctx context.Context, token *oauth2.Token) (*oauth2.Token, error)
RefreshToken returns a fresh token, using the refresh token if the current one is expired.
type Config ¶
type Config struct {
// Name is the application's name
Name string
// BaseURL is the authorization server's base URL used for discovery.
BaseURL string
// ClientID is the application's ID.
ClientID string
// ClientSecret is the application's secret. Prefer PKCE if possible.
ClientSecret string
// RedirectURL is where the user will be sent after authorization.
RedirectURL string
// Scopes is a list of requested permissions.
Scopes []string
// AuthorizationURL and TokenURL can be provided manually to bypass discovery.
AuthorizationURL string
TokenURL string
}
Config represents the configuration for the Authenticator.
func (*Config) McpFingerprint ¶
type LocalFlow ¶
type LocalFlow struct {
// Port is the port number for the local callback server (e.g., 8080).
Port int
// Timeout is how long to wait for the callback before giving up.
Timeout time.Duration
// CallbackPath is the path for the redirect URI (e.g., "/callback").
CallbackPath string
// contains filtered or unexported fields
}
LocalFlow handles the local web-based OAuth2 flow.
func NewLocalFlow ¶
func NewLocalFlow(opts ...LocalFlowOption) *LocalFlow
NewLocalFlow creates a new LocalFlow with the provided options and sensible defaults.
func (*LocalFlow) OpenBrowser ¶
OpenBrowser opens the specified URL in the default system browser.
type LocalFlowOption ¶
type LocalFlowOption func(*LocalFlow)
LocalFlowOption defines a functional option for configuring the LocalFlow.
func WithCallbackPath ¶
func WithCallbackPath(path string) LocalFlowOption
WithCallbackPath sets the URL path for the callback.
func WithLocalFlowLogger ¶
func WithLocalFlowLogger(logger *slog.Logger) LocalFlowOption
WithLocalFlowLogger sets a custom slog logger for the LocalFlow.
func WithPort ¶
func WithPort(port int) LocalFlowOption
WithPort sets the port for the local callback server.
func WithTimeout ¶
func WithTimeout(timeout time.Duration) LocalFlowOption
WithTimeout sets the timeout for waiting for the callback.
type Metadata ¶
type Metadata struct {
Issuer string `json:"issuer,omitempty"`
AuthorizationURL string `json:"authorization_url,omitempty"`
TokenURL string `json:"token_url,omitempty"`
JWKSURI string `json:"jwks_uri,omitempty"`
RegistrationEndpoint string `json:"registration_endpoint,omitempty"`
ScopesSupported []string `json:"scopes_supported,omitempty"`
ResponseTypes []string `json:"response_types_supported,omitempty"`
GrantTypes []string `json:"grant_types_supported,omitempty"`
CodeChallengeMethods []string `json:"code_challenge_methods_supported,omitempty"`
// Protected Resource Metadata (RFC 9728)
Resource string `json:"resource,omitempty"`
AuthorizationServers []string `json:"authorization_servers,omitempty"`
}
Metadata contains the authorization server's metadata (RFC 8414 / RFC 9728).
func DiscoverMetadata ¶
func DiscoverMetadata(ctx context.Context, baseURL string, client *http.Client, logger *slog.Logger) (*Metadata, error)
DiscoverMetadata attempts to find OAuth2 metadata for a given base URL.
func ProbeMetadata ¶
func ProbeMetadata(ctx context.Context, baseURL string, client *http.Client, logger *slog.Logger) (*Metadata, bool, error)
ProbeMetadata checks if a resource is protected and attempts discovery via headers.
func (*Metadata) GetEndpoints ¶
GetEndpoints returns the resolved authorization and token URLs.
func (*Metadata) MarshalBinary ¶
MarshalBinary encodes the Metadata struct into JSON bytes.
func (*Metadata) UnmarshalBinary ¶
UnmarshalBinary decodes JSON bytes back into the Metadata struct.
func (*Metadata) UnmarshalJSON ¶
UnmarshalJSON implements custom decoding to handle standard and aliased endpoint names.
type Option ¶
type Option func(*Authenticator)
Option defines a functional option for configuring the Authenticator.
func WithHTTPClient ¶
WithHTTPClient sets a custom HTTP client for discovery and token exchange.
func WithLogger ¶
WithLogger sets a custom slog logger for the Authenticator.