1
1
package io.xh.hoist.ldap
2
2
3
+
3
4
import io.xh.hoist.BaseService
4
5
import io.xh.hoist.cache.Cache
5
6
import org.apache.directory.api.ldap.model.entry.Attribute
6
- import org.apache.directory.api.ldap.model.message.SearchScope
7
7
import org.apache.directory.api.ldap.model.exception.LdapAuthenticationException
8
+ import org.apache.directory.api.ldap.model.message.SearchScope
8
9
import org.apache.directory.ldap.client.api.LdapConnectionConfig
9
- import org.apache.directory.ldap.client.api.NoVerificationTrustManager
10
10
import org.apache.directory.ldap.client.api.LdapNetworkConnection
11
+ import org.apache.directory.ldap.client.api.NoVerificationTrustManager
11
12
12
13
import static grails.async.Promises.task
13
14
import static io.xh.hoist.util.DateTimeUtils.SECONDS
@@ -53,15 +54,21 @@ class LdapService extends BaseService {
53
54
}
54
55
55
56
LdapPerson lookupUser (String sName ) {
56
- searchOne(" (sAMAccountName=$sName ) " , LdapPerson , true )
57
+ withDebug([" Looking up user" , [sAMAccountName : sName]]) {
58
+ searchOne(" (sAMAccountName=$sName ) " , LdapPerson , true )
59
+ }
57
60
}
58
61
59
62
List<LdapPerson > lookupGroupMembers (String dn ) {
60
- lookupGroupMembersInternal(dn, true )
63
+ withDebug([" Looking up group members" , [dn : dn]]) {
64
+ lookupGroupMembersInternal(dn, true )
65
+ }
61
66
}
62
67
63
68
List<LdapGroup > findGroups (String sNamePart ) {
64
- searchMany(" (sAMAccountName=*$sNamePart *)" , LdapGroup , true )
69
+ withDebug(" Finding groups with name matching *$sNamePart " ) {
70
+ searchMany(" (sAMAccountName=*$sNamePart *)" , LdapGroup , true )
71
+ }
65
72
}
66
73
67
74
/**
@@ -71,8 +78,11 @@ class LdapService extends BaseService {
71
78
* otherwise, failed lookups will be logged, and resolved as null.
72
79
*/
73
80
Map<String , LdapGroup > lookupGroups (Set<String > dns , boolean strictMode = false ) {
74
- dns. collectEntries { dn -> [dn, task { lookupGroupInternal(dn, strictMode) }] }
75
- .collectEntries { [it. key, it. value. get()] }
81
+ withDebug([" Looking up groups" , [dns : dns, strictMode : strictMode]]) {
82
+ dns. collectEntries { dn -> [dn, task { lookupGroupInternal(dn, strictMode) }] }
83
+ .collectEntries { [it. key, it. value. get()] } as Map<String , LdapGroup >
84
+ }
85
+
76
86
}
77
87
78
88
/**
@@ -82,8 +92,10 @@ class LdapService extends BaseService {
82
92
* otherwise, failed lookups will be logged, and resolved as an empty list.
83
93
*/
84
94
Map<String , List<LdapPerson > > lookupGroupMembers (Set<String > dns , boolean strictMode = false ) {
85
- dns. collectEntries { dn -> [dn, task { lookupGroupMembersInternal(dn, strictMode) }] }
86
- .collectEntries { [it. key, it. value. get()] }
95
+ withDebug([" Looking up group members" , [dns : dns, strictMode : strictMode]]) {
96
+ dns. collectEntries { dn -> [dn, task { lookupGroupMembersInternal(dn, strictMode) }] }
97
+ .collectEntries { [it. key, it. value. get()] } as Map<String , List<LdapPerson > >
98
+ }
87
99
}
88
100
89
101
/**
@@ -128,24 +140,27 @@ class LdapService extends BaseService {
128
140
* @return true if the password is valid and the test connection succeeds
129
141
*/
130
142
boolean authenticate (String username , String password ) {
131
- for (Map server in config. servers) {
132
- String host = server. host
133
- List<LdapPerson > matches = doQuery(server, " (sAMAccountName=$username )" , LdapPerson , true )
134
- if (matches) {
135
- if (matches. size() > 1 ) throw new RuntimeException (" Multiple user records found for $username " )
136
- LdapPerson user = matches. first()
137
- try (def conn = createConnection(host)) {
138
- conn. bind(user. distinguishedname, password)
139
- conn. unBind()
140
- return true
141
- } catch (LdapAuthenticationException ignored) {
142
- logDebug(' Authentication failed, incorrect credentials' , [username : username])
143
- return false
143
+ withDebug([" Attempting LDAP bind to authenticate user" , [username : username]]) {
144
+ for (Map server in config. servers) {
145
+ String host = server. host
146
+ List<LdapPerson > matches = doQuery(server, " (sAMAccountName=$username )" , LdapPerson , true )
147
+ if (matches) {
148
+ if (matches. size() > 1 ) throw new RuntimeException (" Multiple user records found for $username " )
149
+ LdapPerson user = matches. first()
150
+ try (def conn = createConnection(host)) {
151
+ conn. bind(user. distinguishedname, password)
152
+ conn. unBind()
153
+ logDebug(' Authentication successful' , [username : username])
154
+ return true
155
+ } catch (LdapAuthenticationException ignored) {
156
+ logDebug(' Authentication failed, incorrect credentials' , [username : username])
157
+ return false
158
+ }
144
159
}
145
160
}
161
+ logDebug(' Authentication failed, no user found' , [username : username])
162
+ return false
146
163
}
147
- logDebug(' Authentication failed, no user found' , [username : username])
148
- return false
149
164
}
150
165
151
166
// ----------------------
@@ -192,7 +207,7 @@ class LdapService extends BaseService {
192
207
List<T> ret = cache. get(key)
193
208
if (ret != null ) return ret
194
209
195
- withDebug ([" Querying LDAP" , [host : host, filter : filter]]) {
210
+ withTrace ([" Querying LDAP" , [host : host, filter : filter]]) {
196
211
try (def conn = createConnection(host)) {
197
212
String baseDn = isPerson ? server. baseUserDn : server. baseGroupDn
198
213
String [] keys = objType. keys. toArray() as String []
0 commit comments