Documentation
¶
Index ¶
- func APIKeyAuth(config APIKeyAuthConfig) func(http.Handler) http.Handler
- func BasicAuth(config BasicAuthConfig) func(http.Handler) http.Handler
- func BearerAuth(config BearerAuthConfig) func(http.Handler) http.Handler
- func DigestAuth(config DigestAuthConfig) func(http.Handler) http.Handler
- func MutualTLSAuth(config MutualTLSAuthConfig) func(http.Handler) http.Handler
- func OAuth2AuthorizationCodeAuth(config OAuth2AuthorizationCodeConfig) func(http.Handler) http.Handler
- func OAuth2ClientCredentialsAuth(config OAuth2ClientCredentialsConfig) func(http.Handler) http.Handler
- func OAuth2DeviceAuth(config OAuth2DeviceConfig) func(http.Handler) http.Handler
- func OAuth2ImplicitAuth(config OAuth2ImplicitConfig) func(http.Handler) http.Handler
- func OAuth2TokenAuth(config OAuth2TokenConfig) func(http.Handler) http.Handler
- func OpenIDConnectAuth(config OpenIDConnectAuthConfig) func(http.Handler) http.Handler
- func RequireAllScopes(requiredScopes ...string) func(http.Handler) http.Handler
- func RequireAnyScopes(requiredScopes ...string) func(http.Handler) http.Handler
- type APIKeyAuthConfig
- type BasicAuthConfig
- type BearerAuthConfig
- type Config
- type DigestAuthConfig
- type MutualTLSAuthConfig
- type OAuth2AuthorizationCodeConfig
- type OAuth2BaseConfig
- type OAuth2ClientCredentialsConfig
- type OAuth2Config
- type OAuth2DeviceCode
- type OAuth2DeviceConfig
- type OAuth2FlowType
- type OAuth2ImplicitConfig
- type OAuth2PKCEConfig
- type OAuth2Token
- type OAuth2TokenConfig
- type OAuth2TokenKey
- type OIDCToken
- type OIDCTokenKey
- type OpenIDConnectAuthConfig
- type PKCEChallengeMethod
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func APIKeyAuth ¶
func APIKeyAuth(config APIKeyAuthConfig) func(http.Handler) http.Handler
APIKeyAuth returns a middleware that enforces API Key Authentication.
func BasicAuth ¶
func BasicAuth(config BasicAuthConfig) func(http.Handler) http.Handler
BasicAuth returns a middleware that enforces HTTP Basic Authentication.
func BearerAuth ¶
func BearerAuth(config BearerAuthConfig) func(http.Handler) http.Handler
BearerAuth returns a middleware that enforces HTTP Bearer Token Authentication.
func DigestAuth ¶
func DigestAuth(config DigestAuthConfig) func(http.Handler) http.Handler
DigestAuth returns a middleware that enforces HTTP Digest Authentication.
func MutualTLSAuth ¶
func MutualTLSAuth(config MutualTLSAuthConfig) func(http.Handler) http.Handler
MutualTLSAuth returns a middleware that enforces Mutual TLS Authentication.
func OAuth2AuthorizationCodeAuth ¶
func OAuth2AuthorizationCodeAuth(config OAuth2AuthorizationCodeConfig) func(http.Handler) http.Handler
OAuth2AuthorizationCodeAuth returns middleware for OAuth2 Authorization Code flow.
func OAuth2ClientCredentialsAuth ¶
func OAuth2ClientCredentialsAuth(config OAuth2ClientCredentialsConfig) func(http.Handler) http.Handler
OAuth2ClientCredentialsAuth returns middleware for OAuth2 Client Credentials flow.
func OAuth2DeviceAuth ¶
func OAuth2DeviceAuth(config OAuth2DeviceConfig) func(http.Handler) http.Handler
OAuth2DeviceAuth returns middleware for OAuth2 Device Authorization Grant flow.
func OAuth2ImplicitAuth ¶
func OAuth2ImplicitAuth(config OAuth2ImplicitConfig) func(http.Handler) http.Handler
OAuth2ImplicitAuth returns middleware for OAuth2 Implicit flow.
func OAuth2TokenAuth ¶
func OAuth2TokenAuth(config OAuth2TokenConfig) func(http.Handler) http.Handler
OAuth2TokenAuth returns middleware that validates OAuth2 Bearer tokens.
func OpenIDConnectAuth ¶
func OpenIDConnectAuth(config OpenIDConnectAuthConfig) func(http.Handler) http.Handler
OpenIDConnectAuth returns a middleware that enforces OpenID Connect Authentication. If redirect fields are configured, it will redirect users to authenticate. Otherwise, it validates existing Bearer tokens.
func RequireAllScopes ¶
RequireAllScopes returns middleware that requires ALL of the specified scopes. The token must have every scope in the requiredScopes slice.
Types ¶
type APIKeyAuthConfig ¶
type APIKeyAuthConfig struct {
// KeyValidator is called with the API key, should return true if valid
KeyValidator func(key string) bool
// KeyName is the name of the API key parameter (default: "api_key")
KeyName string
// KeyLocation specifies where to look for the API key: "header", "query", "cookie"
KeyLocation string
UnauthorizedHandler http.Handler
}
APIKeyAuthConfig holds configuration for API key authentication middleware.
type BasicAuthConfig ¶
type BasicAuthConfig struct {
// Authenticator is called with username and password, should return true if valid
Authenticator func(username, password string) bool
// Realm is the authentication realm (default: "Restricted")
Realm string
UnauthorizedHandler http.Handler
}
BasicAuthConfig holds configuration for basic authentication middleware.
type BearerAuthConfig ¶
type BearerAuthConfig struct {
// TokenValidator is called with the bearer token, should return true if valid
TokenValidator func(token string) bool
UnauthorizedHandler http.Handler
}
BearerAuthConfig holds configuration for bearer token authentication middleware.
type Config ¶
type Config struct {
// AllowAnonymousAuth indicates whether anonymous (unauthenticated) access is allowed.
AllowAnonymousAuth bool
// APIKeyAuth configures API Key authentication settings.
APIKeyAuth *APIKeyAuthConfig
// BasicAuth configures Basic authentication settings.
BasicAuth *BasicAuthConfig
// BearerAuth configures Bearer authentication settings.
BearerAuth *BearerAuthConfig
// DigestAuth configures Digest authentication settings.
DigestAuth *DigestAuthConfig
// MutualTLSAuthConfig configures Mutual TLS authentication settings.
MutualTLSAuth *MutualTLSAuthConfig
// OAuth2AuthorizationCode configures OAuth2 Authorization Code flow settings.
OAuth2AuthorizationCode *OAuth2AuthorizationCodeConfig
// OAuth2ClientCredentials configures OAuth2 Client Credentials flow settings.
OAuth2ClientCredentials *OAuth2ClientCredentialsConfig
// OAuth2Device configures OAuth2 Device Code flow settings.
OAuth2Device *OAuth2DeviceConfig
// OAuth2Implicit configures OAuth2 Implicit flow settings.
OAuth2Implicit *OAuth2ImplicitConfig
// OpenIDConnectAuth configures OpenID Connect authentication settings.
OpenIDConnectAuth *OpenIDConnectAuthConfig
}
type DigestAuthConfig ¶
type DigestAuthConfig struct {
// Realm is the authentication realm
Realm string
// PasswordGetter is called with username and realm, should return the password and true if user exists
PasswordGetter func(username, realm string) (password string, ok bool)
// NonceTTL is the time-to-live for nonces (default 30 minutes)
NonceTTL time.Duration
UnauthorizedHandler http.Handler
}
DigestAuthConfig holds configuration for digest authentication middleware.
type MutualTLSAuthConfig ¶
type MutualTLSAuthConfig struct {
// CertificateValidator is called with the client certificate, should return true if valid
CertificateValidator func(cert *x509.Certificate) bool
UnauthorizedHandler http.Handler
}
MutualTLSAuthConfig holds configuration for mutual TLS authentication middleware.
type OAuth2AuthorizationCodeConfig ¶
type OAuth2AuthorizationCodeConfig struct {
OAuth2BaseConfig
// ClientSecret is the OAuth2 client secret
ClientSecret string
// AuthorizationURL is the OAuth2 authorization endpoint
AuthorizationURL string
// RedirectURL is the OAuth2 redirect URI
RedirectURL string
// StateStore stores/retrieves OAuth2 state parameters
StateStore func(state string) (redirectURL string, ok bool)
// TokenStore stores/retrieves OAuth2 tokens
TokenStore func(sessionID string) (*OAuth2Token, bool)
// SessionIDExtractor extracts session ID from request
SessionIDExtractor func(r *http.Request) string
// PKCE configuration (optional)
PKCE *OAuth2PKCEConfig
}
OAuth2AuthorizationCodeConfig holds configuration for Authorization Code flow.
type OAuth2BaseConfig ¶
type OAuth2BaseConfig struct {
// ClientID is the OAuth2 client identifier
ClientID string
// TokenURL is the OAuth2 token endpoint
TokenURL string
// Scopes are the requested OAuth2 scopes
Scopes []string
// TokenValidator validates access tokens
TokenValidator func(token string) bool
UnauthorizedHandler http.Handler
// RefreshBuffer is the time buffer before expiration to trigger refresh (default: 5 minutes)
RefreshBuffer time.Duration
}
OAuth2BaseConfig holds common OAuth2 configuration fields.
type OAuth2ClientCredentialsConfig ¶
type OAuth2ClientCredentialsConfig struct {
OAuth2BaseConfig
// ClientSecret is the OAuth2 client secret
ClientSecret string
// TokenStore stores/retrieves OAuth2 tokens (optional for caching)
TokenStore func(sessionID string) (*OAuth2Token, bool)
// SessionIDExtractor extracts session ID from request (optional)
SessionIDExtractor func(r *http.Request) string
}
OAuth2ClientCredentialsConfig holds configuration for Client Credentials flow.
type OAuth2Config ¶
type OAuth2Config struct {
// ClientID is the OAuth2 client identifier
ClientID string
// ClientSecret is the OAuth2 client secret
ClientSecret string
// AuthorizationURL is the OAuth2 authorization endpoint
AuthorizationURL string
// TokenURL is the OAuth2 token endpoint
TokenURL string
// RedirectURL is the OAuth2 redirect URI
RedirectURL string
// Scopes are the requested OAuth2 scopes
Scopes []string
// TokenValidator validates access tokens
TokenValidator func(token string) bool
// StateStore stores/retrieves OAuth2 state parameters
StateStore func(state string) (redirectURL string, ok bool)
// TokenStore stores/retrieves OAuth2 tokens
TokenStore func(sessionID string) (*OAuth2Token, bool)
// SessionIDExtractor extracts session ID from request
SessionIDExtractor func(r *http.Request) string
UnauthorizedHandler http.Handler
// RefreshBuffer is the time buffer before expiration to trigger refresh (default: 5 minutes)
RefreshBuffer time.Duration
}
OAuth2Config holds common OAuth2 configuration (deprecated - use flow-specific configs).
type OAuth2DeviceCode ¶
type OAuth2DeviceCode struct {
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationURI string `json:"verification_uri"`
VerificationURIComplete string `json:"verification_uri_complete,omitempty"`
ExpiresIn int `json:"expires_in"`
Interval int `json:"interval,omitempty"`
}
OAuth2DeviceCode represents a device authorization response.
type OAuth2DeviceConfig ¶
type OAuth2DeviceConfig struct {
OAuth2BaseConfig
}
OAuth2DeviceConfig holds configuration for Device Authorization Grant flow.
type OAuth2FlowType ¶
type OAuth2FlowType string
OAuth2FlowType represents the different OAuth2 flow types.
const ( OAuth2FlowAuthorizationCode OAuth2FlowType = "authorization_code" OAuth2FlowImplicit OAuth2FlowType = "implicit" OAuth2FlowDevice OAuth2FlowType = "device_code" OAuth2FlowClientCredentials OAuth2FlowType = "client_credentials" )
type OAuth2ImplicitConfig ¶
type OAuth2ImplicitConfig struct {
OAuth2BaseConfig
// AuthorizationURL is the OAuth2 authorization endpoint
AuthorizationURL string
// RedirectURL is the OAuth2 redirect URI
RedirectURL string
// StateStore stores/retrieves OAuth2 state parameters
StateStore func(state string) (redirectURL string, ok bool)
}
OAuth2ImplicitConfig holds configuration for Implicit flow.
type OAuth2PKCEConfig ¶
type OAuth2PKCEConfig struct {
// CodeVerifierStore stores/retrieves PKCE code verifiers by state
CodeVerifierStore func(state string) (codeVerifier string, ok bool)
// ChallengeMethod specifies the PKCE challenge method ("S256" or "plain")
ChallengeMethod PKCEChallengeMethod
}
OAuth2PKCEConfig holds PKCE (Proof Key for Code Exchange) configuration.
type OAuth2Token ¶
type OAuth2Token struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token,omitempty"`
Scope string `json:"scope,omitempty"`
IssuedAt time.Time `json:"issued_at,omitempty"` // When token was issued
ExpiresAt time.Time `json:"expires_at,omitempty"` // When token expires
}
OAuth2Token represents an OAuth2 token response with expiration tracking.
func (*OAuth2Token) IsExpired ¶
func (t *OAuth2Token) IsExpired(buffer time.Duration) bool
IsExpired checks if the access token is expired with a buffer.
func (*OAuth2Token) NeedsRefresh ¶
func (t *OAuth2Token) NeedsRefresh(buffer time.Duration) bool
NeedsRefresh checks if the token should be refreshed (expired or close to expiring).
type OAuth2TokenConfig ¶
type OAuth2TokenConfig struct {
// TokenValidator validates access tokens
TokenValidator func(token string) bool
UnauthorizedHandler http.Handler
}
OAuth2TokenConfig holds configuration for simple token validation.
type OAuth2TokenKey ¶
type OAuth2TokenKey struct{}
OAuth2TokenKey is the context key for OAuth2 tokens.
type OIDCToken ¶
type OIDCToken struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
IDToken string `json:"id_token"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token,omitempty"`
Scope string `json:"scope,omitempty"`
IssuedAt time.Time `json:"issued_at,omitempty"` // When token was issued
ExpiresAt time.Time `json:"expires_at,omitempty"` // When token expires
}
OIDCToken represents an OpenID Connect token response with expiration tracking.
type OpenIDConnectAuthConfig ¶
type OpenIDConnectAuthConfig struct {
// TokenValidator validates ID tokens (for simple token validation)
TokenValidator func(token string) bool
UnauthorizedHandler http.Handler
// Fields for full OIDC flow (optional - if provided, enables redirect)
IssuerURL string // OIDC provider issuer URL
ClientID string // OIDC client ID
ClientSecret string // OIDC client secret
RedirectURL string // Callback URL
Scopes []string // Requested scopes (default: ["openid"])
// State management (required for redirect flow)
StateStore func(state string) (redirectURL string, ok bool)
// Token storage (optional)
TokenStore func(sessionID string) (*OIDCToken, bool)
SessionIDExtractor func(r *http.Request) string
// RefreshBuffer is the time buffer before expiration to trigger refresh (default: 5 minutes)
RefreshBuffer time.Duration
}
OpenIDConnectAuthConfig holds configuration for OpenID Connect authentication middleware.
type PKCEChallengeMethod ¶
type PKCEChallengeMethod string
PKCE challenge methods
const ( PKCES256 PKCEChallengeMethod = "S256" PKCEPlain PKCEChallengeMethod = "plain" )