forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mw_oauth2_key_exists.go
74 lines (58 loc) · 1.94 KB
/
mw_oauth2_key_exists.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
package main
import (
"errors"
"net/http"
"strings"
"github.com/Sirupsen/logrus"
"github.com/TykTechnologies/tyk/apidef"
)
// Oauth2KeyExists will check if the key being used to access the API is in the request data,
// and then if the key is in the storage engine
type Oauth2KeyExists struct {
*BaseMiddleware
}
func (k *Oauth2KeyExists) GetName() string {
return "Oauth2KeyExists"
}
// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (k *Oauth2KeyExists) ProcessRequest(w http.ResponseWriter, r *http.Request, _ interface{}) (error, int) {
// We're using OAuth, start checking for access keys
token := r.Header.Get("Authorization")
parts := strings.Split(token, " ")
if len(parts) < 2 {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": GetIPFromRequest(r),
}).Info("Attempted access with malformed header, no auth header found.")
return errors.New("Authorization field missing"), 400
}
if strings.ToLower(parts[0]) != "bearer" {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": GetIPFromRequest(r),
}).Info("Bearer token malformed")
return errors.New("Bearer token malformed"), 400
}
accessToken := parts[1]
session, keyExists := k.CheckSessionAndIdentityForValidKey(accessToken)
if !keyExists {
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": GetIPFromRequest(r),
"key": accessToken,
}).Info("Attempted access with non-existent key.")
// Fire Authfailed Event
AuthFailed(k.BaseMiddleware, r, accessToken)
// Report in health check
ReportHealthCheckValue(k.Spec.Health, KeyFailure, "-1")
return errors.New("Key not authorised"), 403
}
// Set session state on context, we will need it later
switch k.Spec.BaseIdentityProvidedBy {
case apidef.OAuthKey, apidef.UnsetAuth:
ctxSetSession(r, &session)
ctxSetAuthToken(r, accessToken)
}
// Request is valid, carry on
return nil, 200
}