-
Notifications
You must be signed in to change notification settings - Fork 180
/
context.go
66 lines (58 loc) · 1.83 KB
/
context.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
// Package context manipulates context metadata
package context
import (
"context"
"net/http"
"strings"
"github.com/micro/go-micro/metadata"
"golang.org/x/net/http/httpguts"
"github.com/pydio/cells/common"
)
// MinioMetaFromContext prepares metadata for minio client, merging context medata
// and eventually the Context User Key value (X-Pydio-User). Used to prepare metadata
// sent by Minio Clients
func MinioMetaFromContext(ctx context.Context) (md map[string]string, ok bool) {
md = make(map[string]string)
if meta, mOk := metadata.FromContext(ctx); mOk {
for k, v := range meta {
if httpguts.ValidHeaderFieldName(k) && httpguts.ValidHeaderFieldValue(v) {
md[k] = v
}
}
}
if user := ctx.Value(common.PYDIO_CONTEXT_USER_KEY); user != nil {
md[common.PYDIO_CONTEXT_USER_KEY] = user.(string)
}
return md, len(md) > 0
}
func AppendCellsMetaFromContext(ctx context.Context, req *http.Request) {
if meta, ok := MinioMetaFromContext(ctx); ok {
for k, v := range meta {
if strings.ToLower(k) == "authorization" {
continue
}
if httpguts.ValidHeaderFieldName(k) && httpguts.ValidHeaderFieldValue(v) {
req.Header.Set(k, v)
}
}
}
}
func WithUserNameMetadata(ctx context.Context, userName string) context.Context {
md := make(map[string]string)
if meta, ok := metadata.FromContext(ctx); ok {
for k, v := range meta {
md[k] = v
}
}
md[common.PYDIO_CONTEXT_USER_KEY] = userName
ctx = metadata.NewContext(ctx, md)
// Add it as value for easier use inside the gateway, but this will not be transmitted
ctx = context.WithValue(ctx, common.PYDIO_CONTEXT_USER_KEY, userName)
return ctx
}
func ContextMetadata(ctx context.Context) (map[string]string, bool) {
return metadata.FromContext(ctx)
}
func WithMetadata(ctx context.Context, md map[string]string) context.Context {
return metadata.NewContext(ctx, md)
}