Documentation
¶
Overview ¶
Package auth provides authentication and authorization for Creel.
Index ¶
- func ContextWithPrincipal(ctx context.Context, p *Principal) context.Context
- func GenerateAPIKey() (raw string, hash string, prefix string, err error)
- func HashAPIKey(raw string) string
- func IsAPIKey(token string) bool
- func PermissionLevel(a Action) int
- func StaticKeysFromConfig(keys []config.APIKeyConfig) map[string]*Principal
- func UnaryInterceptor(apiKeyValidator *APIKeyValidator, oidcValidator *OIDCValidator) grpc.UnaryServerInterceptor
- type APIKeyValidator
- type Action
- type Authorizer
- type Grant
- type GrantAuthorizer
- type GrantStore
- type KeyLookup
- type OIDCValidator
- type Principal
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ContextWithPrincipal ¶
ContextWithPrincipal returns a new context carrying the given principal.
func GenerateAPIKey ¶
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 ¶
HashAPIKey returns the SHA-256 hex digest of a raw API key.
func PermissionLevel ¶
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.
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 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.
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)
}
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.
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 ¶
PrincipalFromContext extracts the principal from the context, or nil if absent.