31 lines
819 B
Go
31 lines
819 B
Go
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 != ""
|
|
}
|