auth

package
v0.2.31 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 30, 2026 License: Apache-2.0 Imports: 38 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	COOKIE = "cookie"
	HTTPS  = "https"
	TRUE   = "true"
)

Variables

This section is empty.

Functions

func AuthClientLoader

func AuthClientLoader(secrets kdexv1alpha1.Secrets) (map[string]AuthClient, error)

func DiscoveryHandler

func DiscoveryHandler(issuer string) http.HandlerFunc

DiscoveryHandler creates an HTTP handler that serves the OpenID discovery document.

func FormatADGUID

func FormatADGUID(b []byte) (string, error)

FormatADGUID converts the raw binary objectGUID from AD into a standard UUID string. AD uses a little-endian format for the first three components.

func JWKSHandler

func JWKSHandler(keyPairs *keys.KeyPairs) http.HandlerFunc

JWKSHandler creates an HTTP handler that serves the JWKS endpoint. This endpoint exposes the public key(s) used to verify JWT signatures.

func NewLDAPLookup

func NewLDAPLookup(secret corev1.Secret) *ldapLookup

func NewRoleProvider

func NewRoleProvider(
	ctx context.Context,
	c client.Client,
	focalHost string,
	controllerNamespace string,
	lookups []Lookup,
) (*scopeProvider, error)

func NewSecretLookup

func NewSecretLookup(secrets kdexv1alpha1.Secrets) *secretLookup

func SetAuthContext

func SetAuthContext(ctx context.Context, ac AuthContext) context.Context

SetAuthContext sets the auth context in the context.

Types

type AuthClient

type AuthClient struct {
	AllowedGrantTypes []string
	AllowedScopes     []string
	ClientID          string
	ClientSecret      string
	Description       string
	Name              string
	Public            bool
	RedirectURIs      []string
	RequirePKCE       bool
}

type AuthContext

type AuthContext jwt.MapClaims

func GetAuthContext

func GetAuthContext(ctx context.Context) (AuthContext, bool)

GetAuthContext retrieves the claims from the context.

func (AuthContext) GetAudience

func (ac AuthContext) GetAudience() (jwt.ClaimStrings, error)

GetAudience implements the Claims interface.

func (AuthContext) GetAuthMethod

func (ac AuthContext) GetAuthMethod() (AuthMethod, error)

func (AuthContext) GetEntitlements

func (ac AuthContext) GetEntitlements() ([]string, error)

func (AuthContext) GetExpirationTime

func (ac AuthContext) GetExpirationTime() (*jwt.NumericDate, error)

GetExpirationTime implements the Claims interface.

func (AuthContext) GetIssuedAt

func (ac AuthContext) GetIssuedAt() (*jwt.NumericDate, error)

GetIssuedAt implements the Claims interface.

func (AuthContext) GetIssuer

func (ac AuthContext) GetIssuer() (string, error)

GetIssuer implements the Claims interface.

func (AuthContext) GetNotBefore

func (ac AuthContext) GetNotBefore() (*jwt.NumericDate, error)

GetNotBefore implements the Claims interface.

func (AuthContext) GetRoles

func (ac AuthContext) GetRoles() ([]string, error)

func (AuthContext) GetScopes

func (ac AuthContext) GetScopes() ([]string, error)

func (AuthContext) GetSubject

func (ac AuthContext) GetSubject() (string, error)

GetSubject implements the Claims interface.

type AuthMethod

type AuthMethod string
const (
	AuthMethodLocal  AuthMethod = "local"
	AuthMethodOIDC   AuthMethod = "oidc"
	AuthMethodOAuth2 AuthMethod = "oauth2"
)

type AuthorizationChecker

type AuthorizationChecker struct {
	// contains filtered or unexported fields
}

AuthorizationChecker validates whether a user has the required permissions.

func NewAuthorizationChecker

func NewAuthorizationChecker(anonymousEntitlements []string, log logr.Logger) *AuthorizationChecker

NewAuthorizationChecker creates a new authorization checker.

func (*AuthorizationChecker) CalculateRequirements

func (ac *AuthorizationChecker) CalculateRequirements(
	resource string,
	resourceName string,
	kdexreqs []kdexv1alpha1.SecurityRequirement,
	verbs ...string,
) ([]kdexv1alpha1.SecurityRequirement, error)

