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(d *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) UnmarshalCaddyfileToken(d *caddyfile.Dispenser) (bool, 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) (template.TokenVerifier, 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
- func (rules *Ruleset) ContainsAllow() bool
- func (rules *Ruleset) Evaluate(r *http.Request) (RuleEvaluation, error)
- func (rules *Ruleset) Provision(ctx caddy.Context) error
- func (rules *Ruleset) UnmarshalCaddyfile(d *caddyfile.Dispenser) error
- func (rules *Ruleset) UnmarshalCaddyfileToken(d *caddyfile.Dispenser) (bool, error)
- func (rules *Ruleset) Validate() error
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 ¶ added in v0.2.1
ParseAction attempts to convert a string to a Action.
func (*Action) AppendText ¶ added in v0.2.1
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 ¶ added in v0.2.1
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"`
}
App holds configuration for all the named OIDC providers within a Caddy configuration.
func (*App) CaddyModule ¶
func (*App) CaddyModule() caddy.ModuleInfo
func (*App) GetInheritedProvider ¶ added in v0.4.0
func (a *App) GetInheritedProvider(name string) (*OIDCProviderModule, error)
GetInheritedProvider returns the OIDCProviderModule for the given name. If the name is empty, then the default provider is returned. If the named provider is not configured, then an error is returned.
If a named provider is configured, then the baseline configuration is applied to the provider from the application global default provider configuration.
The caller must not modify the returned provider.
type ClaimMatch ¶
A ClaimMatch represents a claim name and a list of (optional) allowed values for that claim.
func (*ClaimMatch) MatchWithRepl ¶ added in v0.3.0
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 ¶ added in v0.2.1
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 ¶ added in v0.2.1
func ParseEvaluationResult(name string) (EvaluationResult, error)
ParseEvaluationResult attempts to convert a string to a EvaluationResult.
func (*EvaluationResult) AppendText ¶ added in v0.2.1
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 ¶ added in v0.2.1
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 ¶ added in v0.2.1
func (x EvaluationResult) MarshalText() ([]byte, error)
MarshalText implements the text marshaller method.
func (EvaluationResult) String ¶ added in v0.2.1
func (x EvaluationResult) String() string
String implements the Stringer interface.
func (*EvaluationResult) UnmarshalText ¶ added in v0.2.1
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 ¶ added in v0.2.3
type MatchAuthMethod struct {
Match []authenticator.AuthMethod `json:"match,omitempty"`
}
MatchAuthMethod matches the authentication method used for the incoming request.
func (*MatchAuthMethod) CaddyModule ¶ added in v0.2.3
func (*MatchAuthMethod) CaddyModule() caddy.ModuleInfo
func (*MatchAuthMethod) MatchWithError ¶ added in v0.2.3
func (m *MatchAuthMethod) MatchWithError(r *http.Request) (bool, error)
func (*MatchAuthMethod) UnmarshalCaddyfile ¶ added in v0.2.3
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 {
OIDCProviderModule
// Inherits is the name of a globally configured OIDC provider to inherit settings from.
// The inherited configuration will be merged with the local configuration.
Inherits string `json:"inherits,omitempty"`
Policies Ruleset `json:"policies"`
// contains filtered or unexported fields
}
OIDCMiddleware is a middleware that authenticates and authorizes requests based on configured rules. It contains its own OIDC provider configuration. During provisioning, it applies the inherited baseline configuration to the local configuration.
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(d *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) UnmarshalCaddyfileToken ¶ added in v0.4.0
func (m *OIDCProviderModule) UnmarshalCaddyfileToken(d *caddyfile.Dispenser) (bool, error)
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 ¶ added in v0.3.0
type Provider struct {
Log *zap.Logger
Clock func() time.Time
Issuer string
UsernameClaim string
ProtectedResource *ProtectedResourceMetadataConfiguration
Authenticators authenticator.Set
Discovery *deferred.Result[*discoveryConfiguration]
}
Provider holds the built configuration for an OIDC provider and authentication logic.
func (*Provider) AuthCodeURL ¶ added in v0.3.0
func (*Provider) GetUsernameClaim ¶ added in v0.3.0
func (*Provider) GetVerifier ¶ added in v0.3.0
func (*Provider) ProtectedResourceMetadata ¶ added in v0.3.0
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 ¶ added in v0.3.0
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 ¶ added in v0.2.1
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 ¶ added in v0.2.1
MatchWithError returns true if the request matches the rule. Unlike caddyhttp.MatcherSets, an empty matcher set never matches a request.
type RuleEvaluation ¶ added in v0.2.1
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 ¶ added in v0.2.1
type Ruleset []*Rule
A Ruleset is a set of authorization rules.
func (*Ruleset) ContainsAllow ¶ added in v0.2.1
ContainsAllow returns true if the set contains at least one ActionAllow rule.
func (*Ruleset) Evaluate ¶ added in v0.2.1
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 ¶ added in v0.2.1
UnmarshalCaddyfile sets up the Ruleset from Caddyfile tokens. Syntax:
allow|deny <rule_id> {
...
}
func (*Ruleset) UnmarshalCaddyfileToken ¶ added in v0.4.0
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
|
|
|
baseline
Package baseline provides a mechanism for applying baseline overrides to Go structs
|
Package baseline provides a mechanism for applying baseline overrides to Go structs |
|
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. |
|
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. |
|
Package template is an internal package that provides primitives for Caddy replacer replaced values.
|
Package template is an internal package that provides primitives for Caddy replacer replaced values. |