-
Notifications
You must be signed in to change notification settings - Fork 89
/
context.go
156 lines (126 loc) · 3.31 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package api
import (
"context"
"net/url"
jwt "github.com/dgrijalva/jwt-go"
"github.com/netlify/git-gateway/conf"
"github.com/netlify/git-gateway/models"
)
type Role struct {
Name string
}
type contextKey string
func (c contextKey) String() string {
return "git-gateway api context key " + string(c)
}
const (
accessTokenKey = contextKey("access_token")
tokenKey = contextKey("jwt")
requestIDKey = contextKey("request_id")
configKey = contextKey("config")
instanceIDKey = contextKey("instance_id")
instanceKey = contextKey("instance")
proxyTargetKey = contextKey("target")
signatureKey = contextKey("signature")
netlifyIDKey = contextKey("netlify_id")
)
// withToken adds the JWT token to the context.
func withToken(ctx context.Context, token *jwt.Token) context.Context {
return context.WithValue(ctx, tokenKey, token)
}
// getToken reads the JWT token from the context.
func getToken(ctx context.Context) *jwt.Token {
obj := ctx.Value(tokenKey)
if obj == nil {
return nil
}
return obj.(*jwt.Token)
}
func getClaims(ctx context.Context) *GatewayClaims {
token := getToken(ctx)
if token == nil {
return nil
}
return token.Claims.(*GatewayClaims)
}
func withRequestID(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, requestIDKey, id)
}
func getRequestID(ctx context.Context) string {
obj := ctx.Value(requestIDKey)
if obj == nil {
return ""
}
return obj.(string)
}
func getConfig(ctx context.Context) *conf.Configuration {
obj := ctx.Value(configKey)
if obj == nil {
return nil
}
config := obj.(*conf.Configuration)
return config
}
func withConfig(ctx context.Context, config *conf.Configuration) context.Context {
return context.WithValue(ctx, configKey, config)
}
func withInstanceID(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, instanceIDKey, id)
}
func withProxyTarget(ctx context.Context, target *url.URL) context.Context {
return context.WithValue(ctx, proxyTargetKey, target)
}
func getProxyTarget(ctx context.Context) *url.URL {
obj := ctx.Value(proxyTargetKey)
if obj == nil {
return nil
}
return obj.(*url.URL)
}
func withAccessToken(ctx context.Context, token string) context.Context {
return context.WithValue(ctx, accessTokenKey, token)
}
func getAccessToken(ctx context.Context) string {
obj := ctx.Value(accessTokenKey)
if obj == nil {
return ""
}
return obj.(string)
}
func getInstanceID(ctx context.Context) string {
obj := ctx.Value(instanceIDKey)
if obj == nil {
return ""
}
return obj.(string)
}
func withInstance(ctx context.Context, i *models.Instance) context.Context {
return context.WithValue(ctx, instanceKey, i)
}
func getInstance(ctx context.Context) *models.Instance {
obj := ctx.Value(instanceKey)
if obj == nil {
return nil
}
return obj.(*models.Instance)
}
func withSignature(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, signatureKey, id)
}
func getSignature(ctx context.Context) string {
obj := ctx.Value(signatureKey)
if obj == nil {
return ""
}
return obj.(string)
}
func withNetlifyID(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, netlifyIDKey, id)
}
func getNetlifyID(ctx context.Context) string {
obj := ctx.Value(netlifyIDKey)
if obj == nil {
return ""
}
return obj.(string)
}