forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coprocess_helpers.go
158 lines (133 loc) · 4.44 KB
/
coprocess_helpers.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
// +build coprocess
package main
import (
"github.com/TykTechnologies/tyk/coprocess"
)
// TykSessionState takes a coprocess.SessionState (as returned by the Protocol Buffer binding), and outputs a standard Tyk SessionState.
func TykSessionState(sessionState *coprocess.SessionState) SessionState {
var session SessionState
accessDefinitions := make(map[string]AccessDefinition, len(sessionState.AccessRights))
for key, protoAccessDefinition := range sessionState.AccessRights {
allowedUrls := make([]AccessSpec, len(protoAccessDefinition.AllowedUrls))
for _, protoAllowedURL := range protoAccessDefinition.AllowedUrls {
allowedURL := AccessSpec{protoAllowedURL.Url, protoAllowedURL.Methods}
allowedUrls = append(allowedUrls, allowedURL)
}
accessDefinition := AccessDefinition{protoAccessDefinition.ApiName, protoAccessDefinition.ApiId, protoAccessDefinition.Versions, allowedUrls}
accessDefinitions[key] = accessDefinition
}
var basicAuthData struct {
Password string `json:"password" msg:"password"`
Hash HashType `json:"hash_type" msg:"hash_type"`
}
if sessionState.BasicAuthData != nil {
basicAuthData.Password = sessionState.BasicAuthData.Password
basicAuthData.Hash = HashType(sessionState.BasicAuthData.Hash)
}
var jwtData struct {
Secret string `json:"secret" msg:"secret"`
}
if sessionState.JwtData != nil {
jwtData.Secret = sessionState.JwtData.Secret
}
var monitor struct {
TriggerLimits []float64 `json:"trigger_limits" msg:"trigger_limits"`
}
if sessionState.Monitor != nil {
monitor.TriggerLimits = sessionState.Monitor.TriggerLimits
}
session = SessionState{
sessionState.LastCheck,
sessionState.Allowance,
sessionState.Rate,
sessionState.Per,
sessionState.Expires,
sessionState.QuotaMax,
sessionState.QuotaRenews,
sessionState.QuotaRemaining,
sessionState.QuotaRenewalRate,
accessDefinitions,
sessionState.OrgId,
sessionState.OauthClientId,
sessionState.OauthKeys,
basicAuthData,
jwtData,
sessionState.HmacEnabled,
sessionState.HmacSecret,
sessionState.IsInactive,
sessionState.ApplyPolicyId,
sessionState.DataExpires,
monitor,
sessionState.EnableDetailedRecording,
nil,
sessionState.Tags,
sessionState.Alias,
sessionState.LastUpdated,
sessionState.IdExtractorDeadline,
sessionState.SessionLifetime,
"",
}
return session
}
// ProtoSessionState takes a standard SessionState and outputs a SessionState object compatible with Protocol Buffers.
func ProtoSessionState(sessionState SessionState) *coprocess.SessionState {
accessDefinitions := make(map[string]*coprocess.AccessDefinition, len(sessionState.AccessRights))
for key, accessDefinition := range sessionState.AccessRights {
var allowedUrls []*coprocess.AccessSpec
for _, allowedURL := range accessDefinition.AllowedURLs {
accessSpec := &coprocess.AccessSpec{allowedURL.URL, allowedURL.Methods}
allowedUrls = append(allowedUrls, accessSpec)
}
protoAccessDefinition := &coprocess.AccessDefinition{
accessDefinition.APIName, accessDefinition.APIID, accessDefinition.Versions, allowedUrls,
}
accessDefinitions[key] = protoAccessDefinition
}
var basicAuthData *coprocess.BasicAuthData
basicAuthData = &coprocess.BasicAuthData{sessionState.BasicAuthData.Password, string(sessionState.BasicAuthData.Hash)}
var jwtData *coprocess.JWTData
jwtData = &coprocess.JWTData{sessionState.JWTData.Secret}
var monitor *coprocess.Monitor
monitor = &coprocess.Monitor{sessionState.Monitor.TriggerLimits}
session := &coprocess.SessionState{
sessionState.LastCheck,
sessionState.Allowance,
sessionState.Rate,
sessionState.Per,
sessionState.Expires,
sessionState.QuotaMax,
sessionState.QuotaRenews,
sessionState.QuotaRemaining,
sessionState.QuotaRenewalRate,
accessDefinitions,
sessionState.OrgID,
sessionState.OauthClientID,
sessionState.OauthKeys,
basicAuthData,
jwtData,
sessionState.HMACEnabled,
sessionState.HmacSecret,
sessionState.IsInactive,
sessionState.ApplyPolicyID,
sessionState.DataExpires,
monitor,
sessionState.EnableDetailedRecording,
"",
sessionState.Tags,
sessionState.Alias,
sessionState.LastUpdated,
sessionState.IdExtractorDeadline,
sessionState.SessionLifetime,
}
return session
}
// ProtoMap is a helper function for maps with string slice values.
func ProtoMap(inputMap map[string][]string) map[string]string {
newMap := make(map[string]string, 0)
if inputMap != nil {
for k, v := range inputMap {
newMap[k] = v[0]
}
}
return newMap
}