auth

package
v2.11.2 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidCredentials                = errors.New("invalid username or password")
	ErrInvalidToken                      = errors.New("invalid or expired token")
	ErrTokenExpired                      = errors.New("token has expired")
	ErrMissingSecret                     = errors.New("token secret is not configured")
	ErrPasswordMismatch                  = errors.New("current password is incorrect")
	ErrWeakPassword                      = errors.New("password does not meet requirements")
	ErrExternalAuthPasswordManagement    = errors.New("passwords for externally authenticated users are managed by their authentication provider")
	ErrCannotDeleteSelf                  = errors.New("cannot delete your own account")
	ErrInvalidAPIKey                     = errors.New("invalid API key")
	ErrAPIKeyNotConfigured               = errors.New("API key management is not configured")
	ErrInvalidCreatorID                  = errors.New("creator ID is required")
	ErrInvalidWebhookToken               = errors.New("invalid webhook token")
	ErrWebhookNotConfigured              = errors.New("webhook management is not configured")
	ErrWebhookDisabled                   = errors.New("webhook is disabled")
	ErrInvalidWebhookAuthMode            = errors.New("invalid webhook auth mode")
	ErrInvalidWebhookHMACEnforcementMode = errors.New("invalid webhook HMAC enforcement mode")
	ErrWebhookHMACNotSupported           = errors.New("webhook HMAC is not supported by this store")
	ErrMissingWebhookHMACSignature       = errors.New("missing webhook HMAC signature")
	ErrInvalidWebhookHMACSignature       = errors.New("invalid webhook HMAC signature")
	ErrWebhookHMACNotConfigured          = errors.New("webhook HMAC is not configured")
	ErrUserDisabled                      = auth.ErrUserDisabled
)

Service errors.

Functions

This section is empty.

Types

type Claims

type Claims struct {
	jwt.RegisteredClaims
	UserID   string    `json:"uid"`
	Username string    `json:"username"`
	Role     auth.Role `json:"role"`
	// PasswordChangedAt is the Unix nanosecond timestamp of the user's last password
	// change at token issuance. Zero means the user had never changed their password.
	// Tokens issued before a subsequent password change are rejected.
	PasswordChangedAt int64 `json:"pwd_changed_at_ns,omitempty"`
}

Claims represents the JWT claims.

type Config

type Config struct {
	// TokenSecret is the opaque JWT signing key.
	TokenSecret auth.TokenSecret
	// TokenTTL is the token time-to-live.
	TokenTTL time.Duration
	// BcryptCost is the cost factor for bcrypt hashing.
	BcryptCost int
}

Config holds the configuration for the auth service.

type CreateAPIKeyInput

type CreateAPIKeyInput struct {
	Name               string
	Description        string
	Role               auth.Role
	WorkspaceAccess    *auth.WorkspaceAccess
	AllowedSurfaces    []auth.APIKeySurface
	AttributionClass   auth.APIKeyAttributionClass
	OwnerUserID        string
	ServiceAccountName string
}

CreateAPIKeyInput contains the input for creating an API key.

type CreateAPIKeyResult

type CreateAPIKeyResult struct {
	APIKey  *auth.APIKey
	FullKey string // Only returned once at creation
}

CreateAPIKeyResult contains the result of creating an API key.

type CreateUserInput

type CreateUserInput struct {
	Username        string
	Password        string
	Role            auth.Role
	WorkspaceAccess *auth.WorkspaceAccess
}

CreateUserInput contains the input for creating a user.

type CreateWebhookResult

type CreateWebhookResult struct {
	Webhook   *auth.Webhook
	FullToken string // Only returned once at creation
}

CreateWebhookResult contains the result of creating a webhook.

type Option

type Option func(*Service)

Option is a functional option for configuring the Service.

func WithAPIKeyStore

func WithAPIKeyStore(store auth.APIKeyStore) Option

WithAPIKeyStore sets the API key store for the service.

func WithWebhookStore

func WithWebhookStore(store auth.WebhookStore) Option

WithWebhookStore sets the webhook store for the service.

type Service

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

Service provides authentication and user management functionality.

func New

