@@ -97,7 +97,7 @@ func checkAndApplyTrialPeriod(keyName, apiId string, newSession *SessionState) {
97
97
}
98
98
}
99
99
100
- func doAddOrUpdate (keyName string , newSession SessionState , dontReset bool ) error {
100
+ func doAddOrUpdate (keyName string , newSession * SessionState , dontReset bool ) error {
101
101
newSession .LastUpdated = strconv .Itoa (int (time .Now ().Unix ()))
102
102
103
103
if len (newSession .AccessRights ) > 0 {
@@ -117,7 +117,7 @@ func doAddOrUpdate(keyName string, newSession SessionState, dontReset bool) erro
117
117
}).Error ("Could not add key for this API ID, API doesn't exist." )
118
118
return errors .New ("API must be active to add keys" )
119
119
}
120
- checkAndApplyTrialPeriod (keyName , apiId , & newSession )
120
+ checkAndApplyTrialPeriod (keyName , apiId , newSession )
121
121
122
122
// Lets reset keys if they are edited by admin
123
123
if ! apiSpec .DontSetQuotasOnCreate {
@@ -127,7 +127,7 @@ func doAddOrUpdate(keyName string, newSession SessionState, dontReset bool) erro
127
127
newSession .QuotaRenews = time .Now ().Unix () + newSession .QuotaRenewalRate
128
128
}
129
129
130
- err := apiSpec .SessionManager .UpdateSession (keyName , newSession , getLifetime (apiSpec , & newSession ))
130
+ err := apiSpec .SessionManager .UpdateSession (keyName , newSession , getLifetime (apiSpec , newSession ))
131
131
if err != nil {
132
132
return err
133
133
}
@@ -145,8 +145,8 @@ func doAddOrUpdate(keyName string, newSession SessionState, dontReset bool) erro
145
145
spec .SessionManager .ResetQuota (keyName , newSession )
146
146
newSession .QuotaRenews = time .Now ().Unix () + newSession .QuotaRenewalRate
147
147
}
148
- checkAndApplyTrialPeriod (keyName , spec .APIID , & newSession )
149
- err := spec .SessionManager .UpdateSession (keyName , newSession , getLifetime (spec , & newSession ))
148
+ checkAndApplyTrialPeriod (keyName , spec .APIID , newSession )
149
+ err := spec .SessionManager .UpdateSession (keyName , newSession , getLifetime (spec , newSession ))
150
150
if err != nil {
151
151
return err
152
152
}
@@ -248,7 +248,7 @@ func handleAddOrUpdate(keyName string, r *http.Request) ([]byte, int) {
248
248
249
249
}
250
250
suppressReset := r .FormValue ("suppress_reset" ) == "1"
251
- if err := doAddOrUpdate (keyName , newSession , suppressReset ); err != nil {
251
+ if err := doAddOrUpdate (keyName , & newSession , suppressReset ); err != nil {
252
252
return createError ("Failed to create key, ensure security settings are correct." ), 500
253
253
}
254
254
@@ -398,7 +398,7 @@ func handleDeleteKey(keyName, apiID string) ([]byte, int) {
398
398
// Go through ALL managed API's and delete the key
399
399
for _ , spec := range ApiSpecRegister {
400
400
spec .SessionManager .RemoveSession (keyName )
401
- spec .SessionManager .ResetQuota (keyName , SessionState {})
401
+ spec .SessionManager .ResetQuota (keyName , & SessionState {})
402
402
}
403
403
404
404
log .WithFields (logrus.Fields {
@@ -421,7 +421,7 @@ func handleDeleteKey(keyName, apiID string) ([]byte, int) {
421
421
}
422
422
423
423
sessionManager .RemoveSession (keyName )
424
- sessionManager .ResetQuota (keyName , SessionState {})
424
+ sessionManager .ResetQuota (keyName , & SessionState {})
425
425
426
426
statusObj := APIModifyKeySuccess {keyName , "ok" , "deleted" }
427
427
responseMessage , err = json .Marshal (& statusObj )
@@ -920,9 +920,9 @@ func orgHandler(w http.ResponseWriter, r *http.Request) {
920
920
}
921
921
922
922
func handleOrgAddOrUpdate (keyName string , r * http.Request ) ([]byte , int ) {
923
- var newSession SessionState
923
+ newSession := new ( SessionState )
924
924
925
- if err := json .NewDecoder (r .Body ).Decode (& newSession ); err != nil {
925
+ if err := json .NewDecoder (r .Body ).Decode (newSession ); err != nil {
926
926
log .Error ("Couldn't decode new session object: " , err )
927
927
return createError ("Request malformed" ), 400
928
928
}
@@ -1146,8 +1146,8 @@ func createKeyHandler(w http.ResponseWriter, r *http.Request) {
1146
1146
return
1147
1147
}
1148
1148
1149
- var newSession SessionState
1150
- if err := json .NewDecoder (r .Body ).Decode (& newSession ); err != nil {
1149
+ newSession := new ( SessionState )
1150
+ if err := json .NewDecoder (r .Body ).Decode (newSession ); err != nil {
1151
1151
log .WithFields (logrus.Fields {
1152
1152
"prefix" : "api" ,
1153
1153
"status" : "fail" ,
@@ -1168,14 +1168,14 @@ func createKeyHandler(w http.ResponseWriter, r *http.Request) {
1168
1168
for apiID := range newSession .AccessRights {
1169
1169
apiSpec := GetSpecForApi (apiID )
1170
1170
if apiSpec != nil {
1171
- checkAndApplyTrialPeriod (newKey , apiID , & newSession )
1171
+ checkAndApplyTrialPeriod (newKey , apiID , newSession )
1172
1172
// If we have enabled HMAC checking for keys, we need to generate a secret for the client to use
1173
1173
if ! apiSpec .DontSetQuotasOnCreate {
1174
1174
// Reset quota by default
1175
1175
apiSpec .SessionManager .ResetQuota (newKey , newSession )
1176
1176
newSession .QuotaRenews = time .Now ().Unix () + newSession .QuotaRenewalRate
1177
1177
}
1178
- err := apiSpec .SessionManager .UpdateSession (newKey , newSession , getLifetime (apiSpec , & newSession ))
1178
+ err := apiSpec .SessionManager .UpdateSession (newKey , newSession , getLifetime (apiSpec , newSession ))
1179
1179
if err != nil {
1180
1180
responseMessage := createError ("Failed to create key - " + err .Error ())
1181
1181
doJSONWrite (w , 403 , responseMessage )
@@ -1209,13 +1209,13 @@ func createKeyHandler(w http.ResponseWriter, r *http.Request) {
1209
1209
}).Warning ("No API Access Rights set on key session, adding key to all APIs." )
1210
1210
1211
1211
for _ , spec := range ApiSpecRegister {
1212
- checkAndApplyTrialPeriod (newKey , spec .APIID , & newSession )
1212
+ checkAndApplyTrialPeriod (newKey , spec .APIID , newSession )
1213
1213
if ! spec .DontSetQuotasOnCreate {
1214
1214
// Reset quote by default
1215
1215
spec .SessionManager .ResetQuota (newKey , newSession )
1216
1216
newSession .QuotaRenews = time .Now ().Unix () + newSession .QuotaRenewalRate
1217
1217
}
1218
- err := spec .SessionManager .UpdateSession (newKey , newSession , getLifetime (spec , & newSession ))
1218
+ err := spec .SessionManager .UpdateSession (newKey , newSession , getLifetime (spec , newSession ))
1219
1219
if err != nil {
1220
1220
responseMessage := createError ("Failed to create key - " + err .Error ())
1221
1221
doJSONWrite (w , 403 , responseMessage )
@@ -1740,20 +1740,19 @@ func healthCheckhandler(w http.ResponseWriter, r *http.Request) {
1740
1740
1741
1741
func UserRatesCheck () http.HandlerFunc {
1742
1742
return func (w http.ResponseWriter , r * http.Request ) {
1743
- sessionState := context . Get ( r , SessionData )
1744
- if sessionState == nil {
1743
+ session := ctxGetSession ( r )
1744
+ if session == nil {
1745
1745
responseMessage := createError ("Health checks are not enabled for this node" )
1746
1746
doJSONWrite (w , 405 , responseMessage )
1747
1747
return
1748
1748
}
1749
1749
1750
- userSession := sessionState .(SessionState )
1751
1750
returnSession := PublicSessionState {}
1752
- returnSession .Quota .QuotaRenews = userSession .QuotaRenews
1753
- returnSession .Quota .QuotaRemaining = userSession .QuotaRemaining
1754
- returnSession .Quota .QuotaMax = userSession .QuotaMax
1755
- returnSession .RateLimit .Rate = userSession .Rate
1756
- returnSession .RateLimit .Per = userSession .Per
1751
+ returnSession .Quota .QuotaRenews = session .QuotaRenews
1752
+ returnSession .Quota .QuotaRemaining = session .QuotaRemaining
1753
+ returnSession .Quota .QuotaMax = session .QuotaMax
1754
+ returnSession .RateLimit .Rate = session .Rate
1755
+ returnSession .RateLimit .Per = session .Per
1757
1756
1758
1757
responseMessage , err := json .Marshal (returnSession )
1759
1758
if err != nil {
@@ -1836,3 +1835,17 @@ func ctxSetData(r *http.Request, m map[string]interface{}) {
1836
1835
}
1837
1836
context .Set (r , ContextData , m )
1838
1837
}
1838
+
1839
+ func ctxGetSession (r * http.Request ) * SessionState {
1840
+ if v := context .Get (r , SessionData ); v != nil {
1841
+ return v .(* SessionState )
1842
+ }
1843
+ return nil
1844
+ }
1845
+
1846
+ func ctxSetSession (r * http.Request , s * SessionState ) {
1847
+ if s == nil {
1848
+ panic ("setting a nil context SessionData" )
1849
+ }
1850
+ context .Set (r , SessionData , s )
1851
+ }
0 commit comments