Documentation
¶
Index ¶
- Constants
- func APIKeyMiddleware(validator KeyValidator) func(http.Handler) http.Handler
- func BearerToken(r *http.Request) (string, error)
- func EmailFromContext(ctx context.Context) string
- func GenerateAPIKey() (plaintext, hash string, err error)
- func HashAPIKey(key string) string
- func IsLocalAdmin(ctx context.Context) bool
- func KeyPrefix(plaintext string) string
- func TenantIDFromContext(ctx context.Context) uuid.UUID
- func WithEmail(ctx context.Context, email string) context.Context
- func WithLocalAdmin(ctx context.Context) context.Context
- func WithSubject(ctx context.Context, s Subject) context.Context
- func WithTenantID(ctx context.Context, id uuid.UUID) context.Context
- type APIKeyValidator
- type KeyInfo
- type KeyValidator
- type Subject
Constants ¶
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 ¶
BearerToken extracts the Bearer token from an Authorization header. Returns an error if the header is missing or malformed.
func EmailFromContext ¶
EmailFromContext extracts the caller's email from the context.
func GenerateAPIKey ¶
GenerateAPIKey creates a new random API key. Returns the plaintext key (to show once) and its SHA-256 hash (to store).
func HashAPIKey ¶
HashAPIKey returns the SHA-256 hex digest of a key.
func IsLocalAdmin ¶
IsLocalAdmin reports whether the context was marked as a local admin.
func TenantIDFromContext ¶
TenantIDFromContext extracts the tenant ID from the context. Returns uuid.Nil if not set.
func WithLocalAdmin ¶
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 ¶
WithSubject returns a new context carrying the resolved authorization subject.
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 ¶
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 ¶
KeyValidator resolves an API key to its tenant identity. Defined as an interface so middleware can be tested without a real database.