func (*AuthorizationChecker) CheckAccess

func (ac *AuthorizationChecker) CheckAccess(
	ctx context.Context,
	resource string,
	resourceName string,
	kdexreqs []kdexv1alpha1.SecurityRequirement,
	verbs ...string,
) (bool, error)

func (*AuthorizationChecker) GetParsedEntitlements added in v0.2.9

func (ac *AuthorizationChecker) GetParsedEntitlements(ctx context.Context) entitlements.ParsedEntitlements

func (*AuthorizationChecker) ParseEntitlements added in v0.2.9

func (*AuthorizationChecker) ParseRequirements added in v0.2.9

func (*AuthorizationChecker) VerifyResourceParsedEntitlements added in v0.2.9

func (ac *AuthorizationChecker) VerifyResourceParsedEntitlements(
	resource string,
	resourceName string,
	parsedEntitlements entitlements.ParsedEntitlements,
	parsedRequirements entitlements.ParsedRequirements,
	verbs ...string,
) (bool, error)

type AuthorizationCodeClaims

type AuthorizationCodeClaims struct {
	AuthMethod          AuthMethod `json:"auth_method"`
	ClientID            string     `json:"cid"`
	CodeChallenge       string     `json:"challenge,omitempty"`
	CodeChallengeMethod string     `json:"challenge_method,omitempty"`
	Exp                 int64      `json:"exp"`
	RedirectURI         string     `json:"uri"`
	Scope               string     `json:"scp"`
	Subject             string     `json:"sub"`
}

type CompiledMappingRule

type CompiledMappingRule struct {
	dmapper.MappingRule
	Program cel.Program
}

type Config

type Config struct {
	ActivePair            *keys.KeyPair
	AnonymousEntitlements []string
	Audience              string
	AutoExtendSession     bool
	Clients               map[string]AuthClient
	CookieName            string
	FunctionURLs          []string
	Issuer                string
	KeyPairs              *keys.KeyPairs
	MaxSessionAge         time.Duration
	OIDC                  struct {
		BlockKey     string
		ClientID     string
		ClientSecret string
		IDTokenStore idtoken.IDTokenStore
		Name         string
		ProviderURL  string
		RedirectURL  string
		Scopes       []string
	}
	RefreshTokenTTL time.Duration
	Signer          sign.Signer
	TokenManager    *apitoken.TokenManager
	TokenTTL        time.Duration
}

func (*Config) AddAuthentication

func (c *Config) AddAuthentication(mux http.Handler, exchanger *Exchanger) http.Handler

func (*Config) IsAuthEnabled

func (c *Config) IsAuthEnabled() bool

func (*Config) IsM2MEnabled

func (c *Config) IsM2MEnabled() bool

func (*Config) IsOIDCEnabled

func (c *Config) IsOIDCEnabled() bool

func (*Config) WithAuthentication added in v0.2.9

func (c *Config) WithAuthentication(exchanger *Exchanger) func(http.Handler) http.Handler

WithAuthentication creates a middleware that validates JWT tokens from the Authorization header. It injects the claims into the request context if the token is valid. If the Header is present but invalid, it returns 401 Unauthorized. If the Header is missing, it proceeds without claims (anonymous access).

type ConfigBuilder added in v0.2.9

type ConfigBuilder struct {
	APITokenManagerLoader  func() (*apitoken.TokenManager, error)
	Audience               string
	AuthClientLoader       func() (map[string]AuthClient, error)
	CacheManager           cache.CacheManager
	DevMode                bool
	Functions              kdexv1alpha1.KDexFunctionList
	Issuer                 string
	KeyLoader              func() (*keys.KeyPairs, error)
	OIDCClientConfigLoader func() (*OIDCClientConfig, error)
}

func NewConfigBuilder added in v0.2.9

func NewConfigBuilder() *ConfigBuilder

func (*ConfigBuilder) Build added in v0.2.9

func (cb *ConfigBuilder) Build(auth *kdexv1alpha1.Auth) (*Config, error)

func (*ConfigBuilder) WithAPITokenManagerLoader added in v0.2.9

