-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetadata.go
83 lines (71 loc) · 2.54 KB
/
metadata.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package myjson
import "context"
type ctxKey int
const (
metadataKey ctxKey = 0
)
var (
// MetadataKeyNamespace is the key for the database namespace - it will return as "default" if not set
MetadataKeyNamespace = "namespace"
// MetadataKeyUserID is the key for the user id for use in x-authorizers (optional)
MetadataKeyUserID = "userId"
// MetadataKeyRoles is the key for the user roles([]string) for use in x-authorizers (optional)
MetadataKeyRoles = "roles"
// MetadataKeyGroups is the key for the user groups([]string) for use in x-authorizers (optional)
MetadataKeyGroups = "groups"
)
// GetMetadataValue gets a metadata value from the context if it exists
func GetMetadataValue(ctx context.Context, key string) any {
m, ok := ctx.Value(metadataKey).(*Document)
if ok {
val := m.Get(key)
if val == nil && key == MetadataKeyNamespace {
return "default"
}
return val
}
if key == MetadataKeyNamespace {
return "default"
}
return nil
}
// SetMetadataValues sets metadata key value pairs in the context
func SetMetadataValues(ctx context.Context, data map[string]any) context.Context {
m := ExtractMetadata(ctx)
_ = m.SetAll(data)
return context.WithValue(ctx, metadataKey, m)
}
// SetMetadataNamespace sets the metadata namespace
func SetMetadataNamespace(ctx context.Context, namespace string) context.Context {
m := ExtractMetadata(ctx)
_ = m.Set(MetadataKeyNamespace, namespace)
return context.WithValue(ctx, metadataKey, m)
}
// SetMetadataUserID sets the metadata userID for targeting in the collections x-authorizers
func SetMetadataUserID(ctx context.Context, userID string) context.Context {
m := ExtractMetadata(ctx)
_ = m.Set(MetadataKeyUserID, userID)
return context.WithValue(ctx, metadataKey, m)
}
// SetMetadataRoles sets the metadata user roles for targeting in the collections x-authorizers
func SetMetadataRoles(ctx context.Context, roles []string) context.Context {
m := ExtractMetadata(ctx)
_ = m.Set(MetadataKeyRoles, roles)
return context.WithValue(ctx, metadataKey, m)
}
// SetMetadataGroups sets the metadata user groups for targeting in the collections x-authorizers
func SetMetadataGroups(ctx context.Context, groups []string) context.Context {
m := ExtractMetadata(ctx)
_ = m.Set(MetadataKeyGroups, groups)
return context.WithValue(ctx, metadataKey, m)
}
// ExtractMetadata extracts metadata from the context and returns it
func ExtractMetadata(ctx context.Context) *Document {
m, ok := ctx.Value(metadataKey).(*Document)
if ok {
return m
}
m = NewDocument()
_ = m.Set(MetadataKeyNamespace, "default")
return m
}