func New(store auth.UserStore, config Config, opts ...Option) *Service

New creates a new auth service using the provided user store and configuration. If TokenTTL or BcryptCost are not set (<= 0) they are replaced with package defaults.

func (*Service) Authenticate

func (s *Service) Authenticate(ctx context.Context, username, password string) (*auth.User, error)

Authenticate verifies credentials and returns the user if valid.

func (*Service) AuthorizeWebhookRequest

func (s *Service) AuthorizeWebhookRequest(
	ctx context.Context,
	dagName, token, signature string,
	body []byte,
) (*auth.Webhook, error)

AuthorizeWebhookRequest validates the request according to the webhook's auth mode.

func (*Service) ChangePassword

func (s *Service) ChangePassword(ctx context.Context, userID, oldPassword, newPassword string) error

ChangePassword changes a user's password after verifying the old password.

func (*Service) ConfigureWebhookHMAC

func (s *Service) ConfigureWebhookHMAC(
	ctx context.Context,
	dagName string,
	authMode auth.WebhookAuthMode,
	enforcementMode auth.WebhookHMACEnforcementMode,
) (*auth.Webhook, error)

ConfigureWebhookHMAC updates HMAC auth mode or enforcement without rotating the secret.

func (*Service) CountUsers

func (s *Service) CountUsers(ctx context.Context) (int64, error)

CountUsers returns the total number of users in the store.

func (*Service) CreateAPIKey

func (s *Service) CreateAPIKey(ctx context.Context, input CreateAPIKeyInput, creatorID string) (*CreateAPIKeyResult, error)

CreateAPIKey creates a new API key.

func (*Service) CreateUser

func (s *Service) CreateUser(ctx context.Context, input CreateUserInput) (*auth.User, error)

CreateUser creates a new user.

func (*Service) CreateWebhook

func (s *Service) CreateWebhook(ctx context.Context, dagName, creatorID string) (*CreateWebhookResult, error)

CreateWebhook creates a new webhook for a DAG.

func (*Service) DeleteAPIKey

func (s *Service) DeleteAPIKey(ctx context.Context, id string) error

DeleteAPIKey deletes an API key by ID.

func (*Service) DeleteUser

func (s *Service) DeleteUser(ctx context.Context, id string, currentUserID string) error

DeleteUser deletes a user by ID. The currentUserID prevents users from deleting themselves.

func (*Service) DeleteWebhook

func (s *Service) DeleteWebhook(ctx context.Context, dagName string) error

DeleteWebhook deletes a webhook by DAG name.

func (*Service) DisableWebhookHMAC

func (s *Service) DisableWebhookHMAC(ctx context.Context, dagName string) (*auth.Webhook, error)

DisableWebhookHMAC removes HMAC auth from the webhook and returns it to token-only mode.

func (*Service) EnableWebhookHMAC

func (s *Service) EnableWebhookHMAC(
	ctx context.Context,
	dagName string,
	authMode auth.WebhookAuthMode,
	enforcementMode auth.WebhookHMACEnforcementMode,
) (*WebhookHMACSecretResult, error)

EnableWebhookHMAC configures HMAC auth for an existing webhook and returns the generated secret exactly once.

func (*Service) GenerateToken

func (s *Service) GenerateToken(user *auth.User) (*TokenResult, error)

GenerateToken creates a JWT token for the given user. Returns the token string and its expiry time.

func (*Service) GetAPIKey

func (s *Service) GetAPIKey(ctx context.Context, id string) (*auth.APIKey, error)

GetAPIKey retrieves an API key by ID.

func (*Service) GetUser

func (s *Service) GetUser(ctx context.Context, id string) (*auth.User, error)

GetUser retrieves a user by ID.

func (*Service) GetUserFromToken

func (s *Service) GetUserFromToken(ctx context.Context, tokenString string) (*auth.User, error)

GetUserFromToken validates a token and returns the associated user.

func (*Service) GetWebhookByDAGName

func (s *Service) GetWebhookByDAGName(ctx context.Context, dagName string) (*auth.Webhook, error)

GetWebhookByDAGName retrieves the webhook for a specific DAG.

