-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscope.go
More file actions
40 lines (33 loc) · 943 Bytes
/
scope.go
File metadata and controls
40 lines (33 loc) · 943 Bytes
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
package shield
import "context"
type contextKey string
const (
tenantKey contextKey = "shield_tenant"
appKey contextKey = "shield_app"
)
// WithTenant returns a context carrying the given tenant ID.
func WithTenant(ctx context.Context, tenantID string) context.Context {
return context.WithValue(ctx, tenantKey, tenantID)
}
// TenantFromContext extracts the tenant ID from context.
// Returns empty string if not set.
func TenantFromContext(ctx context.Context) string {
v, ok := ctx.Value(tenantKey).(string)
if !ok {
return ""
}
return v
}
// WithApp returns a context carrying the given app ID.
func WithApp(ctx context.Context, appID string) context.Context {
return context.WithValue(ctx, appKey, appID)
}
// AppFromContext extracts the app ID from context.
// Returns empty string if not set.
func AppFromContext(ctx context.Context) string {
v, ok := ctx.Value(appKey).(string)
if !ok {
return ""
}
return v
}