forked from documize/community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap.go
292 lines (246 loc) · 6.71 KB
/
ldap.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package ldap
import (
"crypto/tls"
"fmt"
"math"
"strings"
"github.com/documize/community/core/stringutil"
lm "github.com/documize/community/model/auth"
"github.com/documize/community/model/user"
"github.com/pkg/errors"
ld "gopkg.in/ldap.v2"
)
// Connect establishes connection to LDAP server.
func connect(c lm.LDAPConfig) (l *ld.Conn, err error) {
address := fmt.Sprintf("%s:%d", c.ServerHost, c.ServerPort)
fmt.Println("Connecting to LDAP server", address)
l, err = ld.Dial("tcp", address)
if err != nil {
err = errors.Wrap(err, "unable to dial LDAP server")
return
}
if c.EncryptionType == lm.EncryptionTypeStartTLS {
fmt.Println("Using StartTLS with LDAP server")
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true})
if err != nil {
err = errors.Wrap(err, "unable to startTLS with LDAP server")
return
}
}
return
}
// Authenticate user against LDAP provider.
func authenticate(l *ld.Conn, c lm.LDAPConfig, username, pwd string) (lu lm.LDAPUser, success bool, err error) {
success = false
err = nil
userAttrs := c.GetUserFilterAttributes()
filter := fmt.Sprintf("(%s=%s)", c.AttributeUserRDN, username)
searchRequest := ld.NewSearchRequest(
c.BaseDN,
ld.ScopeWholeSubtree, ld.NeverDerefAliases, 0, 0, false,
filter,
userAttrs,
nil,
)
err = l.Bind(c.BindDN, c.BindPassword)
if err != nil {
errors.Wrap(err, "unable to bind admin user")
return
}
sr, err := l.Search(searchRequest)
if err != nil {
err = errors.Wrap(err, "unable to execute LDAP search during authentication")
return
}
if len(sr.Entries) == 0 {
err = errors.Wrap(err, "user not found during LDAP authentication")
return
}
if len(sr.Entries) != 1 {
err = errors.Wrap(err, "dupe users found during LDAP authentication")
return
}
userdn := sr.Entries[0].DN
// Bind as the user to verify their password
err = l.Bind(userdn, pwd)
if err != nil {
return
}
lu = extractUser(c, sr.Entries[0])
success = true
return
}
// ExecuteUserFilter returns all matching LDAP users.
func executeUserFilter(c lm.LDAPConfig) (u []lm.LDAPUser, err error) {
l, err := connect(c)
if err != nil {
err = errors.Wrap(err, "unable to dial LDAP server")
return
}
defer l.Close()
// Authenticate with LDAP server using admin credentials.
err = l.Bind(c.BindDN, c.BindPassword)
if err != nil {
errors.Wrap(err, "unable to bind admin user")
return
}
searchRequest := ld.NewSearchRequest(
c.BaseDN,
ld.ScopeWholeSubtree, ld.NeverDerefAliases, math.MaxInt32, 0, false,
c.UserFilter,
c.GetUserFilterAttributes(),
nil,
)
sr, err := l.SearchWithPaging(searchRequest, lm.MaxPageSize)
if err != nil {
errors.Wrap(err, "unable to execute directory search for user filter "+c.UserFilter)
return
}
for _, e := range sr.Entries {
u = append(u, extractUser(c, e))
}
return
}
// ExecuteGroupFilter returns all matching LDAP users that are paft of specified groups.
func executeGroupFilter(c lm.LDAPConfig) (u []lm.LDAPUser, err error) {
l, err := connect(c)
if err != nil {
err = errors.Wrap(err, "unable to dial LDAP server")
return
}
defer l.Close()
// Authenticate with LDAP server using admin credentials.
err = l.Bind(c.BindDN, c.BindPassword)
if err != nil {
errors.Wrap(err, "unable to bind admin user")
return
}
searchRequest := ld.NewSearchRequest(
c.BaseDN,
ld.ScopeWholeSubtree, ld.NeverDerefAliases, math.MaxInt32, 0, false,
c.GroupFilter,
c.GetGroupFilterAttributes(),
nil,
)
sr, err := l.SearchWithPaging(searchRequest, lm.MaxPageSize)
if err != nil {
errors.Wrap(err, "unable to execute directory search for user filter "+c.GroupFilter)
return
}
for _, g := range sr.Entries {
rawMembers := g.GetAttributeValues(c.AttributeGroupMember)
if len(rawMembers) == 0 {
continue
}
for _, entry := range rawMembers {
// get CN element from DN
parts := strings.Split(entry, ",")
if len(parts) == 0 {
continue
}
filter := fmt.Sprintf("(%s)", parts[0])
usr := ld.NewSearchRequest(
c.BaseDN,
ld.ScopeWholeSubtree, ld.NeverDerefAliases, 0, 0, false,
filter,
c.GetUserFilterAttributes(),
nil,
)
ue, err := l.Search(usr)
if err != nil {
continue
}
if len(ue.Entries) > 0 {
for _, ur := range ue.Entries {
u = append(u, extractUser(c, ur))
}
}
}
}
return
}
// extractUser build user record from LDAP result attributes.
func extractUser(c lm.LDAPConfig, e *ld.Entry) (u lm.LDAPUser) {
u.Firstname = e.GetAttributeValue(c.AttributeUserFirstname)
u.Lastname = e.GetAttributeValue(c.AttributeUserLastname)
u.Email = strings.ToLower(e.GetAttributeValue(c.AttributeUserEmail))
u.RemoteID = e.GetAttributeValue(c.AttributeUserRDN)
u.CN = e.GetAttributeValue("cn")
if len(u.Firstname) == 0 {
u.Firstname = "LDAP"
}
if len(u.Lastname) == 0 {
u.Lastname = "User"
}
return
}
// ConvertUsers creates a unique list of users using email as primary key.
// The result is a collection of Documize user objects.
func convertUsers(c lm.LDAPConfig, lu []lm.LDAPUser) (du []user.User) {
for _, i := range lu {
add := true
for _, j := range du {
if len(j.Email) > 0 && i.Email == j.Email {
add = false
break
}
}
// skip if empty email address
add = len(i.Email) > 0
if add {
du = append(du, convertUser(c, i))
}
}
return
}
// ConvertUser turns LDAP user into Documize user.
func convertUser(c lm.LDAPConfig, lu lm.LDAPUser) (du user.User) {
du = user.User{}
du.Active = true
du.ViewUsers = false
du.Analytics = false
du.Admin = false
du.GlobalAdmin = false
du.Editor = c.DefaultPermissionAddSpace
du.Email = lu.Email
du.Firstname = lu.Firstname
du.Lastname = lu.Lastname
du.Initials = stringutil.MakeInitials(lu.Firstname, lu.Lastname)
return
}
// FetchUsers from LDAP server using both User and Group filters.
func fetchUsers(c lm.LDAPConfig) (du []user.User, err error) {
du = []user.User{}
e1 := []lm.LDAPUser{}
e2 := []lm.LDAPUser{}
e3 := []lm.LDAPUser{}
if len(c.UserFilter) > 0 {
e1, err = executeUserFilter(c)
if err != nil {
err = errors.Wrap(err, "unable to execute user filter")
return
}
}
if len(c.GroupFilter) > 0 {
e2, err = executeGroupFilter(c)
if err != nil {
err = errors.Wrap(err, "unable to execute group filter")
return
}
}
// convert users from LDAP format to Documize format.
e3 = append(e3, e1...)
e3 = append(e3, e2...)
du = convertUsers(c, e3)
return
}