Documentation
¶
Overview ¶
Package caddy_oidc is a Caddy plugin for providing authentication and authorization using an OIDC IdP
Index ¶
- Constants
- Variables
- func MatchWildcard(pattern string, value string) bool
- type Action
- type App
- type ClaimMatch
- type EvaluationResult
- type MatchAnonymous
- type MatchAuthMethod
- type MatchClaim
- type MatchUser
- type OAuthProtectedResource
- type OIDCMiddleware
- func (mw *OIDCMiddleware) CaddyModule() caddy.ModuleInfo
- func (mw *OIDCMiddleware) Provision(ctx caddy.Context) error
- func (mw *OIDCMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error
- func (mw *OIDCMiddleware) UnmarshalCaddyfile(dis *caddyfile.Dispenser) error
- func (mw *OIDCMiddleware) Validate() error
- type OIDCProviderModule
- func (*OIDCProviderModule) CaddyModule() caddy.ModuleInfo
- func (m *OIDCProviderModule) Create(ctx caddy.Context) (*Provider, error)
- func (m *OIDCProviderModule) Provision(ctx caddy.Context) error
- func (m *OIDCProviderModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
- func (m *OIDCProviderModule) Validate() error
- type ProtectedResourceMetadataConfiguration
- type Provider
- func (pr *Provider) AuthCodeURL(ctx context.Context, state string, opts ...oauth2.AuthCodeOption) (string, error)
- func (pr *Provider) Exchange(ctx context.Context, code string, opts ...oauth2.AuthCodeOption) (*oauth2.Token, error)
- func (pr *Provider) GetUsernameClaim() string
- func (pr *Provider) GetVerifier(ctx context.Context) (*oidc.IDTokenVerifier, error)
- func (pr *Provider) Now() time.Time
- func (pr *Provider) ProtectedResourceMetadata(r *http.Request) (*OAuthProtectedResource, bool)
- func (pr *Provider) ServeHTTPOAuthProtectedResource(rw http.ResponseWriter, r *http.Request) error
- func (pr *Provider) UserInfo(ctx context.Context, tokenSource oauth2.TokenSource) (*oidc.UserInfo, error)
- type Rule
- type RuleEvaluation
- type Ruleset
Constants ¶
const ( // SessionCtxKey is the context key used to store the authentication session object. // The context value is of type *Session. SessionCtxKey caddy.CtxKey = "oidc_session" // AuthMethodCtxKey is the context key used to store the authentication method used for the incoming request. // The context value is of type AuthMethod. AuthMethodCtxKey caddy.CtxKey = "oidc_auth_method" )
const (
// DefaultUsernameClaim is the default username claim to use for the BearerAuthenticator if none is specified.
DefaultUsernameClaim = "sub"
)
const WellKnownOAuthProtectedResourcePath = "/.well-known/oauth-protected-resource"
WellKnownOAuthProtectedResourcePath is the path for the OAuth protected resource metadata endpoint.
Variables ¶
var ErrAccessDenied = errors.New("access denied")
ErrAccessDenied is returned when the request is denied access.
var ErrInvalidAction = errors.New("not a valid Action")
var ErrInvalidEvaluationResult = errors.New("not a valid EvaluationResult")
Functions ¶
func MatchWildcard ¶
MatchWildcard matches a possible wildcard pattern against a value. Uses the same wildcard matching logic as caddyhttp.MatchHeader.
Types ¶
type Action ¶
type Action uint8
Action represents the possible actions to take when a rule is matched. ENUM(allow, deny)
func ParseAction ¶
ParseAction attempts to convert a string to a Action.
func (*Action) AppendText ¶
AppendText appends the textual representation of itself to the end of b (allocating a larger slice if necessary) and returns the updated slice.
Implementations must not retain b, nor mutate any bytes within b[:len(b)].
func (Action) IsValid ¶
IsValid provides a quick way to determine if the typed value is part of the allowed enumerated values
func (Action) MarshalText ¶
MarshalText implements the text marshaller method.
func (*Action) UnmarshalText ¶
UnmarshalText implements the text unmarshaller method.
type App ¶
type App struct {
// Default contains the default / baseline OIDC configuration for this App.
// The Default is used as a baseline configuration during caddyfile unmarshalling of named providers
// and can be referenced directly in an OIDCMiddleware when a provider is not defined.
Default OIDCProviderModule `json:"default"`
Providers map[string]*OIDCProviderModule `json:"providers,omitempty"`
// contains filtered or unexported fields
}
App holds configuration for all the named OIDC providers within a Caddy configuration.
func (*App) CaddyModule ¶
func (*App) CaddyModule() caddy.ModuleInfo
func (*App) ProvisionProvider ¶
type ClaimMatch ¶
A ClaimMatch represents a claim name and a list of (optional) allowed values for that claim.
func (*ClaimMatch) MatchWithRepl ¶
MatchWithRepl matches the session claims against the claim match. Claims must be a valid gjson result containing a JSON object. If there are no values to match, MatchWithRepl returns true as long as the claim exists. Otherwise, at least one value must match. Both names and values of the ClaimMatch are pre-processed using the replacer.
type EvaluationResult ¶
type EvaluationResult uint8
EvaluationResult represents the possible results of ruleset evaluation. ENUM(implicit deny, explicit deny, allow)
const ( // EvaluationResultImplicitDeny is a EvaluationResult of type Implicit Deny. EvaluationResultImplicitDeny EvaluationResult = iota // EvaluationResultExplicitDeny is a EvaluationResult of type Explicit Deny. EvaluationResultExplicitDeny // EvaluationResultAllow is a EvaluationResult of type Allow. EvaluationResultAllow )
func ParseEvaluationResult ¶
func ParseEvaluationResult(name string) (EvaluationResult, error)
ParseEvaluationResult attempts to convert a string to a EvaluationResult.
func (*EvaluationResult) AppendText ¶
func (x *EvaluationResult) AppendText(b []byte) ([]byte, error)
AppendText appends the textual representation of itself to the end of b (allocating a larger slice if necessary) and returns the updated slice.
Implementations must not retain b, nor mutate any bytes within b[:len(b)].
func (EvaluationResult) IsValid ¶
func (x EvaluationResult) IsValid() bool
IsValid provides a quick way to determine if the typed value is part of the allowed enumerated values
func (EvaluationResult) MarshalText ¶
func (x EvaluationResult) MarshalText() ([]byte, error)
MarshalText implements the text marshaller method.
func (EvaluationResult) String ¶
func (x EvaluationResult) String() string
String implements the Stringer interface.
func (*EvaluationResult) UnmarshalText ¶
func (x *EvaluationResult) UnmarshalText(text []byte) error
UnmarshalText implements the text unmarshaller method.
type MatchAnonymous ¶
type MatchAnonymous struct{}
MatchAnonymous matches requests that are anonymous or do not have a valid session in the request context.
func (*MatchAnonymous) CaddyModule ¶
func (*MatchAnonymous) CaddyModule() caddy.ModuleInfo
func (*MatchAnonymous) MatchWithError ¶
func (*MatchAnonymous) MatchWithError(r *http.Request) (bool, error)
func (*MatchAnonymous) UnmarshalCaddyfile ¶
func (*MatchAnonymous) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
type MatchAuthMethod ¶
type MatchAuthMethod struct {
Match []authenticator.AuthMethod `json:"match,omitempty"`
}
MatchAuthMethod matches the authentication method used for the incoming request.
func (*MatchAuthMethod) CaddyModule ¶
func (*MatchAuthMethod) CaddyModule() caddy.ModuleInfo
func (*MatchAuthMethod) MatchWithError ¶
func (m *MatchAuthMethod) MatchWithError(r *http.Request) (bool, error)
func (*MatchAuthMethod) UnmarshalCaddyfile ¶
func (m *MatchAuthMethod) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
type MatchClaim ¶
type MatchClaim []ClaimMatch
MatchClaim matches claims in a request session. The claim value in the session must be a string or an array of strings. If the claim value is an array, the match succeeds if any of the values match.
func (*MatchClaim) CaddyModule ¶
func (*MatchClaim) CaddyModule() caddy.ModuleInfo
func (*MatchClaim) MatchWithError ¶
func (m *MatchClaim) MatchWithError(r *http.Request) (bool, error)
func (*MatchClaim) UnmarshalCaddyfile ¶
func (m *MatchClaim) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
type MatchUser ¶
type MatchUser struct {
Usernames []string `json:"usernames,omitempty"`
}
MatchUser matches the request against a list of wildcard-matched usernames present within the session stored in the incoming context. If the session is anonymous, no usernames are considered and the match always fails.
func (*MatchUser) CaddyModule ¶
func (*MatchUser) CaddyModule() caddy.ModuleInfo
func (*MatchUser) MatchWithError ¶
type OAuthProtectedResource ¶
type OAuthProtectedResource struct {
Resource string `json:"resource"`
AuthorizationServers []string `json:"authorization_servers"`
ScopesSupported []string `json:"scopes_supported"`
BearerMethodsSupported []string `json:"bearer_methods_supported,omitempty"`
// Audience is a custom extension to the OAuth Protected Resource Metadata spec.
Audience string `json:"audience,omitempty"`
}
OAuthProtectedResource is the JSON payload sent from /.well-known/oauth-protected-resource or advertised in WWW-Authenticate on 401 responses.
func (*OAuthProtectedResource) WWWAuthenticate ¶
func (md *OAuthProtectedResource) WWWAuthenticate() string
WWWAuthenticate returns the value of the WWW-Authenticate header for this resource. https://datatracker.ietf.org/doc/html/rfc9728#name-use-of-www-authenticate-for https://datatracker.ietf.org/doc/html/rfc6750#section-3
type OIDCMiddleware ¶
type OIDCMiddleware struct {
// The provider to use.
// If left empty, the default provider configuration is used.
ProviderName string `json:"provider,omitempty"`
Policies Ruleset `json:"policies"`
Provider *Provider `json:"-"`
}
OIDCMiddleware is a middleware that authenticates and authorizes requests based on configured rules. It's associated with a separately configured OIDC provider by name.
func (*OIDCMiddleware) CaddyModule ¶
func (mw *OIDCMiddleware) CaddyModule() caddy.ModuleInfo
func (*OIDCMiddleware) Provision ¶
func (mw *OIDCMiddleware) Provision(ctx caddy.Context) error
Provision sets up the OIDCMiddleware by loading the configured OIDC provider and then provisioning the configured ruleset for the middleware. The named provider must be configured.
func (*OIDCMiddleware) ServeHTTP ¶
func (mw *OIDCMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, next caddyhttp.Handler) error
ServeHTTP implements caddyhttp.MiddlewareHandler. It wraps interceptRequest to handle errors to ensure any error returned is a caddyhttp.HandlerError. Without this, Caddy's error_directive does not properly set error replacer vars, which can result in HTTP 200 responses when it tries to parse `{err.status_code}`.
func (*OIDCMiddleware) UnmarshalCaddyfile ¶
func (mw *OIDCMiddleware) UnmarshalCaddyfile(dis *caddyfile.Dispenser) error
UnmarshalCaddyfile sets up the OIDCMiddleware from Caddyfile tokens.
oidc [example] {
allow|deny {
...
}
}
func (*OIDCMiddleware) Validate ¶
func (mw *OIDCMiddleware) Validate() error
Validate validates the configuration of the OIDCMiddleware.
type OIDCProviderModule ¶
type OIDCProviderModule struct {
Issuer string `json:"issuer"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret,omitempty"`
Scope []string `json:"scope,omitempty"`
Username string `json:"username,omitempty"`
Authenticators *authenticator.Set `json:"authenticators,omitempty"`
TLSInsecureSkipVerify bool `json:"tls_insecure_skip_verify,omitempty"`
ProtectedResourceMetadata *ProtectedResourceMetadataConfiguration `json:"protected_resource_metadata,omitempty"`
// TokenParams is an arbitrary map of additional key-values to set as URL parameters
// when performing a code exchange. Values support Caddy placeholders such as
// {file./path/to/secret} and {env.VAR} which are resolved at exchange time.
TokenParams map[string]string `json:"token_params,omitempty"`
}
OIDCProviderModule holds the configuration for an OIDC provider.
func (*OIDCProviderModule) CaddyModule ¶
func (*OIDCProviderModule) CaddyModule() caddy.ModuleInfo
func (*OIDCProviderModule) Create ¶
func (m *OIDCProviderModule) Create(ctx caddy.Context) (*Provider, error)
Create creates a Provider instance from this provider module configuration.
func (*OIDCProviderModule) Provision ¶
func (m *OIDCProviderModule) Provision(ctx caddy.Context) error
func (*OIDCProviderModule) UnmarshalCaddyfile ¶
func (m *OIDCProviderModule) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
UnmarshalCaddyfile sets up the OIDCProviderModule instance from Caddyfile tokens.
{
issuer <issuer>
client_id <client_id>
authenticate <authenticator>
tls_insecure_skip_verify
scope [<scope>...]
protected_resource <protected_resource>
}
func (*OIDCProviderModule) Validate ¶
func (m *OIDCProviderModule) Validate() error
type ProtectedResourceMetadataConfiguration ¶
type ProtectedResourceMetadataConfiguration struct {
Disable bool `json:"disable"`
Audience bool `json:"audience,omitempty"`
}
ProtectedResourceMetadataConfiguration configures the protected resource metadata endpoint.
func (*ProtectedResourceMetadataConfiguration) UnmarshalCaddyfile ¶
func (c *ProtectedResourceMetadataConfiguration) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
UnmarshalCaddyfile sets up the ProtectedResourceMetadataConfiguration from Caddyfile tokens.
protected_resource_metadata disable | {
audience
}
type Provider ¶
type Provider struct {
Log *zap.Logger
Clock func() time.Time
Issuer string
UsernameClaim string
ProtectedResource *ProtectedResourceMetadataConfiguration
Authenticators authenticator.Set
Discovery *deferred.Result[*providerDiscoveryConfiguration]
}
Provider holds the built configuration for an OIDC provider and authentication logic.
func (*Provider) AuthCodeURL ¶
func (*Provider) GetUsernameClaim ¶
func (*Provider) GetVerifier ¶
func (*Provider) ProtectedResourceMetadata ¶
func (pr *Provider) ProtectedResourceMetadata(r *http.Request) (*OAuthProtectedResource, bool)
ProtectedResourceMetadata returns the OAuth protected resource metadata for this authenticator. If protected resource metadata is not enabled, then false is returned.
func (*Provider) ServeHTTPOAuthProtectedResource ¶
ServeHTTPOAuthProtectedResource returns the OAuth protected resource metadata for the endpoint .well-known/oauth-protected-resource. If the endpoint is disabled, then a 404 not found response is returned.
type Rule ¶
type Rule struct {
ID string `json:"id,omitempty"`
Action Action `json:"action"`
MatcherSetsRaw caddy.ModuleMap `caddy:"namespace=http.matchers" json:"match,omitempty"`
Matchers caddyhttp.MatcherSet `json:"-"`
}
A Rule represents a single authorization rule with an associated action to take when matched with a request.
func (*Rule) MatchWithError ¶
MatchWithError returns true if the request matches the rule. Unlike caddyhttp.MatcherSets, an empty matcher set never matches a request.
type RuleEvaluation ¶
type RuleEvaluation struct {
Result EvaluationResult `json:"result"`
// The optional ID of the matched rule.
// If the result is EvaluationResultImplicitDeny, this field is always empty.
RuleID string `json:"rule_id"`
}
RuleEvaluation represents the result of evaluating a ruleset.
type Ruleset ¶
type Ruleset []*Rule
A Ruleset is a set of authorization rules.
func (*Ruleset) ContainsAllow ¶
ContainsAllow returns true if the set contains at least one ActionAllow rule.
func (*Ruleset) Evaluate ¶
func (rules *Ruleset) Evaluate(r *http.Request) (RuleEvaluation, error)
Evaluate all rules in the set and return the evaluation result. At least one allow rule must match to return EvaluationResultAllow. If any "deny" rule is matched, return EvaluationResultExplicitDeny.
func (*Ruleset) UnmarshalCaddyfile ¶
UnmarshalCaddyfile sets up the Ruleset from Caddyfile tokens. Syntax:
allow|deny <rule_id> {
...
}
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package authenticator provides a modular plugin interface for providing authentication mechanisms to the caddy-oidc plugin.
|
Package authenticator provides a modular plugin interface for providing authentication mechanisms to the caddy-oidc plugin. |
|
internal
|
|
|
deferred
Package deferred provides a simple way to defer a function call in a separate goroutine and allow multiple callers to wait for the result.
|
Package deferred provides a simple way to defer a function call in a separate goroutine and allow multiple callers to wait for the result. |
|
lazy
Package lazy provides one-shot lazy initialization of a value.
|
Package lazy provides one-shot lazy initialization of a value. |
|
pkgtest
Package pkgtest provides utilities for testing.
|
Package pkgtest provides utilities for testing. |
|
Package request provides utilities for working with HTTP requests.
|
Package request provides utilities for working with HTTP requests. |
|
Package session contains types and functions for working with authentication sessions.
|
Package session contains types and functions for working with authentication sessions. |