func (*Service) HasAPIKeyStore

func (s *Service) HasAPIKeyStore() bool

HasAPIKeyStore returns true if API key management is configured.

func (*Service) HasWebhookStore

func (s *Service) HasWebhookStore() bool

HasWebhookStore returns true if webhook management is configured.

func (*Service) ListAPIKeys

func (s *Service) ListAPIKeys(ctx context.Context) ([]*auth.APIKey, error)

ListAPIKeys returns all API keys.

func (*Service) ListUsers

func (s *Service) ListUsers(ctx context.Context) ([]*auth.User, error)

ListUsers returns all users.

func (*Service) ListWebhooks

func (s *Service) ListWebhooks(ctx context.Context) ([]*auth.Webhook, error)

ListWebhooks returns all webhooks.

func (*Service) RegenerateWebhookHMACSecret

func (s *Service) RegenerateWebhookHMACSecret(ctx context.Context, dagName string) (*WebhookHMACSecretResult, error)

RegenerateWebhookHMACSecret rotates the HMAC secret immediately and returns the new secret exactly once.

func (*Service) RegenerateWebhookToken

func (s *Service) RegenerateWebhookToken(ctx context.Context, dagName string) (*CreateWebhookResult, error)

RegenerateWebhookToken generates a new token for an existing webhook. The old token becomes invalid immediately.

func (*Service) ResetPassword

func (s *Service) ResetPassword(ctx context.Context, userID, newPassword string) error

ResetPassword allows an admin to reset a user's password without knowing the old password.

func (*Service) ToggleWebhook

func (s *Service) ToggleWebhook(ctx context.Context, dagName string, enabled bool) (*auth.Webhook, error)

ToggleWebhook enables or disables a webhook without changing the token.

func (*Service) UpdateAPIKey

func (s *Service) UpdateAPIKey(ctx context.Context, id string, input UpdateAPIKeyInput) (*auth.APIKey, error)

UpdateAPIKey updates an existing API key.

func (*Service) UpdateUser

func (s *Service) UpdateUser(ctx context.Context, id string, input UpdateUserInput) (*auth.User, error)

UpdateUser updates an existing user.

func (*Service) ValidateAPIKey

func (s *Service) ValidateAPIKey(ctx context.Context, keySecret string) (*auth.APIKey, error)

ValidateAPIKey validates an API key and returns the associated APIKey if valid.

func (*Service) ValidateToken

func (s *Service) ValidateToken(tokenString string) (*Claims, error)

ValidateToken validates a JWT token and returns the claims.

func (*Service) ValidateWebhookToken

func (s *Service) ValidateWebhookToken(ctx context.Context, dagName, token string) (*auth.Webhook, error)

ValidateWebhookToken validates a webhook token for a specific DAG. Returns the webhook if valid and enabled.

type TokenResult

type TokenResult struct {
	Token     string
	ExpiresAt time.Time
}

TokenResult contains the generated token and its expiry time.

type UpdateAPIKeyInput

type UpdateAPIKeyInput struct {
	Name               *string
	Description        *string
	Role               *auth.Role
	WorkspaceAccess    *auth.WorkspaceAccess
	AllowedSurfaces    *[]auth.APIKeySurface
	AttributionClass   *auth.APIKeyAttributionClass
	OwnerUserID        *string
	ServiceAccountName *string
}

UpdateAPIKeyInput contains the input for updating an API key.

type UpdateUserInput

type UpdateUserInput struct {
	Username        *string
	Role            *auth.Role
	WorkspaceAccess *auth.WorkspaceAccess
	Password        *string
	IsDisabled      *bool
}

UpdateUserInput contains the input for updating a user. Note: Password field is supported by the service for direct usage, but the API handler intentionally omits it - password changes should go through ChangePassword (user self-service) or ResetPassword (admin).

type WebhookHMACSecretResult

type WebhookHMACSecretResult struct {
	Webhook    *auth.Webhook
	FullSecret string // Only returned once at creation or rotation
}

WebhookHMACSecretResult contains the result of enabling or rotating HMAC.

Jump to

Keyboard shortcuts

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