-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.go
More file actions
49 lines (41 loc) · 1.16 KB
/
scope.go
File metadata and controls
49 lines (41 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package keysmith
import (
"context"
"github.com/xraph/forge"
)
type tenantScope struct {
appID string
tenantID string
}
type ctxKeyApp struct{}
type ctxKeyTenant struct{}
// WithTenant sets the tenant scope on the context for standalone usage
// (without Forge). This is the non-Forge equivalent of forge.Scope.
func WithTenant(ctx context.Context, appID, tenantID string) context.Context {
ctx = context.WithValue(ctx, ctxKeyApp{}, appID)
ctx = context.WithValue(ctx, ctxKeyTenant{}, tenantID)
return ctx
}
// scopeFromContext extracts tenant scope from forge.Scope or standalone context.
// Falls back to explicit tenant if Forge scope is not set (standalone mode).
func scopeFromContext(ctx context.Context) tenantScope {
fscope, ok := forge.ScopeFrom(ctx)
if ok {
return tenantScope{
appID: fscope.AppID(),
tenantID: fscope.OrgID(),
}
}
return tenantScope{
appID: appIDFromContext(ctx),
tenantID: tenantIDFromContext(ctx),
}
}
func appIDFromContext(ctx context.Context) string {
v, _ := ctx.Value(ctxKeyApp{}).(string)
return v
}
func tenantIDFromContext(ctx context.Context) string {
v, _ := ctx.Value(ctxKeyTenant{}).(string)
return v
}