auth

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 21, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package auth provides authentication support for the platform.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetToken

func GetToken(ctx context.Context) string

GetToken retrieves a token from the context.

func ValidateClaims

func ValidateClaims(claims map[string]any, required []string) error

ValidateClaims validates required claims are present.

func WithToken

func WithToken(ctx context.Context, token string) context.Context

WithToken adds a token to the context.

func WithUserContext

func WithUserContext(ctx context.Context, uc *UserContext) context.Context

WithUserContext adds user context to the context.

Types

type APIKey

type APIKey struct {
	Key   string   // The API key value
	Name  string   // Display name for the key
	Roles []string // Roles assigned to this key
}

APIKey represents an API key entry.

type APIKeyAuthenticator

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

APIKeyAuthenticator authenticates using API keys.

func NewAPIKeyAuthenticator

func NewAPIKeyAuthenticator(cfg APIKeyConfig) *APIKeyAuthenticator

NewAPIKeyAuthenticator creates a new API key authenticator.

func (*APIKeyAuthenticator) AddKey

func (a *APIKeyAuthenticator) AddKey(key APIKey)

AddKey adds an API key at runtime.

func (*APIKeyAuthenticator) Authenticate

func (a *APIKeyAuthenticator) Authenticate(ctx context.Context) (*middleware.UserInfo, error)

Authenticate validates the API key and returns user info.

func (*APIKeyAuthenticator) RemoveKey

func (a *APIKeyAuthenticator) RemoveKey(keyValue string)

RemoveKey removes an API key.

type APIKeyConfig

type APIKeyConfig struct {
	Keys []APIKey
}

APIKeyConfig holds API key configuration.

type APIKeyExtractor

type APIKeyExtractor struct {
	HeaderName string // e.g., "X-API-Key"
	QueryParam string // e.g., "api_key"
}

APIKeyExtractor extracts API keys from headers or query params.

func (*APIKeyExtractor) Extract

func (e *APIKeyExtractor) Extract(ctx context.Context) (string, error)

Extract extracts an API key from the context.

type BearerTokenExtractor

type BearerTokenExtractor struct {
	HeaderName string // Default: "Authorization"
}

BearerTokenExtractor extracts Bearer tokens from Authorization header.

func (*BearerTokenExtractor) Extract

func (e *BearerTokenExtractor) Extract(ctx context.Context) (string, error)

Extract extracts a bearer token from the context.

type ChainedAuthConfig

type ChainedAuthConfig struct {
	AllowAnonymous bool
}

ChainedAuthConfig configures the chained authenticator.

type ChainedAuthenticator

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

ChainedAuthenticator tries multiple authenticators in order.

func NewChainedAuthenticator

func NewChainedAuthenticator(cfg ChainedAuthConfig, authenticators ...middleware.Authenticator) *ChainedAuthenticator

NewChainedAuthenticator creates a new chained authenticator.

func (*ChainedAuthenticator) Authenticate

func (c *ChainedAuthenticator) Authenticate(ctx context.Context) (*middleware.UserInfo, error)

Authenticate tries each authenticator in order.

type ClaimsExtractor

type ClaimsExtractor struct {
	// RoleClaimPath is the dot-separated path to roles in claims.
	// e.g., "realm_access.roles" or "roles"
	RoleClaimPath string

	// RolePrefix filters roles to those starting with this prefix.
	RolePrefix string

	// GroupClaimPath is the dot-separated path to groups in claims.
	GroupClaimPath string

	// EmailClaimPath is the path to the email claim.
	EmailClaimPath string

	// NameClaimPath is the path to the name claim.
	NameClaimPath string

	// SubjectClaimPath is the path to the subject claim.
	SubjectClaimPath string
}

ClaimsExtractor extracts values from JWT claims.

func DefaultClaimsExtractor

func DefaultClaimsExtractor() *ClaimsExtractor

DefaultClaimsExtractor returns an extractor with common defaults.

func (*ClaimsExtractor) Extract

func (e *ClaimsExtractor) Extract(claims map[string]any) (*UserContext, error)

Extract extracts user context from claims.

type OIDCAuthenticator

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

OIDCAuthenticator authenticates using OIDC tokens.

func NewOIDCAuthenticator

func NewOIDCAuthenticator(cfg OIDCConfig) (*OIDCAuthenticator, error)

NewOIDCAuthenticator creates a new OIDC authenticator.

func (*OIDCAuthenticator) Authenticate

func (a *OIDCAuthenticator) Authenticate(ctx context.Context) (*middleware.UserInfo, error)

Authenticate validates the token and returns user info.

func (*OIDCAuthenticator) FetchJWKS

func (a *OIDCAuthenticator) FetchJWKS(ctx context.Context) error

FetchJWKS fetches the JWKS from the issuer.

type OIDCConfig

type OIDCConfig struct {
	// Issuer is the OIDC issuer URL.
	Issuer string

	// ClientID is the OAuth client ID.
	ClientID string

	// Audience is the expected audience claim.
	Audience string

	// RoleClaimPath is the path to roles in claims.
	RoleClaimPath string

	// RolePrefix filters roles to those with this prefix.
	RolePrefix string

	// SkipIssuerVerification skips issuer verification (for testing).
	SkipIssuerVerification bool
}

OIDCConfig configures OIDC authentication.

type TokenExtractor

type TokenExtractor interface {
	Extract(ctx context.Context) (string, error)
}

TokenExtractor extracts tokens from various sources.

type UserContext

type UserContext struct {
	UserID    string         `json:"user_id"`
	Email     string         `json:"email,omitempty"`
	Name      string         `json:"name,omitempty"`
	Roles     []string       `json:"roles,omitempty"`
	Groups    []string       `json:"groups,omitempty"`
	Claims    map[string]any `json:"claims,omitempty"`
	AuthType  string         `json:"auth_type"` // "oidc", "apikey"
	TokenType string         `json:"token_type,omitempty"`
}

UserContext holds authenticated user information.

func GetUserContext

func GetUserContext(ctx context.Context) *UserContext

GetUserContext retrieves user context from the context.

func (*UserContext) HasAnyRole

func (uc *UserContext) HasAnyRole(roles ...string) bool

HasAnyRole checks if the user has any of the specified roles.

func (*UserContext) HasRole

func (uc *UserContext) HasRole(role string) bool

HasRole checks if the user has a specific role.

func (*UserContext) InGroup

func (uc *UserContext) InGroup(group string) bool

InGroup checks if the user is in a specific group.

Jump to

Keyboard shortcuts

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