feat: add per-user tenant scoping
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package tenancy
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type contextKey string
|
||||
|
||||
const tenantKeyContextKey contextKey = "tenancy.tenant_key"
|
||||
|
||||
// WithTenantKey returns a context scoped to the authenticated tenant boundary.
|
||||
// The tenant key is intentionally opaque; today it is the authenticated API key
|
||||
// or OAuth client id, and callers should not interpret it as a human username.
|
||||
func WithTenantKey(ctx context.Context, tenantKey string) context.Context {
|
||||
tenantKey = strings.TrimSpace(tenantKey)
|
||||
if tenantKey == "" {
|
||||
return ctx
|
||||
}
|
||||
return context.WithValue(ctx, tenantKeyContextKey, tenantKey)
|
||||
}
|
||||
|
||||
func KeyFromContext(ctx context.Context) (string, bool) {
|
||||
if ctx == nil {
|
||||
return "", false
|
||||
}
|
||||
value, ok := ctx.Value(tenantKeyContextKey).(string)
|
||||
value = strings.TrimSpace(value)
|
||||
return value, ok && value != ""
|
||||
}
|
||||
Reference in New Issue
Block a user