auth

package
v1.0.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package auth contains the domain types and logic for authentication.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUserNotFound is returned when a proxy user is not found.
	ErrUserNotFound = errors.New("user not found")
	// ErrUserKeyNotFound is returned when a proxy API key is not found.
	ErrUserKeyNotFound = errors.New("user API key not found")
)

Sentinel errors for user store operations.

View Source
var ErrInvalidKey = errors.New("invalid api key")

ErrInvalidKey is returned when an API key is invalid (expired or revoked).

View Source
var ErrUnknownHashType = errors.New("unknown hash type")

ErrUnknownHashType is returned when a stored hash has an unrecognized format.

Functions

func DetectHashType

func DetectHashType(storedHash string) string

DetectHashType identifies the hash algorithm used for a stored hash. Returns "argon2id" for PHC format, "sha256" for prefixed or bare hex, "unknown" for unrecognized formats.

func HashKey

func HashKey(rawKey string) string

HashKey returns the SHA-256 hex hash of the raw key. Deprecated: Use HashKeyArgon2id for new keys. This is kept for backward compatibility.

func HashKeyArgon2id

func HashKeyArgon2id(rawKey string) (string, error)

HashKeyArgon2id returns an Argon2id hash of the raw key in PHC format. The hash includes a random salt and uses OWASP minimum parameters. Format: $argon2id$v=19$m=47104,t=1,p=1$<salt>$<hash>

func VerifyKey

func VerifyKey(rawKey, storedHash string) (bool, error)

VerifyKey verifies a raw key against a stored hash. Supports Argon2id (PHC format), SHA-256 prefixed, and legacy bare SHA-256 hex. Returns (true, nil) if match, (false, nil) if no match, (false, ErrUnknownHashType) for unrecognized hash formats.

Types

type APIKey

type APIKey struct {
	// Key is the hashed key value (SHA-256 hex or Argon2id PHC format).
	Key string
	// IdentityID maps this key to an Identity.
	IdentityID string
	// Name is a human-readable label for this key.
	Name string
	// CreatedAt is when the key was created (UTC).
	CreatedAt time.Time
	// ExpiresAt is when the key expires (nil = never expires).
	ExpiresAt *time.Time
	// Revoked indicates if the key has been revoked.
	Revoked bool
}

APIKey represents an API key for authentication.

func (*APIKey) IsExpired

func (k *APIKey) IsExpired() bool

IsExpired returns true if the API key has expired. A key with nil ExpiresAt never expires.

type APIKeyService

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

APIKeyService validates API keys and returns identities.

func NewAPIKeyService

func NewAPIKeyService(store AuthStore) *APIKeyService

NewAPIKeyService creates a new APIKeyService with the given store.

func (*APIKeyService) Validate

func (s *APIKeyService) Validate(ctx context.Context, rawKey string) (*Identity, error)

Validate checks an API key and returns the associated identity. Returns ErrInvalidKey if key is invalid, expired, or revoked. Returns store-specific errors if key or identity doesn't exist.

Supports both SHA-256 (direct lookup) and Argon2id (iteration) hashes.

type AuthStore

type AuthStore interface {
	// GetAPIKey retrieves an API key by its hash.
	// Returns outbound.ErrKeyNotFound if key doesn't exist.
	GetAPIKey(ctx context.Context, keyHash string) (*APIKey, error)

	// GetIdentity retrieves user identity by ID.
	// Returns outbound.ErrIdentityNotFound if identity doesn't exist.
	GetIdentity(ctx context.Context, id string) (*Identity, error)

	// ListAPIKeys returns all stored API keys for iteration-based verification.
	ListAPIKeys(ctx context.Context) ([]*APIKey, error)
}

AuthStore provides credential lookup for authentication. This interface is defined in the domain to avoid circular imports. Implementations: in-memory (dev), PostgreSQL (prod).

type ExtendedIdentity

type ExtendedIdentity struct {
	Identity
	// Enabled indicates if this user is active.
	Enabled bool
	// CreatedAt is when the user was created (UTC).
	CreatedAt time.Time
	// UpdatedAt is when the user was last modified (UTC).
	UpdatedAt time.Time
}

ExtendedIdentity extends Identity with additional fields for admin operations.

type Identity

type Identity struct {
	// ID is the unique identifier for this identity.
	ID string
	// Name is the display name for this identity.
	Name string
	// Roles are the roles assigned to this identity.
	Roles []Role
}

Identity represents an authenticated user or service.

func (*Identity) HasAnyRole

func (i *Identity) HasAnyRole(roles ...Role) bool

HasAnyRole returns true if the identity has any of the specified roles.

func (*Identity) HasRole

func (i *Identity) HasRole(role Role) bool

HasRole returns true if the identity has the specified role.

type Role

type Role string

Role represents a user role for authorization purposes.

const (
	// RoleAdmin has full access to all operations.
	RoleAdmin Role = "admin"
	// RoleUser has standard access to most operations.
	RoleUser Role = "user"
	// RoleReadOnly has read-only access to operations.
	RoleReadOnly Role = "read-only"
)

func (Role) IsValid

func (r Role) IsValid() bool

IsValid returns true if the role is a known valid role.

type UserStore

type UserStore interface {
	// ListUsers retrieves all proxy users.
	// Returns both enabled and disabled users.
	ListUsers(ctx context.Context) ([]Identity, error)

	// GetUser retrieves a proxy user by ID.
	// Returns ErrUserNotFound if the user doesn't exist.
	GetUser(ctx context.Context, id string) (*Identity, error)

	// CreateUser creates a new proxy user.
	// The ID field is set by the implementation if empty.
	CreateUser(ctx context.Context, user *Identity) error

	// UpdateUser updates an existing proxy user.
	// Updates Name, Roles, and Enabled fields.
	UpdateUser(ctx context.Context, user *Identity) error

	// DeleteUser deletes a proxy user by ID.
	// Also deletes associated API keys (cascade).
	// Returns ErrUserNotFound if the user doesn't exist.
	DeleteUser(ctx context.Context, id string) error

	// CreateUserAPIKey creates a new API key for a proxy user.
	// The keyHash should be the SHA-256 hex hash of the raw API key.
	// Returns ErrUserNotFound if the user doesn't exist.
	CreateUserAPIKey(ctx context.Context, userID, keyHash string, expiresAt *time.Time) error

	// RevokeUserAPIKey revokes a proxy user's API key by its hash.
	// Returns ErrUserKeyNotFound if the key doesn't exist.
	RevokeUserAPIKey(ctx context.Context, keyHash string) error
}

UserStore provides admin CRUD operations for proxy users (identities). This interface is defined in the domain to avoid circular imports. Implementations: PostgreSQL (prod).

Jump to

Keyboard shortcuts

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