forked from pydio/cells
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
131 lines (119 loc) · 4.03 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* Copyright (c) 2019. Abstrium SAS <team (at) pydio.com>
* This file is part of Pydio Cells.
*
* Pydio Cells is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio Cells is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio Cells. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <https://pydio.com>.
*/
// 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 strings.ToLower(k) == "x-pydio-claims" {
continue
}
if httpguts.ValidHeaderFieldName(k) && httpguts.ValidHeaderFieldValue(v) {
md[k] = v
}
}
}
if user := ctx.Value(common.PydioContextUserKey); user != nil {
md[common.PydioContextUserKey] = user.(string)
}
return md, len(md) > 0
}
// AppendCellsMetaFromContext extract valid header names from context and push them
// to the request Headers
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)
}
}
}
}
// WithUserNameMetadata appends a user name to both the context metadata and as context key.
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.PydioContextUserKey] = 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.PydioContextUserKey, userName)
return ctx
}
// ContextMetadata wraps micro lib metadata.FromContext call
func ContextMetadata(ctx context.Context) (map[string]string, bool) {
return metadata.FromContext(ctx)
}
// WithMetadata wraps micro lib metadata.NewContext call
func WithMetadata(ctx context.Context, md map[string]string) context.Context {
return metadata.NewContext(ctx, md)
}
// CanonicalMeta extract header name or its lowercase version
func CanonicalMeta(ctx context.Context, name string) (string, bool) {
if md, o := ContextMetadata(ctx); o {
if val, ok := md[name]; ok {
return val, true
} else if val, ok := md[strings.ToLower(name)]; ok {
return val, true
}
}
return "", false
}
// WithAdditionalMetadata retrieves existing meta, adds new key/values to the map and produces a new context
func WithAdditionalMetadata(ctx context.Context, meta map[string]string) context.Context {
md := make(map[string]string)
if meta, ok := metadata.FromContext(ctx); ok {
for k, v := range meta {
md[k] = v
}
}
for k, v := range meta {
md[k] = v
}
return metadata.NewContext(ctx, md)
}
// ContextWithMetaCopy makes sure the metadata map will is replicated and unique to this context
func WithMetaCopy(ctx context.Context) context.Context {
md := make(map[string]string)
if meta, ok := metadata.FromContext(ctx); ok {
for k, v := range meta {
md[k] = v
}
}
return metadata.NewContext(ctx, md)
}