middleware

package
v0.0.0-...-daf38dd Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// UserIDKey is the context key for the authenticated user ID.
	UserIDKey contextKey = "user_id"
	// TenantIDKey is the context key for the tenant (org) ID.
	TenantIDKey contextKey = "tenant_id"
	// OrgIDKey is the context key for the Clerk organization ID.
	OrgIDKey contextKey = "org_id"
)
View Source
const JWTClaimInternalTenantID = "internal_tenant_id"

JWTClaimInternalTenantID is the Clerk session JWT claim that carries the Postgres tenants.id UUID. Configure it in the Clerk Dashboard session token template (see AGENTS.md).

View Source
const MaxJSONBodySize int64 = 1 << 20 // 1 MB

MaxJSONBodySize is the maximum allowed size for JSON request bodies (1 MB). File uploads use a separate, larger limit enforced in the upload handler.

Variables

This section is empty.

Functions

func BodyLimitMiddleware

func BodyLimitMiddleware(next http.Handler) http.Handler

BodyLimitMiddleware restricts the size of request bodies to prevent denial-of-service attacks via excessively large JSON payloads. File upload endpoints should apply their own limit and are excluded here by checking Content-Type.

func CORSMiddleware

func CORSMiddleware(allowedOrigins []string) func(http.Handler) http.Handler

CORSMiddleware returns an http.Handler middleware that applies CORS headers based on the provided list of allowed origins. If allowedOrigins contains "*", all origins are permitted (useful during development).

func GetOrgID

func GetOrgID(ctx context.Context) string

GetOrgID extracts the Clerk organization ID from the request context.

func GetTenantID

func GetTenantID(ctx context.Context) string

GetTenantID extracts the tenant ID from the request context.

func GetUserID

func GetUserID(ctx context.Context) string

GetUserID extracts the user ID from the request context.

func LoggingMiddleware

func LoggingMiddleware(next http.Handler) http.Handler

LoggingMiddleware logs every HTTP request using slog structured logging. It records the method, path, status code, response size, and duration.

func RecoveryMiddleware

func RecoveryMiddleware(next http.Handler) http.Handler

RecoveryMiddleware recovers from panics in downstream handlers, logs the stack trace, and returns a 500 Internal Server Error to the client. It should be the outermost middleware in the chain.

func WithOrgID

func WithOrgID(ctx context.Context, orgID string) context.Context

WithOrgID returns a new context with the given org ID set.

func WithTenantID

func WithTenantID(ctx context.Context, tenantID string) context.Context

WithTenantID returns a new context with the given tenant ID set.

func WithUserID

func WithUserID(ctx context.Context, userID string) context.Context

WithUserID returns a new context with the given user ID set.

Types

type AuthMiddleware

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

AuthMiddleware validates JWT tokens from the Authorization header.

func NewAuthMiddleware

func NewAuthMiddleware(clerkSecretKey string, allowDevBypass bool) *AuthMiddleware

NewAuthMiddleware creates a new AuthMiddleware. allowDevBypass enables X-Dev-User-ID / X-Dev-Tenant-ID (and token=dev) only in local development; it must be false for staging/production. When clerkSecretKey is empty and allowDevBypass is true, JWT validation is skipped only for requests that use the dev header bypass.

func (*AuthMiddleware) Authenticate

func (am *AuthMiddleware) Authenticate(next http.Handler) http.Handler

Authenticate returns an http.Handler middleware that validates JWT bearer tokens. In development mode, the middleware also accepts X-Dev-User-ID and X-Dev-Tenant-ID headers as a convenience bypass.

type TenantMiddleware

type TenantMiddleware struct{}

TenantMiddleware ensures that every authenticated request has a valid tenant context. It must be placed after AuthMiddleware in the middleware chain.

func NewTenantMiddleware

func NewTenantMiddleware() *TenantMiddleware

NewTenantMiddleware creates a new TenantMiddleware.

func (*TenantMiddleware) InjectTenant

func (tm *TenantMiddleware) InjectTenant(next http.Handler) http.Handler

InjectTenant returns an http.Handler middleware that reads the tenant ID previously set by the auth middleware, validates that it is present, and allows the request to proceed. If the tenant ID is missing the request is rejected with 401 Unauthorized.

Jump to

Keyboard shortcuts

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