auth

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2026 License: AGPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const SubjectTypeUser = "user"

SubjectTypeUser is the only subject type in the unified authorization model (humans and tenant service principals are both "user"). Matches authz.TypeUser and feeds the Pass 2 Check evaluator's subject type.

Variables

This section is empty.

Functions

func APIKeyMiddleware

func APIKeyMiddleware(validator KeyValidator) func(http.Handler) http.Handler

APIKeyMiddleware validates the Bearer token and injects the tenant ID into the request context. On rejection it writes an RFC 6750 §3 response (401, JSON error/error_description); the WWW-Authenticate challenge is added by an outer middleware.

func BearerToken

func BearerToken(r *http.Request) (string, error)

BearerToken extracts the Bearer token from an Authorization header. Returns an error if the header is missing or malformed.

func EmailFromContext

func EmailFromContext(ctx context.Context) string

EmailFromContext extracts the caller's email from the context.

func GenerateAPIKey

func GenerateAPIKey() (plaintext, hash string, err error)

GenerateAPIKey creates a new random API key. Returns the plaintext key (to show once) and its SHA-256 hash (to store).

func HashAPIKey

func HashAPIKey(key string) string

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

func IsLocalAdmin

func IsLocalAdmin(ctx context.Context) bool

IsLocalAdmin reports whether the context was marked as a local admin.

func KeyPrefix

func KeyPrefix(plaintext string) string

KeyPrefix returns the display prefix for an API key (first 8 chars after mmcp_).

func TenantIDFromContext

func TenantIDFromContext(ctx context.Context) uuid.UUID

TenantIDFromContext extracts the tenant ID from the context. Returns uuid.Nil if not set.

func WithEmail

func WithEmail(ctx context.Context, email string) context.Context

WithEmail returns a new context with the caller's email.

func WithLocalAdmin

func WithLocalAdmin(ctx context.Context) context.Context

WithLocalAdmin marks a context as an offline, inherently-privileged local admin. Exists solely for the memory-admin CLI, which runs directly against the DB (holding DATABASE_URL is already full control) and has no Subject to resolve; the admin gate honors this so the CLI reuses the same tuple-seeding lifecycle methods as the network paths.

SECURITY: only the in-process CLI sets this. Network entry points (MCP, HTTP) build context from a real authenticated Subject and never call it, so no request can escalate by setting it.

func WithSubject

func WithSubject(ctx context.Context, s Subject) context.Context

WithSubject returns a new context carrying the resolved authorization subject.

func WithTenantID

func WithTenantID(ctx context.Context, id uuid.UUID) context.Context

WithTenantID returns a new context with the given tenant ID.

Types

type APIKeyValidator

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

APIKeyValidator looks up API keys and resolves them to tenant IDs.

func NewAPIKeyValidator

func NewAPIKeyValidator(db *gorm.DB) *APIKeyValidator

func (*APIKeyValidator) ValidateKey

func (v *APIKeyValidator) ValidateKey(ctx context.Context, key string) (KeyInfo, error)

ValidateKey checks the key hash against the database. Returns the tenant info if valid, or an error if not found / revoked.

type KeyInfo

type KeyInfo struct {
	TenantID uuid.UUID
	Email    string
	// SubjectID is the key's unified subject id: its explicit subject_id when
	// set, else the tenant service principal "svc:<tenant_id>". Always user-type.
	SubjectID string
}

KeyInfo holds the resolved identity from an API key lookup.

type KeyValidator

type KeyValidator interface {
	ValidateKey(ctx context.Context, key string) (KeyInfo, error)
}

KeyValidator resolves an API key to its tenant identity. Defined as an interface so middleware can be tested without a real database.

type Subject

type Subject struct {
	Type string
	ID   string
}

Subject is the unified authorization principal for a request. JWT humans (id == tenant_users.id) and API-key callers (id == the key's subject_id, else "svc:<tenant_id>") both resolve to one; the Pass 2 Check evaluator runs against it. Type is always SubjectTypeUser today.

func SubjectFromContext

func SubjectFromContext(ctx context.Context) (Subject, bool)

SubjectFromContext extracts the authorization subject. The bool is false when none was resolved (e.g. a JWT caller with no tenant_users row); Pass 2 fails closed on that.

Jump to

Keyboard shortcuts

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