func (cb *ConfigBuilder) WithAPITokenManagerLoader(apiTokenManagerLoader func() (*apitoken.TokenManager, error)) *ConfigBuilder

func (*ConfigBuilder) WithAudience added in v0.2.9

func (cb *ConfigBuilder) WithAudience(audience string) *ConfigBuilder

func (*ConfigBuilder) WithAuthClientLoader added in v0.2.9

func (cb *ConfigBuilder) WithAuthClientLoader(authClientLoader func() (map[string]AuthClient, error)) *ConfigBuilder

func (*ConfigBuilder) WithCacheManager added in v0.2.9

func (cb *ConfigBuilder) WithCacheManager(cacheManager cache.CacheManager) *ConfigBuilder

func (*ConfigBuilder) WithDevMode added in v0.2.9

func (cb *ConfigBuilder) WithDevMode(devMode bool) *ConfigBuilder

func (*ConfigBuilder) WithFunctions added in v0.2.9

func (cb *ConfigBuilder) WithFunctions(functions kdexv1alpha1.KDexFunctionList) *ConfigBuilder

func (*ConfigBuilder) WithIssuer added in v0.2.9

func (cb *ConfigBuilder) WithIssuer(issuer string) *ConfigBuilder

func (*ConfigBuilder) WithKeyLoader added in v0.2.9

func (cb *ConfigBuilder) WithKeyLoader(keyLoader func() (*keys.KeyPairs, error)) *ConfigBuilder

func (*ConfigBuilder) WithOIDCClientConfigLoader added in v0.2.9

func (cb *ConfigBuilder) WithOIDCClientConfigLoader(oidcClientConfigLoader func() (*OIDCClientConfig, error)) *ConfigBuilder

type ContextKey

type ContextKey string

ContextKey is a custom type for context keys to avoid collisions.

type Exchanger

type Exchanger struct {
	// contains filtered or unexported fields
}

func NewExchanger

func NewExchanger(
	ctx context.Context,
	cfg Config,
	cacheManager cache.CacheManager,
	sp InternalIdentityProvider,
) (*Exchanger, error)

func (*Exchanger) AuthCodeURL

func (e *Exchanger) AuthCodeURL(state string) string

func (*Exchanger) CreateAuthorizationCode

func (e *Exchanger) CreateAuthorizationCode(ctx context.Context, claims AuthorizationCodeClaims) (string, error)

func (*Exchanger) EndSessionURL

func (e *Exchanger) EndSessionURL() (string, error)

func (*Exchanger) ExchangeCode

func (e *Exchanger) ExchangeCode(ctx context.Context, code string) (string, error)

func (*Exchanger) ExchangeToken

func (e *Exchanger) ExchangeToken(ctx context.Context, rawIDToken string) (string, error)

func (*Exchanger) GetClient

func (e *Exchanger) GetClient(clientID string) (AuthClient, bool)

func (*Exchanger) GetOIDCClientID

func (e *Exchanger) GetOIDCClientID() string

func (*Exchanger) GetScopesSupported

func (e *Exchanger) GetScopesSupported() ([]string, error)

func (*Exchanger) GetTokenTTL

func (e *Exchanger) GetTokenTTL() time.Duration

func (*Exchanger) IsRefreshTokenEnabled

func (e *Exchanger) IsRefreshTokenEnabled() bool

func (*Exchanger) LoginClient

func (e *Exchanger) LoginClient(ctx context.Context, clientID, clientSecret, scope string) (TokenSet, error)

func (*Exchanger) LoginLocal

func (e *Exchanger) LoginLocal(ctx context.Context, username, password, scope, clientID string, authMethod AuthMethod) (TokenSet, error)

func (*Exchanger) RedeemAuthorizationCode

func (e *Exchanger) RedeemAuthorizationCode(ctx context.Context, code, clientID, redirectURI, codeVerifier string) (TokenSet, error)

RedeemAuthorizationCode validates and exchanges an authorization code for a TokenSet.

func (*Exchanger) RedeemRefreshToken

func (e *Exchanger) RedeemRefreshToken(ctx context.Context, tokenID, clientID string) (TokenSet, error)

