-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ctx.go
158 lines (133 loc) · 3.27 KB
/
ctx.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
157
158
package ctx
import (
"context"
"encoding/json"
"net/http"
"github.com/TykTechnologies/tyk/internal/httputil"
"github.com/TykTechnologies/tyk/apidef/oas"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/apidef"
logger "github.com/TykTechnologies/tyk/log"
"github.com/TykTechnologies/tyk/storage"
"github.com/TykTechnologies/tyk/user"
)
type Key uint
const (
SessionData Key = iota
// Deprecated: UpdateSession was used to trigger a session update, use *SessionData.Touch instead.
UpdateSession
AuthToken
HashedAuthToken
VersionData
VersionName
VersionDefault
OrgSessionContext
ContextData
RetainHost
TrackThisEndpoint
DoNotTrackThisEndpoint
UrlRewritePath
RequestMethod
OrigRequestURL
LoopLevel
LoopLevelLimit
ThrottleLevel
ThrottleLevelLimit
Trace
CheckLoopLimits
UrlRewriteTarget
TransformedRequestMethod
Definition
RequestStatus
GraphQLRequest
GraphQLIsWebSocketUpgrade
OASOperation
// CacheOptions holds cache options required for cache writer middleware.
CacheOptions
OASDefinition
SelfLooping
)
func ctxSetSession(r *http.Request, s *user.SessionState, scheduleUpdate bool, hashKey bool) {
if s == nil {
panic("setting a nil context SessionData")
}
if s.KeyID == "" {
s.KeyID = GetAuthToken(r)
}
if s.KeyHashEmpty() {
s.SetKeyHash(storage.HashKey(s.KeyID, hashKey))
}
ctx := r.Context()
ctx = context.WithValue(ctx, SessionData, s)
ctx = context.WithValue(ctx, AuthToken, s.KeyID)
if scheduleUpdate {
s.Touch()
}
httputil.SetContext(r, ctx)
}
func GetAuthToken(r *http.Request) string {
if v := r.Context().Value(AuthToken); v != nil {
return v.(string)
}
return ""
}
func GetSession(r *http.Request) *user.SessionState {
if v := r.Context().Value(SessionData); v != nil {
if val, ok := v.(*user.SessionState); ok {
return val
} else {
logger.Get().Warning("SessionState struct differ from the gateway version, trying to unmarshal.")
sess := user.SessionState{}
b, _ := json.Marshal(v)
e := json.Unmarshal(b, &sess)
if e == nil {
return &sess
}
}
}
return nil
}
func SetSession(r *http.Request, s *user.SessionState, scheduleUpdate bool, hashKey ...bool) {
if len(hashKey) > 1 {
ctxSetSession(r, s, scheduleUpdate, hashKey[0])
} else {
ctxSetSession(r, s, scheduleUpdate, config.Global().HashKeys)
}
}
func SetDefinition(r *http.Request, s *apidef.APIDefinition) {
ctx := r.Context()
ctx = context.WithValue(ctx, Definition, s)
httputil.SetContext(r, ctx)
}
func GetDefinition(r *http.Request) *apidef.APIDefinition {
if v := r.Context().Value(Definition); v != nil {
if val, ok := v.(*apidef.APIDefinition); ok {
return val
} else {
logger.Get().Warning("APIDefinition struct differ from the gateway version, trying to unmarshal.")
def := apidef.APIDefinition{}
b, _ := json.Marshal(v)
e := json.Unmarshal(b, &def)
if e == nil {
return &def
}
}
}
return nil
}
// GetOASDefinition returns a deep copy of the OAS definition of the called API.
func GetOASDefinition(r *http.Request) *oas.OAS {
v := r.Context().Value(OASDefinition)
if v == nil {
return nil
}
val, ok := v.(*oas.OAS)
if !ok {
return nil
}
ret, err := val.Clone()
if err != nil {
logger.Get().WithError(err).Error("Cloning OAS object in the request context")
}
return ret
}