-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathfrom_req.go
98 lines (76 loc) · 2.31 KB
/
from_req.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
package adxusercontext
import (
"context"
"strings"
"github.com/grafana/grafana-plugin-sdk-go/backend"
)
func FromQueryReq(ctx context.Context, req *backend.QueryDataRequest) context.Context {
if req == nil {
return ctx
}
idToken := getQueryReqHeader(req, "X-ID-Token")
accessToken := extractBearerToken(getQueryReqHeader(req, "Authorization"))
currentUser := CurrentUserContext{
User: req.PluginContext.User,
IdToken: idToken,
AccessToken: accessToken,
}
return WithCurrentUser(ctx, currentUser)
}
func FromResourceReq(ctx context.Context, req *backend.CallResourceRequest) context.Context {
if req == nil {
return ctx
}
idToken := getResourceReqHeader(req, "X-ID-Token")
accessToken := extractBearerToken(getResourceReqHeader(req, "Authorization"))
currentUser := CurrentUserContext{
User: req.PluginContext.User,
IdToken: idToken,
AccessToken: accessToken,
}
return WithCurrentUser(ctx, currentUser)
}
func FromHealthCheckReq(ctx context.Context, req *backend.CheckHealthRequest) context.Context {
if req == nil {
return ctx
}
// TODO: Add support for ID and access tokens
currentUser := CurrentUserContext{
User: req.PluginContext.User,
IdToken: "",
AccessToken: "",
}
return WithCurrentUser(ctx, currentUser)
}
func getQueryReqHeader(req *backend.QueryDataRequest, headerName string) string {
// TODO: Check what subset of Unicode is case-insensitive for request headers, possibly narrow to ASCII
headerNameCI := strings.ToLower(headerName)
for name, value := range req.Headers {
if strings.ToLower(name) == headerNameCI {
return value
}
}
return ""
}
func getResourceReqHeader(req *backend.CallResourceRequest, headerName string) string {
// TODO: Check what subset of Unicode is case-insensitive for request headers, possibly narrow to ASCII
headerNameCI := strings.ToLower(headerName)
for name, values := range req.Headers {
if strings.ToLower(name) == headerNameCI {
if len(values) > 0 {
return values[0]
} else {
return ""
}
}
}
return ""
}
func extractBearerToken(authorizationHeader string) string {
const bearerPrefix = "Bearer "
var accessToken string
if strings.HasPrefix(authorizationHeader, bearerPrefix) {
accessToken = strings.TrimPrefix(authorizationHeader, bearerPrefix)
}
return accessToken
}