RedeemRefreshToken validates and consumes a refresh token (one-time use / rotation), then returns a fresh TokenSet including a rotated refresh token.

type InternalIdentityProvider

type InternalIdentityProvider interface {
	FindInternal(subject string, password string) (jwt.MapClaims, error)
	FindInternalRolesAndEntitlements(subject string) ([]string, []string, error)
}

type JWK

type JWK struct {
	Alg string `json:"alg"`
	Kid string `json:"kid"`
	Kty string `json:"kty"`
	Use string `json:"use"`
	// RSA fields
	E string `json:"e,omitempty"`
	N string `json:"n,omitempty"`
	// ECDSA fields
	Crv string `json:"crv,omitempty"`
	X   string `json:"x,omitempty"`
	Y   string `json:"y,omitempty"`
}

JWK represents a JSON Web Key.

type JWKSet

type JWKSet struct {
	Keys []JWK `json:"keys"`
}

JWKSet represents a JSON Web Key Set.

type Lookup

type Lookup interface {
	FindInternal(subject string, password string) (bool, jwt.MapClaims, error)
	Type() string
}

type OAuth2

type OAuth2 struct {
	AuthConfig    *Config
	AuthExchanger *Exchanger
}

func (*OAuth2) AuthorizeHandler

func (o *OAuth2) AuthorizeHandler(w http.ResponseWriter, r *http.Request)

func (*OAuth2) OAuth2TokenHandler

func (o *OAuth2) OAuth2TokenHandler(w http.ResponseWriter, r *http.Request)

func (*OAuth2) OAuthGet

func (o *OAuth2) OAuthGet(w http.ResponseWriter, r *http.Request)

type OIDCClientConfig added in v0.2.9

type OIDCClientConfig struct {
	ClientID     string
	ClientSecret string
	BlockKey     string
	Name         string
}

func OIDCConfigLoader

func OIDCConfigLoader(secrets kdexv1alpha1.Secrets, devMode bool) (*OIDCClientConfig, error)

type OIDCProviderClaims

type OIDCProviderClaims struct {
	EndSessionURL   string   `json:"end_session_endpoint"`
	ScopesSupported []string `json:"scopes_supported"`
}

type OpenIDConfiguration

type OpenIDConfiguration struct {
	AuthorizationEndpoint            string   `json:"authorization_endpoint,omitempty"`
	ClaimsSupported                  []string `json:"claims_supported,omitempty"`
	GrantTypesSupported              []string `json:"grant_types_supported,omitempty"`
	IDTokenSigningAlgValuesSupported []string `json:"id_token_signing_alg_values_supported"`
	Issuer                           string   `json:"issuer"`
	JwksURI                          string   `json:"jwks_uri"`
	ResponseTypesSupported           []string `json:"response_types_supported"`
	ScopesSupported                  []string `json:"scopes_supported,omitempty"`
	SubjectTypesSupported            []string `json:"subject_types_supported"`
	TokenEndpoint                    string   `json:"token_endpoint,omitempty"`
}

OpenIDConfiguration represents the OIDC discovery document.

type RefreshTokenClaims

type RefreshTokenClaims struct {
	AuthMethod       AuthMethod `json:"auth_method"`
	ClientID         string     `json:"cid"`
	ExpiresAt        int64      `json:"exp"`
	IssuedAt         int64      `json:"iat"`
	OriginalIssuedAt int64      `json:"oiat"`
	Scope            string     `json:"scp"`
	Subject          string     `json:"sub"`
}

RefreshTokenClaims holds the data stored inside a refresh token entry in the cache.

type TokenResponse

type TokenResponse struct {
	AccessToken  string `json:"access_token"`
	ExpiresIn    int    `json:"expires_in"`
	IDToken      string `json:"id_token,omitempty"`
	RefreshToken string `json:"refresh_token,omitempty"`
	Scope        string `json:"scope,omitempty"`
	TokenType    string `json:"token_type"`
}

TokenResponse represents the OAuth2 token response.

type TokenSet

type TokenSet struct {
	AccessToken  string
	IDToken      string
	RefreshToken string
	Scope        string
	Subject      string
}

TokenSet is the result of any successful token minting operation.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL