Documentation
¶
Index ¶
- Constants
- func BodyLimitMiddleware(next http.Handler) http.Handler
- func CORSMiddleware(allowedOrigins []string) func(http.Handler) http.Handler
- func GetOrgID(ctx context.Context) string
- func GetTenantID(ctx context.Context) string
- func GetUserID(ctx context.Context) string
- func LoggingMiddleware(next http.Handler) http.Handler
- func RecoveryMiddleware(next http.Handler) http.Handler
- func WithOrgID(ctx context.Context, orgID string) context.Context
- func WithTenantID(ctx context.Context, tenantID string) context.Context
- func WithUserID(ctx context.Context, userID string) context.Context
- type AuthMiddleware
- type TenantMiddleware
Constants ¶
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" )
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).
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 ¶
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 ¶
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 GetTenantID ¶
GetTenantID extracts the tenant ID from the request context.
func LoggingMiddleware ¶
LoggingMiddleware logs every HTTP request using slog structured logging. It records the method, path, status code, response size, and duration.
func RecoveryMiddleware ¶
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 WithTenantID ¶
WithTenantID returns a new context with the given tenant 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.