-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequest.go
79 lines (63 loc) · 2.12 KB
/
request.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
package redtape
import "context"
// Request represents a request to be matched against a policy set.
type Request struct {
Resource string `json:"resource"`
Action string `json:"action"`
Role string `json:"subject"`
Scope string `json:"scope"`
Context context.Context `json:"-"`
}
func NewRequest(res, action, role, scope string, meta ...map[string]interface{}) *Request {
return &Request{
Resource: res,
Action: action,
Role: role,
Scope: scope,
Context: NewRequestContext(context.Background(), meta...),
}
}
// NewRequestWithContext builds a request from the provided parameters.
func NewRequestWithContext(ctx context.Context, res, action, role, scope string, meta ...map[string]interface{}) *Request {
return &Request{
Resource: res,
Action: action,
Role: role,
Scope: scope,
Context: NewRequestContext(ctx, meta...),
}
}
// Metadata returns metadata stored in context or an empty set.
func (r *Request) Metadata() RequestMetadata {
return RequestMetadataFromContext(r.Context)
}
// RequestMetadata is a helper type to allow type safe retrieval.
type RequestMetadata map[string]interface{}
// RequestMetadataKey is a type to identify RequestMetadata embedded in context.
type RequestMetadataKey struct{}
// NewRequestContext builds a context object from an existing context, embedding request metadata. If nil
// values are provided to both arguments, new values are created or returned.
func NewRequestContext(ctx context.Context, meta ...map[string]interface{}) context.Context {
if ctx == nil {
ctx = context.Background()
}
reqmeta := RequestMetadata{}
for _, md := range meta {
for k, v := range md {
reqmeta[k] = v
}
}
ctx = context.WithValue(ctx, RequestMetadataKey{}, reqmeta)
return ctx
}
// RequestMetadataFromContext extracts RequestMetadata from a given context or returns an empty metadata set.
func RequestMetadataFromContext(ctx context.Context) RequestMetadata {
if ctx == nil {
return RequestMetadata{}
}
md := ctx.Value(RequestMetadataKey{})
if md == nil {
return RequestMetadata{}
}
return md.(RequestMetadata)
}