forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_manager.go
177 lines (148 loc) · 5.22 KB
/
auth_manager.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package main
import (
"encoding/base64"
"encoding/json"
"github.com/Sirupsen/logrus"
"github.com/nu7hatch/gouuid"
"strings"
"time"
)
// AuthorisationHandler is used to validate a session key,
// implementing IsKeyAuthorised() to validate if a key exists or
// is valid in any way (e.g. cryptographic signing etc.). Returns
// a SessionState object (deserialised JSON)
type AuthorisationHandler interface {
Init(StorageHandler)
IsKeyAuthorised(string) (SessionState, bool)
IsKeyExpired(*SessionState) bool
}
// SessionHandler handles all update/create/access session functions and deals exclusively with
// SessionState objects, not identity
type SessionHandler interface {
Init(store StorageHandler)
UpdateSession(keyName string, session SessionState, resetTTLTo int64) error
RemoveSession(keyName string)
GetSessionDetail(keyName string) (SessionState, bool)
GetSessions(filter string) []string
GetStore() StorageHandler
ResetQuota(string, SessionState)
}
type KeyGenerator interface {
GenerateAuthKey(OrgID string) string
GenerateHMACSecret() string
}
// DefaultAuthorisationManager implements AuthorisationHandler,
// requires a StorageHandler to interact with key store
type DefaultAuthorisationManager struct {
Store StorageHandler
}
type DefaultSessionManager struct {
Store StorageHandler
}
func (b *DefaultAuthorisationManager) Init(store StorageHandler) {
b.Store = store
b.Store.Connect()
}
// IsKeyAuthorised checks if key exists and can be read into a SessionState object
func (b DefaultAuthorisationManager) IsKeyAuthorised(keyName string) (SessionState, bool) {
jsonKeyVal, err := b.Store.GetKey(keyName)
var newSession SessionState
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "auth-mgr",
"inbound-key": keyName,
"err": err,
}).Warning("Key not found in storage engine")
return newSession, false
}
if marshalErr := json.Unmarshal([]byte(jsonKeyVal), &newSession); marshalErr != nil {
log.Error("Couldn't unmarshal session object")
log.Error(marshalErr)
return newSession, false
}
return newSession, true
}
// IsKeyExpired checks if a key has expired, if the value of SessionState.Expires is 0, it will be ignored
func (b DefaultAuthorisationManager) IsKeyExpired(newSession *SessionState) bool {
if newSession.Expires >= 1 {
//diff := newSession.Expires - time.Now().Unix()
if time.Now().After(time.Unix(newSession.Expires, 0)) {
return true
}
return false
}
return false
}
func (b *DefaultSessionManager) Init(store StorageHandler) {
b.Store = store
b.Store.Connect()
}
func (b *DefaultSessionManager) GetStore() StorageHandler {
return b.Store
}
func (b *DefaultSessionManager) ResetQuota(keyName string, session SessionState) {
rawKey := QuotaKeyPrefix + publicHash(keyName)
log.WithFields(logrus.Fields{
"prefix": "auth-mgr",
"inbound-key": keyName,
"key": rawKey,
}).Info("Reset quota for key.")
rateLimiterSentinelKey := RateLimitKeyPrefix + publicHash(keyName) + ".BLOCKED"
// Clear the rate limiter
go b.Store.DeleteRawKey(rateLimiterSentinelKey)
// Fix the raw key
go b.Store.DeleteRawKey(rawKey)
//go b.Store.SetKey(rawKey, "0", session.QuotaRenewalRate)
}
// UpdateSession updates the session state in the storage engine
func (b DefaultSessionManager) UpdateSession(keyName string, session SessionState, resetTTLTo int64) error {
v, _ := json.Marshal(session)
// Keep the TTL
if config.UseAsyncSessionWrite {
go b.Store.SetKey(keyName, string(v), int64(resetTTLTo))
return nil
}
err := b.Store.SetKey(keyName, string(v), int64(resetTTLTo))
return err
}
func (b DefaultSessionManager) RemoveSession(keyName string) {
b.Store.DeleteKey(keyName)
}
// GetSessionDetail returns the session detail using the storage engine (either in memory or Redis)
func (b DefaultSessionManager) GetSessionDetail(keyName string) (SessionState, bool) {
jsonKeyVal, err := b.Store.GetKey(keyName)
var thisSession SessionState
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "auth-mgr",
"inbound-key": keyName,
"err": err,
}).Debug("Could not get session detail, key not found")
return thisSession, false
}
if marshalErr := json.Unmarshal([]byte(jsonKeyVal), &thisSession); marshalErr != nil {
log.Error("Couldn't unmarshal session object (may be cache miss): ", marshalErr)
return thisSession, false
}
return thisSession, true
}
// GetSessions returns all sessions in the key store that match a filter key (a prefix)
func (b DefaultSessionManager) GetSessions(filter string) []string {
return b.Store.GetKeys(filter)
}
type DefaultKeyGenerator struct {
}
// GenerateAuthKey is a utility function for generating new auth keys. Returns the storage key name and the actual key
func (b DefaultKeyGenerator) GenerateAuthKey(OrgID string) string {
u5, _ := uuid.NewV4()
cleanSting := strings.Replace(u5.String(), "-", "", -1)
newAuthKey := expandKey(OrgID, cleanSting)
return newAuthKey
}
// GenerateHMACSecret is a utility function for generating new auth keys. Returns the storage key name and the actual key
func (b DefaultKeyGenerator) GenerateHMACSecret() string {
u5, _ := uuid.NewV4()
cleanSting := strings.Replace(u5.String(), "-", "", -1)
newSecret := base64.StdEncoding.EncodeToString([]byte(cleanSting))
return newSecret
}