auth

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 27, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckAPIKey

func CheckAPIKey(hash, key string) bool

func CheckPassword

func CheckPassword(hash, password string) bool

func ContextWithUser

func ContextWithUser(ctx context.Context, user *User) context.Context

func GenerateAPIKey

func GenerateAPIKey() (fullKey, prefix string, err error)

func HashAPIKey

func HashAPIKey(key string) (string, error)

func HashPassword

func HashPassword(password string) (string, error)

func Middleware

func Middleware(sessionManager *scs.SessionManager, userStore *UserStore, keyStore *KeyStore, legacyAPIKey string) func(http.Handler) http.Handler

Middleware returns a chi-compatible middleware that enforces authentication on all /api/ routes except explicitly exempt paths. It checks three auth methods in order:

  1. Session cookie (via scs session manager)
  2. API key (X-API-Key header with "sk-" prefix, SHA-256 hashed)
  3. Legacy API key (constant-time comparison against a static key)

On success, the authenticated User is attached to the request context via ContextWithUser.

func RegisterRoutes

func RegisterRoutes(api huma.API, sessionManager *scs.SessionManager, userStore *UserStore, keyStore *KeyStore, disableSignup bool)

RegisterRoutes wires up all authentication-related HTTP endpoints onto the provided Huma API: signup, login, logout, me, and API key management.

Types

type APIKeyInfo

type APIKeyInfo struct {
	ID         string     `json:"id"`
	Name       string     `json:"name"`
	Prefix     string     `json:"prefix"`
	LastUsedAt *time.Time `json:"last_used_at"`
	CreatedAt  time.Time  `json:"created_at"`
}

type APIKeyRow

type APIKeyRow struct {
	ID         string
	UserID     string
	Name       string
	KeyPrefix  string
	KeyHash    string
	LastUsedAt *time.Time
	CreatedAt  time.Time
}

type KeyStore

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

func NewKeyStore

func NewKeyStore(pool *pgxpool.Pool) *KeyStore

func (*KeyStore) Create

func (s *KeyStore) Create(ctx context.Context, userID, name, keyPrefix, keyHash string) (*APIKeyRow, error)

func (*KeyStore) Delete

func (s *KeyStore) Delete(ctx context.Context, id, userID string) error

func (*KeyStore) GetByPrefix

func (s *KeyStore) GetByPrefix(ctx context.Context, prefix string) (*APIKeyRow, error)

func (*KeyStore) ListByUser

func (s *KeyStore) ListByUser(ctx context.Context, userID string) ([]APIKeyInfo, error)

func (*KeyStore) UpdateLastUsed

func (s *KeyStore) UpdateLastUsed(ctx context.Context, id string)

type User

type User struct {
	ID    string `json:"id"`
	Email string `json:"email"`
	Name  string `json:"name"`
	Role  string `json:"role"`
}

func UserFromContext

func UserFromContext(ctx context.Context) *User

type UserRow

type UserRow struct {
	ID           string
	Email        string
	Name         string
	PasswordHash string
	Role         string
	CreatedAt    time.Time
}

type UserStore

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

func NewUserStore

func NewUserStore(pool *pgxpool.Pool) *UserStore

func (*UserStore) Count

func (s *UserStore) Count(ctx context.Context) (int, error)

func (*UserStore) Create

func (s *UserStore) Create(ctx context.Context, email, name, passwordHash string) (*UserRow, error)

func (*UserStore) CreateWithRole

func (s *UserStore) CreateWithRole(ctx context.Context, email, name, passwordHash, role string) (*UserRow, error)

func (*UserStore) GetByEmail

func (s *UserStore) GetByEmail(ctx context.Context, email string) (*UserRow, error)

func (*UserStore) GetByID

func (s *UserStore) GetByID(ctx context.Context, id string) (*UserRow, error)

Jump to

Keyboard shortcuts

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