forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware_key_expired_check.go
51 lines (41 loc) · 1.53 KB
/
middleware_key_expired_check.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
package main
import "net/http"
import (
"errors"
"github.com/Sirupsen/logrus"
"github.com/gorilla/context"
)
// KeyExpired middleware will check if the requesting key is expired or not. It makes use of the authManager to do so.
type KeyExpired struct {
TykMiddleware
}
// New lets you do any initialisations for the object can be done here
func (k *KeyExpired) New() {}
// GetConfig retrieves the configuration from the API config - Not used for this middleware
func (k *KeyExpired) GetConfig() (interface{}, error) {
return nil, nil
}
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (k *KeyExpired) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
thisSessionState := context.Get(r, SessionData).(SessionState)
authHeaderValue := context.Get(r, AuthHeaderValue).(string)
keyExpired := k.Spec.AuthManager.IsKeyExpired(&thisSessionState)
if keyExpired {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": r.RemoteAddr,
"key": authHeaderValue,
}).Info("Attempted access from expired key.")
// Fire a key expired event
authHeaderValue := context.Get(r, AuthHeaderValue)
go k.TykMiddleware.FireEvent(EVENT_KeyExpired,
EVENT_KeyExpiredMeta{
EventMetaDefault: EventMetaDefault{Message: "Attempted access from expired key."},
Path: r.URL.Path,
Origin: r.RemoteAddr,
Key: authHeaderValue.(string),
})
return errors.New("Key has expired, please renew"), 403
}
return nil, 200
}