forked from jtblin/go-ldap-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldap-client.go
246 lines (214 loc) · 5.5 KB
/
ldap-client.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Package ldap provides a simple ldap client to authenticate,
// retrieve basic information and groups for a user.
package ldap
import (
"crypto/tls"
"errors"
"fmt"
"regexp"
ldap "gopkg.in/ldap.v2"
)
type LDAPClient struct {
Attributes []string
Base string
BindDN string
BindPassword string
GroupFilter string // e.g. "(memberUid=%s)"
Host string
ServerName string
UserFilter string // e.g. "(uid=%s)"
Conn *ldap.Conn
Port int
InsecureSkipVerify bool
UseSSL bool
SkipTLS bool
ClientCertificates []tls.Certificate // Adding client certificates
}
// Connect connects to the ldap backend.
func (lc *LDAPClient) Connect() error {
if lc.Conn == nil {
var l *ldap.Conn
var err error
address := fmt.Sprintf("%s:%d", lc.Host, lc.Port)
if !lc.UseSSL {
l, err = ldap.Dial("tcp", address)
if err != nil {
return err
}
// Reconnect with TLS
if !lc.SkipTLS {
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
return err
}
}
} else {
config := &tls.Config{
InsecureSkipVerify: lc.InsecureSkipVerify,
ServerName: lc.ServerName,
}
if lc.ClientCertificates != nil && len(lc.ClientCertificates) > 0 {
config.Certificates = lc.ClientCertificates
}
l, err = ldap.DialTLS("tcp", address, config)
if err != nil {
return err
}
}
lc.Conn = l
err = lc.Conn.Bind(lc.BindDN, lc.BindPassword)
if err != nil {
return fmt.Errorf("cannot bind to ldap. Reason %s", err)
}
}
return nil
}
// Close closes the ldap backend connection.
func (lc *LDAPClient) Close() {
if lc.Conn != nil {
lc.Conn.Close()
lc.Conn = nil
}
}
// Authenticate authenticates the user against the ldap backend.
func (lc *LDAPClient) Authenticate(username, password string) (bool, map[string]string, error) {
err := lc.Connect()
if err != nil {
return false, nil, err
}
// First bind with a read only user
if lc.BindDN != "" && lc.BindPassword != "" {
err := lc.Conn.Bind(lc.BindDN, lc.BindPassword)
if err != nil {
return false, nil, err
}
}
attributes := append(lc.Attributes, "dn")
// Search for the given username
searchRequest := ldap.NewSearchRequest(
lc.Base,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf(lc.UserFilter, username),
attributes,
nil,
)
sr, err := lc.Conn.Search(searchRequest)
if err != nil {
return false, nil, err
}
if len(sr.Entries) < 1 {
return false, nil, errors.New("User does not exist")
}
if len(sr.Entries) > 1 {
return false, nil, errors.New("Too many entries returned")
}
userDN := sr.Entries[0].DN
user := map[string]string{}
for _, attr := range lc.Attributes {
user[attr] = sr.Entries[0].GetAttributeValue(attr)
}
// Bind as the user to verify their password
err = lc.Conn.Bind(userDN, password)
if err != nil {
return false, user, err
}
// Rebind as the read only user for any further queries
if lc.BindDN != "" && lc.BindPassword != "" {
err = lc.Conn.Bind(lc.BindDN, lc.BindPassword)
if err != nil {
return true, user, err
}
}
return true, user, nil
}
// GetGroupsOfUser returns the group for a user.
func (lc *LDAPClient) GetGroupsOfUser(username string) ([]string, error) {
err := lc.Connect()
if err != nil {
return nil, err
}
searchRequest := ldap.NewSearchRequest(
lc.Base,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf(lc.GroupFilter, username),
[]string{"cn"}, // can it be something else than "cn"?
nil,
)
sr, err := lc.Conn.Search(searchRequest)
if err != nil {
return nil, err
}
groups := []string{}
for _, entry := range sr.Entries {
groups = append(groups, entry.GetAttributeValue("cn"))
}
return groups, nil
}
// GetGroupsOfUserAD returns the groups for a ActiveDirectory user
func (lc *LDAPClient) GetGroupsOfUserAD(username string) ([]string, error) {
err := lc.Connect()
if err != nil {
return nil, err
}
searchRequest := ldap.NewSearchRequest(
lc.Base,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf(lc.GroupFilter, username),
[]string{"memberOf"},
nil,
)
sr, err := lc.Conn.Search(searchRequest)
if err != nil {
return nil, err
}
groups := []string{}
// return groups, sr.Entries, nil
regexCN := regexp.MustCompile(`CN=(.*?),`)
for _, x := range sr.Entries {
for _, y := range x.Attributes {
if y.Name == "memberOf" {
for _, z := range y.Values {
group := regexCN.FindStringSubmatch(z)[1]
groups = append(groups, group)
}
}
}
}
return groups, nil
}
func (lc *LDAPClient) GetCNUserAD(username string) (string, error) {
err := lc.Connect()
if err != nil {
return "", err
}
// First bind with a read only user
if lc.BindDN != "" && lc.BindPassword != "" {
err := lc.Conn.Bind(lc.BindDN, lc.BindPassword)
if err != nil {
return "", err
}
}
attributes := append(lc.Attributes, "dn")
// Search for the given username
searchRequest := ldap.NewSearchRequest(
lc.Base,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf(lc.UserFilter, username),
attributes,
nil,
)
sr, err := lc.Conn.Search(searchRequest)
if err != nil {
return "", err
}
if len(sr.Entries) < 1 {
return "", errors.New("User does not exist")
}
if len(sr.Entries) > 1 {
return "", errors.New("Too many entries returned")
}
userDN := sr.Entries[0].DN
regexCN := regexp.MustCompile(`CN=(.*?),`)
CN := regexCN.FindStringSubmatch(userDN)[1]
return CN, nil
}