forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmw_key_expired_check.go
72 lines (57 loc) · 2.03 KB
/
mw_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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"errors"
"net/http"
"github.com/Sirupsen/logrus"
)
// KeyExpired middleware will check if the requesting key is expired or not. It makes use of the authManager to do so.
type KeyExpired struct {
*BaseMiddleware
}
func (k *KeyExpired) GetName() string {
return "KeyExpired"
}
// 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, _ interface{}) (error, int) {
session := ctxGetSession(r)
if session == nil {
return errors.New("Session state is missing or unset! Please make sure that auth headers are properly applied"), 400
}
token := ctxGetAuthToken(r)
if session.IsInactive {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": GetIPFromRequest(r),
"key": token,
}).Info("Attempted access from inactive key.")
// Fire a key expired event
k.FireEvent(EventKeyExpired, EventKeyExpiredMeta{
EventMetaDefault: EventMetaDefault{Message: "Attempted access from inactive key.", OriginatingRequest: EncodeRequestToEvent(r)},
Path: r.URL.Path,
Origin: GetIPFromRequest(r),
Key: token,
})
// Report in health check
ReportHealthCheckValue(k.Spec.Health, KeyFailure, "-1")
return errors.New("Key is inactive, please renew"), 403
}
keyExpired := k.Spec.AuthManager.IsKeyExpired(session)
if keyExpired {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": GetIPFromRequest(r),
"key": token,
}).Info("Attempted access from expired key.")
// Fire a key expired event
k.FireEvent(EventKeyExpired, EventKeyExpiredMeta{
EventMetaDefault: EventMetaDefault{Message: "Attempted access from expired key."},
Path: r.URL.Path,
Origin: GetIPFromRequest(r),
Key: token,
})
// Report in health check
ReportHealthCheckValue(k.Spec.Health, KeyFailure, "-1")
return errors.New("Key has expired, please renew"), 401
}
return nil, 200
}