auth

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package auth provides authentication and authorization for Creel.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextWithPrincipal

func ContextWithPrincipal(ctx context.Context, p *Principal) context.Context

ContextWithPrincipal returns a new context carrying the given principal.

func GenerateAPIKey

func GenerateAPIKey() (raw string, hash string, prefix string, err error)

GenerateAPIKey creates a new API key with the creel_ak_ prefix. Returns the raw key (to show once) and its SHA-256 hash (to store).

func HashAPIKey

func HashAPIKey(raw string) string

HashAPIKey returns the SHA-256 hex digest of a raw API key.

func IsAPIKey

func IsAPIKey(token string) bool

IsAPIKey returns true if the token looks like a Creel API key.

func PermissionLevel

func PermissionLevel(a Action) int

PermissionLevel returns the numeric level for permission ordering. admin (3) >= write (2) >= read (1).

func StaticKeysFromConfig

func StaticKeysFromConfig(keys []config.APIKeyConfig) map[string]*Principal

StaticKeysFromConfig builds the static key map from configured API keys.

func UnaryInterceptor

func UnaryInterceptor(apiKeyValidator *APIKeyValidator, oidcValidator *OIDCValidator) grpc.UnaryServerInterceptor

UnaryInterceptor returns a gRPC unary server interceptor that authenticates requests. It tries API key validation first, then OIDC if configured.

Types

type APIKeyValidator

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

APIKeyValidator validates API keys against static config and dynamic DB keys.

func NewAPIKeyValidator

func NewAPIKeyValidator(staticKeys map[string]*Principal, dynamicLookup KeyLookup) *APIKeyValidator

NewAPIKeyValidator creates a validator with static keys and an optional dynamic lookup.

func (*APIKeyValidator) Validate

func (v *APIKeyValidator) Validate(ctx context.Context, rawKey string) (*Principal, error)

Validate checks an API key and returns the associated principal.

type Action

type Action string

Action represents an operation that can be performed on a topic.

const (
	ActionRead  Action = "read"
	ActionWrite Action = "write"
	ActionAdmin Action = "admin"
)

type Authorizer

type Authorizer interface {
	// Check returns nil if the principal may perform the action on the topic.
	Check(ctx context.Context, principal *Principal, topicID string, action Action) error

	// AccessibleTopics returns the set of topic IDs the principal can access
	// with at least the given permission level.
	AccessibleTopics(ctx context.Context, principal *Principal, minAction Action) ([]string, error)
}

Authorizer checks whether a principal is allowed to perform actions on topics.

type Grant

type Grant struct {
	TopicID    string
	Principal  string
	Permission Action
}

Grant represents a permission grant on a topic.

type GrantAuthorizer

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

GrantAuthorizer implements Authorizer using topic_grants and topic ownership.

func NewGrantAuthorizer

func NewGrantAuthorizer(store GrantStore) *GrantAuthorizer

NewGrantAuthorizer creates an authorizer backed by the given grant store.

func (*GrantAuthorizer) AccessibleTopics

func (a *GrantAuthorizer) AccessibleTopics(ctx context.Context, principal *Principal, minAction Action) ([]string, error)

AccessibleTopics returns topic IDs the principal can access at the given level. Owned topics are always included (ownership implies admin).

func (*GrantAuthorizer) Check

func (a *GrantAuthorizer) Check(ctx context.Context, principal *Principal, topicID string, action Action) error

Check returns nil if the principal has sufficient permission on the topic.

type GrantStore

type GrantStore interface {
	// GrantsForPrincipal returns all grants matching the principal ID or any of its groups.
	GrantsForPrincipal(ctx context.Context, principals []string) ([]Grant, error)

	// TopicOwner returns the owner of a topic.
	TopicOwner(ctx context.Context, topicID string) (string, error)

	// TopicIDsByOwner returns the IDs of all topics owned by the given principal.
	TopicIDsByOwner(ctx context.Context, ownerID string) ([]string, error)
}

GrantStore provides access to topic grants and ownership.

type KeyLookup

type KeyLookup interface {
	// LookupKeyHash returns the principal for the given key hash, or empty if not found.
	LookupKeyHash(ctx context.Context, hash string) (*Principal, error)
}

KeyLookup resolves an API key hash to a principal.

type OIDCValidator

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

OIDCValidator validates JWT tokens against configured OIDC providers.

func NewOIDCValidator

func NewOIDCValidator(ctx context.Context, providers []config.OIDCProviderConfig, principalClaim, groupsClaim string) (*OIDCValidator, error)

NewOIDCValidator creates a validator for the given OIDC providers. It performs OIDC discovery for each provider (fetches JWKS endpoints).

func (*OIDCValidator) HasProviders

func (v *OIDCValidator) HasProviders() bool

HasProviders returns true if at least one OIDC provider is configured.

func (*OIDCValidator) Validate

func (v *OIDCValidator) Validate(ctx context.Context, rawToken string) (*Principal, error)

Validate checks a raw JWT token against all configured providers. Returns the authenticated principal or nil if no provider accepts the token.

type Principal

type Principal struct {
	// ID is the unique identifier (e.g. "user:alice@example.com" or "system:bootstrap").
	ID string
	// Groups are the group memberships (e.g. ["group:engineering"]).
	Groups []string
	// IsSystem is true for system accounts authenticated via API key.
	IsSystem bool
}

Principal represents an authenticated identity.

func PrincipalFromContext

func PrincipalFromContext(ctx context.Context) *Principal

PrincipalFromContext extracts the principal from the context, or nil if absent.

Jump to

Keyboard shortcuts

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