forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap_auth_handler.go
183 lines (146 loc) · 4.5 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
package main
import (
"errors"
ldap "github.com/mavricknz/ldap"
"strings"
)
// LDAPStorageHandler implements StorageHandler, 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(confMeta interface{}) {
asMap := confMeta.(map[string]interface{})
l.LDAPServer = asMap["ldap_server"].(string)
l.LDAPPort = uint16(asMap["ldap_port"].(float64))
l.BaseDN = asMap["base_dn"].(string)
attrArray := []string{}
for _, attr := range asMap["attributes"].([]interface{}) {
val := attr.(string)
attrArray = append(attrArray, val)
}
l.Attributes = attrArray
l.SessionAttributeName = asMap["session_attribute_name"].(string)
l.SearchString = asMap["search_string"].(string)
}
func (l *LDAPStorageHandler) Connect() bool {
conn := ldap.NewLDAPConnection(l.LDAPServer, l.LDAPPort)
err := conn.Connect()
if 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) 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 string, sessionState string, timeout int64) error {
l.notifyReadOnly()
return nil
}
func (l *LDAPStorageHandler) SetRawKey(cn string, sessionState 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 (s *LDAPStorageHandler) SetRollingWindow(keyName string, per int64, val string) (int, []interface{}) {
log.Warning("Not Implemented!")
return 0, []interface{}{}
}
func (s *LDAPStorageHandler) SetRollingWindowPipeline(keyName string, per int64, val string) (int, []interface{}) {
log.Warning("Not Implemented!")
return 0, []interface{}{}
}
func (s LDAPStorageHandler) GetSet(keyName string) (map[string]string, error) {
log.Error("Not implemented")
return map[string]string{}, nil
}
func (s LDAPStorageHandler) AddToSet(keyName string, value string) {
log.Error("Not implemented")
}
func (s LDAPStorageHandler) RemoveFromSet(keyName string, value string) {
log.Error("Not implemented")
}
func (s LDAPStorageHandler) DeleteScanMatch(pattern string) bool {
log.Error("Not implemented")
return false
}