middleware

package
v2.5.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2026 License: BSD-3-Clause Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const MetaRequiredPermission = authz.MetaRequiredPermission

MetaRequiredPermission is re-exported from the authz package for callers that reference it via this middleware package. The authoritative definition (and the matcher that consumes it) lives in authz.

Variables

This section is empty.

Functions

func GetCurrentSessionIDFromContext

func GetCurrentSessionIDFromContext(ctx context.Context) (string, bool)

GetCurrentSessionIDFromContext retrieves the current session ID from the context.

func GetCurrentUserFromContext

func GetCurrentUserFromContext(ctx context.Context) (*models.User, bool)

GetCurrentUserFromContext retrieves the current user from the context.

func GetRemoteAddrFromContext

func GetRemoteAddrFromContext(ctx context.Context) string

GetRemoteAddrFromContext retrieves the request remote address from context.

func GetUserIDFromContext

func GetUserIDFromContext(ctx context.Context) (string, bool)

GetUserIDFromContext retrieves the user ID from the context.

func NewActivityBatchID added in v2.5.0

func NewActivityBatchID() func(ctx huma.Context, next func(huma.Context))

NewActivityBatchID lifts the client-supplied activity batch ID header into the request context so activities spawned by one logical bulk action can be grouped without threading the ID through every handler. Invalid values are ignored by utils.WithActivityBatchID.

func NewAuthBridge

func NewAuthBridge(api huma.API, authService *services.AuthService, apiKeyService *services.ApiKeyService, permResolver PermissionResolver, envTokenResolver environmentAccessTokenResolver, cfg *config.Config) func(ctx huma.Context, next func(huma.Context))

NewAuthBridge creates a Huma middleware that validates credentials and enforces security requirements defined on operations. It also resolves the caller's effective PermissionSet via permResolver and stashes it on the request context for downstream RequirePermission checks.

func PermissionsFromContext

func PermissionsFromContext(ctx context.Context) (*authz.PermissionSet, bool)

PermissionsFromContext retrieves the caller's resolved PermissionSet. Returns nil, false on unauthenticated paths.

func RegisterWithPermission added in v2.1.0

func RegisterWithPermission[I, O any](api huma.API, op huma.Operation, perm string, handler func(context.Context, *I) (*O, error))

RegisterWithPermission registers a Huma operation that requires perm. It attaches the RequirePermission middleware AND records perm in the operation metadata (authz.MetaRequiredPermission) so the remote environment proxy can enforce the same permission for environment-scoped operations before forwarding a request to an agent.

Use this instead of huma.Register with an inline RequirePermission middleware for every operation served under /environments/{id}/..., so the required permission stays the single source of truth for both local enforcement and remote-proxy enforcement. It is safe to use for org-level operations too; the recorded metadata is simply unused by the proxy for non-environment paths.

func RequireAnyEnvironmentPermission added in v2.4.0

func RequireAnyEnvironmentPermission(api huma.API, perm string) huma.Middlewares

RequireAnyEnvironmentPermission protects aggregate operations that span environments but do not carry an environment ID in their path. The caller must hold perm globally or for at least one environment; handlers remain responsible for filtering aggregate output to the exact allowed scopes.

func RequireGlobalAdmin

func RequireGlobalAdmin(api huma.API) huma.Middlewares

RequireGlobalAdmin returns a per-operation Huma middleware that rejects any caller who is not a global admin (or sudo). Used for operations that are intentionally not exposed as delegated permissions — role creation/edits, user role assignment, and OIDC mapping management. Keeping these admin-only avoids the meta-escalation surface where a holder of `roles:assign` could promote themselves via a custom role.

func RequirePermission

func RequirePermission(api huma.API, perm string) huma.Middlewares

RequirePermission returns a per-operation Huma middleware that rejects callers lacking `perm`. For env-scoped permissions, the env ID is extracted from the request path (/environments/{id}/...). For org-level permissions, the env ID segment, if any, is ignored.

Attach via Operation.Middlewares:

huma.Register(api, huma.Operation{..., Middlewares: middleware.RequirePermission(api, authz.PermContainersStart)}, h.Handler)

Types

type ContextKey

type ContextKey string

ContextKey is a type for context keys used by Huma handlers.

const (
	// ContextKeyUserID is the context key for the authenticated user's ID.
	ContextKeyUserID ContextKey = "userID"
	// ContextKeyCurrentUser is the context key for the authenticated user model.
	ContextKeyCurrentUser ContextKey = "currentUser"
	// ContextKeyCurrentSessionID is the context key for the authenticated session ID.
	ContextKeyCurrentSessionID ContextKey = "currentSessionID"
	// ContextKeyUserPermissions is the context key for the caller's resolved
	// PermissionSet, attached by the auth bridge.
	ContextKeyUserPermissions ContextKey = "userPermissions"
	// ContextKeyRemoteAddr is the context key for the request remote address.
	ContextKeyRemoteAddr ContextKey = "remoteAddr"
)

type PermissionResolver

type PermissionResolver interface {
	ResolvePermissions(ctx context.Context, user *models.User) (*authz.PermissionSet, error)
	ResolveApiKeyPermissions(ctx context.Context, apiKeyID string) (*authz.PermissionSet, error)
}

PermissionResolver resolves a caller's effective permission set. Implemented by services.RoleService; kept as an interface so tests can stub it.

Jump to

Keyboard shortcuts

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