forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap_auth_handler.go
215 lines (169 loc) · 5.17 KB
/
ldap_auth_handler.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package main
import (
"errors"
"strings"
"github.com/mavricknz/ldap"
)
// LDAPStorageHandler implements storage.Handler, this is a read-only implementation to access keys from an LDAP service
type LDAPStorageHandler struct {
LDAPServer string
LDAPPort uint16
BaseDN string
Attributes []string
SessionAttributeName string
SearchString string
store *ldap.LDAPConnection
}
func (l *LDAPStorageHandler) LoadConfFromMeta(meta map[string]interface{}) {
l.LDAPServer = meta["ldap_server"].(string)
l.LDAPPort = uint16(meta["ldap_port"].(float64))
l.BaseDN = meta["base_dn"].(string)
attrArray := []string{}
for _, attr := range meta["attributes"].([]interface{}) {
val := attr.(string)
attrArray = append(attrArray, val)
}
l.Attributes = attrArray
l.SessionAttributeName = meta["session_attribute_name"].(string)
l.SearchString = meta["search_string"].(string)
}
func (l *LDAPStorageHandler) Connect() bool {
conn := ldap.NewLDAPConnection(l.LDAPServer, l.LDAPPort)
if err := conn.Connect(); err != nil {
log.Error("LDAP server connection failed: ", err)
return false
}
log.Info("LDAP: Connection established")
l.store = conn
return true
}
func (l *LDAPStorageHandler) GetKey(filter string) (string, error) {
log.Debug("Searching for filter: ", filter)
useFilter := strings.Replace(l.SearchString, "TYKKEYID", filter, 1)
log.Warning("Search filter is: ", useFilter)
search_request := ldap.NewSearchRequest(
l.BaseDN,
ldap.ScopeWholeSubtree, ldap.DerefAlways, 0, 0, false,
useFilter,
l.Attributes,
nil)
sr, err := l.store.Search(search_request)
if err != nil {
log.Debug("LDAP Key search failed: ", err)
return "", err
}
if len(sr.Entries) == 0 {
return "", nil
}
log.Debug("Found Key: ", sr.Entries[0])
entry := sr.Entries[0]
if entry.Attributes == nil {
log.Error("LDAP: No attributes found to check for session state. Failing")
return "", errors.New("Attributes for entry are empty")
}
for _, attr := range entry.Attributes {
if attr.Name == l.SessionAttributeName {
log.Debug("Found session data: ", attr.Values[0])
return attr.Values[0], nil
}
}
return "", nil
}
func (l *LDAPStorageHandler) GetRawKey(filter string) (string, error) {
log.Warning("Not implementated")
return "", nil
}
func (l *LDAPStorageHandler) SetExp(cn string, exp int64) error {
log.Warning("Not implementated")
return nil
}
func (l *LDAPStorageHandler) GetExp(cn string) (int64, error) {
log.Warning("Not implementated")
return 0, nil
}
func (l *LDAPStorageHandler) GetKeys(filter string) []string {
log.Warning("Not implementated")
s := []string{}
return s
}
func (l *LDAPStorageHandler) GetKeysAndValues() map[string]string {
log.Warning("Not implementated")
s := map[string]string{}
return s
}
func (l *LDAPStorageHandler) GetKeysAndValuesWithFilter(filter string) map[string]string {
log.Warning("Not implementated")
s := map[string]string{}
return s
}
func (l *LDAPStorageHandler) SetKey(cn, session string, timeout int64) error {
l.notifyReadOnly()
return nil
}
func (l *LDAPStorageHandler) SetRawKey(cn, session string, timeout int64) error {
l.notifyReadOnly()
return nil
}
func (l *LDAPStorageHandler) DeleteKey(cn string) bool {
return l.notifyReadOnly()
}
func (l *LDAPStorageHandler) DeleteRawKey(cn string) bool {
return l.notifyReadOnly()
}
func (l *LDAPStorageHandler) DeleteKeys(keys []string) bool {
return l.notifyReadOnly()
}
func (l *LDAPStorageHandler) Decrement(keyName string) {
l.notifyReadOnly()
}
func (l *LDAPStorageHandler) IncrememntWithExpire(keyName string, timeout int64) int64 {
l.notifyReadOnly()
return 999
}
func (l *LDAPStorageHandler) notifyReadOnly() bool {
log.Warning("LDAP storage is READ ONLY")
return false
}
func (l *LDAPStorageHandler) SetRollingWindow(keyName string, per int64, val string, pipeline bool) (int, []interface{}) {
log.Warning("Not Implemented!")
return 0, nil
}
func (l LDAPStorageHandler) GetSet(keyName string) (map[string]string, error) {
log.Error("Not implemented")
return nil, nil
}
func (l LDAPStorageHandler) AddToSet(keyName, value string) {
log.Error("Not implemented")
}
func (l LDAPStorageHandler) AppendToSet(keyName, value string) {
log.Error("Not implemented")
}
func (r *LDAPStorageHandler) AppendToSetPipelined(key string, values []string) {
log.Error("Not implemented")
}
func (l LDAPStorageHandler) RemoveFromSet(keyName, value string) {
log.Error("Not implemented")
}
func (l LDAPStorageHandler) GetAndDeleteSet(keyName string) []interface{} {
log.Error("Not implemented")
return nil
}
func (l LDAPStorageHandler) DeleteScanMatch(pattern string) bool {
log.Error("Not implemented")
return false
}
func (l LDAPStorageHandler) GetKeyPrefix() string {
log.Error("Not implemented")
return ""
}
func (l LDAPStorageHandler) AddToSortedSet(keyName, value string, score float64) {
log.Error("Not implemented")
}
func (l LDAPStorageHandler) GetSortedSetRange(keyName, scoreFrom, scoreTo string) ([]string, []float64, error) {
log.Error("Not implemented")
return nil, nil, nil
}
func (l LDAPStorageHandler) RemoveSortedSetRange(keyName, scoreFrom, scoreTo string) error {
log.Error("Not implemented")
return nil
}