Wiki source code of User Authentication
Version 60.1 by Valdis Vitolins on 2012/10/11
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | XWiki supports several different authentication mechanisms for authenticating users: | ||
2 | |||
3 | {{toc/}} | ||
4 | |||
5 | The form authentication is the default mechanism. | ||
6 | |||
7 | {{info}} | ||
8 | Note that currently XWiki allows only one method of authentication to be enabled at a time. This will probably be improved in the future. | ||
9 | {{/info}} | ||
10 | |||
11 | = Form Authentication = | ||
12 | |||
13 | Form authentication is the default way to get authenticated within a Wiki. It requires a user and a password. | ||
14 | |||
15 | = Basic Authentication = | ||
16 | |||
17 | XWiki supports [basic access authentication>http://en.wikipedia.org/wiki/Basic_access_authentication], a method designed to allow a Web browser, or other client program, to provide credentials - in the form of a user name and password - when making a request. You can get authenticated against an XWiki server with the basic authentication protocol using the following URL scheme: | ||
18 | |||
19 | {{code}} | ||
20 | http://username:password@mywiki.xwiki.com/xwiki/bin/view/Main/WebHome?basicauth=1 | ||
21 | {{/code}} | ||
22 | |||
23 | {{warning}} | ||
24 | Be careful that if you use the ##HTTP## protocol your password will be sent in clear over the network and is thus very unsafe. When using Basic Authentication you should make sure your wiki is configured to use ##HTTPS##. | ||
25 | {{/warning}} | ||
26 | |||
27 | = Social login Xwiki Extension = | ||
28 | |||
29 | See [[http://extensions.xwiki.org/xwiki/bin/view/Extension/Social+Login]] | ||
30 | |||
31 | = LDAP Authentication = | ||
32 | |||
33 | {{warning}} | ||
34 | New LDAP implementation since XWiki Platform 1.3M2, see [[previous LDAP authentication service documentation>>AuthenticationLdapOld]] | ||
35 | {{/warning}} | ||
36 | |||
37 | == Generic LDAP configuration == | ||
38 | |||
39 | In order to enable the LDAP support you have to change the authentication method in //WEB-INF/xwiki.cfg// as follows: | ||
40 | |||
41 | {{code language="properties"}} | ||
42 | #-# LDAP authentication service | ||
43 | # xwiki.authentication.authclass=com.xpn.xwiki.user.impl.LDAP.XWikiLDAPAuthServiceImpl | ||
44 | |||
45 | #-# Turn LDAP authentication on - otherwise only XWiki authentication | ||
46 | #-# - 0: disable | ||
47 | #-# - 1: enable | ||
48 | #-# The default is 1 | ||
49 | # xwiki.authentication.ldap=1 | ||
50 | {{/code}} | ||
51 | |||
52 | You can setup the LDAP configuration in the //xwiki.cfg// file by filling the following properties: | ||
53 | |||
54 | {{code language="properties"}} | ||
55 | #-# LDAP Server (Active Directory, eDirectory, OpenLDAP, etc.) | ||
56 | xwiki.authentication.ldap.server=127.0.0.1 | ||
57 | xwiki.authentication.ldap.port=389 | ||
58 | |||
59 | #-# LDAP login, empty = anonymous access, otherwise specify full dn | ||
60 | #-# {0} is replaced with the user name, {1} with the password | ||
61 | xwiki.authentication.ldap.bind_DN=cn={0},department=USER,department=INFORMATIK,department=1230,o=MP | ||
62 | xwiki.authentication.ldap.bind_pass={1} | ||
63 | |||
64 | #-# The Base DN used in LDAP searches | ||
65 | xwiki.authentication.ldap.base_DN= | ||
66 | |||
67 | #-# LDAP query to search the user in the LDAP database (in case a static admin user is provided in | ||
68 | #-# xwiki.authentication.ldap.bind_DN) | ||
69 | #-# {0} is replaced with the user uid field name and {1} with the user name | ||
70 | #-# The default is ({0}={1}) | ||
71 | # xwiki.authentication.ldap.user_search_fmt=({0}={1}) | ||
72 | |||
73 | #-# Only members of the following group will be verified in the LDAP | ||
74 | #-# otherwise only users that are found after searching starting from the base_DN | ||
75 | # xwiki.authentication.ldap.user_group=cn=developers,ou=groups,o=MegaNova,c=US | ||
76 | |||
77 | #-# [Since 1.5RC1, XWikiLDAPAuthServiceImpl] | ||
78 | #-# Only users not member of the following group can autheticate | ||
79 | # xwiki.authentication.ldap.exclude_group=cn=admin,ou=groups,o=MegaNova,c=US | ||
80 | |||
81 | #-# Specifies the LDAP attribute containing the identifier to be used as the XWiki name | ||
82 | #-# The default is cn | ||
83 | # xwiki.authentication.ldap.UID_attr=cn | ||
84 | |||
85 | #-# [Since 1.5M1, XWikiLDAPAuthServiceImpl] | ||
86 | #-# The potential LDAP groups classes. Separated by commas. | ||
87 | # xwiki.authentication.ldap.group_classes=group,groupOfNames,groupOfUniqueNames,dynamicGroup,dynamicGroupAux,groupWiseDistributionList | ||
88 | |||
89 | #-# [Since 1.5M1, XWikiLDAPAuthServiceImpl] | ||
90 | #-# The potential names of the LDAP groups fields containings the members. Separated by commas. | ||
91 | # xwiki.authentication.ldap.group_memberfields=member,uniqueMember | ||
92 | |||
93 | #-# retrieve the following fields from LDAP and store them in the XWiki user object (xwiki-attribute=ldap-attribute) | ||
94 | xwiki.authentication.ldap.fields_mapping=last_name=sn,first_name=givenName,email=mail | ||
95 | |||
96 | #-# [Since 1.3M2, XWikiLDAPAuthServiceImpl] | ||
97 | #-# On every login update the mapped attributes from LDAP to XWiki otherwise this happens only once when the XWiki | ||
98 | #-# account is created. | ||
99 | #-# - 0: only when creating user | ||
100 | #-# - 1: at each authentication | ||
101 | #-# The default is 0 | ||
102 | xwiki.authentication.ldap.update_user=1 | ||
103 | |||
104 | #-# [Since 1.3M2, XWikiLDAPAuthServiceImpl] | ||
105 | #-# Maps XWiki groups to LDAP groups, separator is "|". The following kind of groups are supported: | ||
106 | #-# * LDAP static groups (users/subgroups are listed statically in the group object) | ||
107 | #-# * [Since 3.3M1] LDAP organization units (users/subgroups are sub object of the provided organization unit) | ||
108 | #-# * [Since 3.3M1] LDAP filter (users/groups are object found in a search with the provided filter), | ||
109 | #-# | character in the filter need to be escaped with backslash (\). | ||
110 | #-# | ||
111 | #-# Here is an example: | ||
112 | # xwiki.authentication.ldap.group_mapping=XWiki.XWikiAdminGroup=cn=AdminRole,ou=groups,o=domain,c=com|\ | ||
113 | # XWiki.LDAPUsers=ou=groups,o=domain,c=com|\ | ||
114 | # XWiki.Organisation=(cn=testers) | ||
115 | |||
116 | #-# [Since 1.3M2, XWikiLDAPAuthServiceImpl] | ||
117 | #-# Time in s after which the list of members in a group is refreshed from LDAP | ||
118 | #-# The default is 21600 (6 hours) | ||
119 | # xwiki.authentication.ldap.groupcache_expiration=21600 | ||
120 | |||
121 | #-# [Since 1.3M2, XWikiLDAPAuthServiceImpl] | ||
122 | #-# - create : synchronize group membership only when the user is first created | ||
123 | #-# - always: synchronize on every login | ||
124 | #-# The default is always | ||
125 | # xwiki.authentication.ldap.mode_group_sync=always | ||
126 | |||
127 | #-# [Since 1.3M2, XWikiLDAPAuthServiceImpl] | ||
128 | #-# If ldap authentication fails for any reason, try XWiki DB authentication with the same credentials | ||
129 | #-# - 0: disable | ||
130 | #-# - 1: enable | ||
131 | #-# The default is 0 | ||
132 | xwiki.authentication.ldap.trylocal=1 | ||
133 | |||
134 | #-# [Since 1.3M2, XWikiLDAPAuthServiceImpl] | ||
135 | #-# SSL connection to LDAP server | ||
136 | #-# - 0: normal | ||
137 | #-# - 1: SSL | ||
138 | #-# The default is 0 | ||
139 | # xwiki.authentication.ldap.ssl=0 | ||
140 | |||
141 | #-# [Since 1.3M2, XWikiLDAPAuthServiceImpl] | ||
142 | #-# The keystore file to use in SSL connection | ||
143 | # xwiki.authentication.ldap.ssl.keystore= | ||
144 | |||
145 | #-# [Since 1.5M1, XWikiLDAPAuthServiceImpl] | ||
146 | #-# The java secure provider used in SSL connection | ||
147 | #-# The default is com.sun.net.ssl.internal.ssl.Provider | ||
148 | # xwiki.authentication.ldap.ssl.secure_provider=com.sun.net.ssl.internal.ssl.Provider | ||
149 | |||
150 | #-# Bypass standard LDAP bind validation by doing a direct password comparison. | ||
151 | #-# If you don't know what you do, don't use that. It's covering very rare and bad use cases. | ||
152 | #-# - 0: disable | ||
153 | #-# - 1: enable | ||
154 | #-# The default is 0 | ||
155 | # xwiki.authentication.ldap.validate_password=0 | ||
156 | |||
157 | #-# [Since 1.5M1, XWikiLDAPAuthServiceImpl] | ||
158 | #-# Specifies the LDAP attribute containing the password to be used "when xwiki.authentication.ldap.validate_password" | ||
159 | #-# is set to 1 | ||
160 | # xwiki.authentication.ldap.password_field=userPassword | ||
161 | |||
162 | #-# [Since 4.3M1, XWikiLDAPAuthServiceImpl] | ||
163 | #-# The maximum number of milliseconds the client waits for any operation under these constraints to complete. | ||
164 | #-# The default is 1000 | ||
165 | # xwiki.authentication.ldap.timeout=1000 | ||
166 | {{/code}} | ||
167 | |||
168 | {{info}} | ||
169 | You can also setup the LDAP configuration in XWiki.XWikiPreferences page by going to the object editor. Simply replace "xwiki.authentication.ldap." by "ldap_". For example ##xwiki.authentication.ldap.base_DN## becomes ##ldap_base_DN##. | ||
170 | {{/info}} | ||
171 | |||
172 | For testing purposes, you may wish to omit the "ldap.fields_mapping" field, to test the authentication first, and then add it later to get the mappings right. | ||
173 | |||
174 | Here are some LDAP client for checking your configuration: | ||
175 | |||
176 | Java based and Open Source: | ||
177 | |||
178 | * [[Apache Directory Studio>>http://directory.apache.org/studio/]] | ||
179 | * [[JXplorer>>http://jxplorer.org]] | ||
180 | |||
181 | Windows only: | ||
182 | |||
183 | * [[Softerra LDAP Browser>>http://www.ldapbrowser.com/download.htm]] | ||
184 | |||
185 | == Detailed use cases == | ||
186 | |||
187 | See [[LDAP configuration uses cases>>LDAPAuthenticationUseCases]] for some detailed use cases. | ||
188 | |||
189 | == Enable LDAP debug log == | ||
190 | |||
191 | See [[AdminGuide.Logging]]. | ||
192 | |||
193 | The specific packages to track for LDAP are ##com.xpn.xwiki.plugin.ldap## and ##com.xpn.xwiki.user.impl.LDAP##. | ||
194 | |||
195 | In XWiki 3.4 you need to add the following in ##WEB-INF/classes/logback.xml##: | ||
196 | |||
197 | {{code}} | ||
198 | <!-- LDAP debugging --> | ||
199 | <logger name="com.xpn.xwiki.plugin.ldap" level="trace"/> | ||
200 | <logger name="com.xpn.xwiki.user.impl.LDAP" level="trace"/> | ||
201 | {{/code}} | ||
202 | |||
203 | Before 3.1, add the following to the log4j configuration file: | ||
204 | |||
205 | {{code}} | ||
206 | log4j.logger.com.xpn.xwiki.plugin.ldap=trace | ||
207 | log4j.logger.com.xpn.xwiki.user.impl.LDAP=trace | ||
208 | {{/code}} | ||
209 | |||
210 | = eXo Authentication = | ||
211 | |||
212 | The eXo authentication is used automatically by adding/editing the //xwiki.exo=1// property in //WEB-INF/xwiki.cfg//. | ||
213 | |||
214 | = Custom Authentication = | ||
215 | |||
216 | This allows plugging to any existing authentication mechanism such as SiteMinder, etc. To configure a custom authentication do the following: | ||
217 | |||
218 | 1. Implement the {{scm path="xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/user/api/XWikiAuthService.java"}}XWikiAuthService{{/scm}} interface. | ||
219 | 1. Edit the //WEB-INF/xwiki.cfg// file and add a //xwiki.authentication.authclass// property pointing to your class. For example: | ||
220 | |||
221 | {{code language="properties"}} | ||
222 | xwiki.authentication.authclass = com.acme.MyCustomAuthenticationService | ||
223 | {{/code}} | ||
224 | |||
225 | You can find various authenticators examples on https://github.com/xwiki-contrib/sandbox/blob/master/authenticators/. | ||
226 | |||
227 | Here's a [[tutorial on implementing a custom authentication class for authenticating against Oracle's SSO>>http://bodez.wordpress.com/2008/10/15/xwiki-user-authentication-with-oracle-sso/]]. | ||
228 | |||
229 | Note, that you also can implement own right management service by implementing {{scm path="xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/user/api/XWikiRightService.java"}}XWikiRightService{{/scm}} interface: | ||
230 | |||
231 | {{code language="properties"}} | ||
232 | xwiki.authentication.rightsclass = com.acme.MyCustomRightsService | ||
233 | {{/code}} | ||
234 | |||
235 | and Group Service by implementing {{scm path="xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/user/api/XWikiGroupService.java"}}XWikiGroupService{{/scm}}: | ||
236 | |||
237 | {{code language="properties"}} | ||
238 | xwiki.authentication.groupclass = com.acme.MyCustomGroupService | ||
239 | {{/code}} | ||
240 | |||
241 | == Custom Authentication using a Groovy script in a wiki page == | ||
242 | |||
243 | Start by specifying you want to use the Groovy Authenticator: | ||
244 | |||
245 | {{code language="properties"}} | ||
246 | xwiki.authentication.authclass = com.xpn.xwiki.user.impl.xwiki.GroovyAuthServiceImpl | ||
247 | {{/code}} | ||
248 | |||
249 | Then add another configuration parameter to specify in which wiki page the authenticator is: | ||
250 | |||
251 | {{code language="properties"}} | ||
252 | xwiki.authentication.groovy.pagename = MySpace.MyPage | ||
253 | {{/code}} | ||
254 | |||
255 | Then in a wiki page put some Groovy code that returns a XWikiAuthService object. | ||
256 | |||
257 | = Authentication parameters = | ||
258 | |||
259 | You can set each of these parameters by setting: | ||
260 | |||
261 | {{code}} | ||
262 | xwiki.authentication.~~param_name~~=~~param_value~~ | ||
263 | {{/code}} | ||
264 | |||
265 | |=Name|=Optional|=Allowed values|=Default value|=Description | ||
266 | |encryptionKey|No(1)|?|n/a|Set the Encryption Key used to create a secret key, the secret key is passed to the Cipher object to be used during encryption and decryption of cookie values. | ||
267 | |validationKey|No(2)|?|n/a|Set the Validation Key used to generate hash value; the hash value is stored with the cookie and used to verify that the cookie has not been tampered with. | ||
268 | |cookiedomains|Yes|String|Server host name|Which host(s) should your cookies be sent to; use only if you want to share cookies across domains, otherwise should be commented out | ||
269 | |cookielife|Yes|Number|14|Number of days cookies take to expire | ||
270 | |cookiepath|Yes|String|/|The webapp path that XWiki cookies should be sent to; if you have anything else running on your web server, this should be set to ///xwiki// | ||
271 | |default_page|Yes|String|/bin/view/ Main/WebHome|Page to redirect to if xredirect parameter is not set | ||
272 | |encryptionalgorithm|Yes|?|?|Set the Encryption Algorithm used to encrypt and decrypt cookies | ||
273 | |encryptionmode|Yes|?|?|Set the Encryption Mode used to encrypt and decrypt cookies | ||
274 | |encryptionpadding|Yes|?|?|Set the Encryption Padding used to encrypt and decrypt cookies | ||
275 | |errorpage|Yes|String|/bin/loginerror/ XWiki/XWikiLogin|Page to redirect to if there is an error logging in | ||
276 | |loginpage|Yes|String|/bin/login/ XWiki/XWikiLogin|Page to redirect to when not logged in | ||
277 | |loginsubmitpage|Yes|String|/loginsubmit/ XWiki/XWikiLogin|The URL where the username and password are posted to when logging in. | ||
278 | |logoutpage|Yes|String|/bin/logout/ XWiki/XWikiLogout|Page to redirect to after logged out | ||
279 | |realmname|Yes|String|XWiki|Sets the realm name | ||
280 | |protection|Yes|all, validation, encryption, none|all|Protection level for the "remember me" cookie functionality | ||
281 | |unauthorized_code|Yes|Number|401|The HTTP status code to return when the login has failed. | ||
282 | |useip|Yes|true / false|true|Specify to use the IP address when encrypting the cookie data; if IP address changes will need to re-login. | ||
283 | |||
284 | 1. Only required if protection = encryption or all (default) | ||
285 | 1. Only required if protection = validation or all (default) | ||
286 | |||
287 | = Kerberos SSO Authentication = | ||
288 | |||
289 | {{warning}} | ||
290 | This implementation of SSO is currently under review see: http://jira.xwiki.org/jira/browse/XWIKI-2496 . The class which is described in this segment of documentation, AppServerTrustedKerberosAuthServiceImpl, is not part of the default XWiki distribution! | ||
291 | {{/warning}} | ||
292 | |||
293 | The following is an example of mod_auth_kerb for Apache being used to easily implement Xwiki authentication of users via by HTTP Negotiate on a linux server. This example assumes you already have a working Apache2 HTTPD and Apache Tomcat setup with mod_jk. | ||
294 | |||
295 | First of all you need to create a principal and keytab for the webserver: | ||
296 | |||
297 | {{code}} | ||
298 | # kadmin | ||
299 | kadmin> addprinc -randkey HTTP/wiki.example.com | ||
300 | kadmin> ktadd -k /etc/apache2/ssl/wiki.keytab HTTP/wiki.example.com | ||
301 | kadmin> quit | ||
302 | {{/code}} | ||
303 | |||
304 | Make sure the keytab has the right permissions and ownership: | ||
305 | |||
306 | {{code}} | ||
307 | chown www-data:www-data /etc/apache2/ssl/wiki.keytab | ||
308 | chmod 400 /etc/apache2/ssl/wiki.keytab | ||
309 | {{/code}} | ||
310 | |||
311 | Install mod_auth_kerb in your linux installation. On Debian or Ubuntu this would be achieved by running: | ||
312 | |||
313 | {{code}} | ||
314 | aptitude install libapache2-mod-auth-kerb | ||
315 | {{/code}} | ||
316 | |||
317 | Of course the installation procedure varies per Linux distribution. | ||
318 | |||
319 | If your xwiki installation is mounted in Apache HTTPD under /xwiki, add the following to the virtual host configuration: | ||
320 | |||
321 | {{code}} | ||
322 | <Location /xwiki/> | ||
323 | AuthType Kerberos | ||
324 | AuthName "Kerberos Login" | ||
325 | KrbAuthRealms EXAMPLE.COM | ||
326 | Krb5Keytab "/etc/apache2/ssl/wiki.keytab" | ||
327 | KrbMethodK5Passwd off | ||
328 | KrbMethodNegotiate on | ||
329 | KrbSaveCredentials on | ||
330 | require valid-user | ||
331 | </Location> | ||
332 | {{/code}} | ||
333 | |||
334 | Make sure Apache Tomcat uses the authentication performed by Apache HTTPD with the "tomcatAuthentication" property in the connector description (which is in the server.xml file of Apache Tomcat): | ||
335 | |||
336 | {{code}} | ||
337 | <Connector port="8009" address="127.0.0.1" enableLookups="false" tomcatAuthentication="false" redirectPort="8443" protocol="AJP/1.3" /> | ||
338 | {{/code}} | ||
339 | |||
340 | Place the authkerb.jar jar in the WEB-INF/lib directory of Xwiki in Apache Tomcat. | ||
341 | |||
342 | Have Xwiki use the authentication module by changing the "xwiki.authentication.authclass" property in WEB-INF/lib/xwiki.cfg file. | ||
343 | |||
344 | {{code}} | ||
345 | xwiki.authentication.authclass=com.xpn.xwiki.user.impl.xwiki.AppServerTrustedKerberosAuthServiceImpl | ||
346 | {{/code}} | ||
347 | |||
348 | If you use Firefox, do not forget to whitelist the xwiki URL for HTTP Negotiate in about:config with the "network.negotiate-auth.trusted-uris" property. possible values for this propperty include (without the quotes): "https:~/~/" for all secured connections or "example.com" for all example.com subdomains. | ||
349 | |||
350 | 2 JBoss SPNEGO (Kerberos in combination with LDAP) I changed the code of the XWikiLDAPAuthServiceImpl to be able to detect the sso user. The authenication already happend by using the SPNEGO module (JAAS). After that I'm using the ldap synchronisation feature to make sure that the user is up to date. The combination leads to an automatic login in the xwiki and the user rights are controlled in the Active Directory server. I hope you can adopt this code or that you can use it for your own projects. | ||
351 | |||
352 | The configuration of ldap: | ||
353 | |||
354 | {{code}} | ||
355 | xwiki.authentication.authclass=com.wiki.sso.SSOLdapAuthenicationImpl | ||
356 | xwiki.authentication.ldap=1 | ||
357 | xwiki.authentication.ldap.server=<ad-server> | ||
358 | xwiki.authentication.ldap.port=389 | ||
359 | xwiki.authentication.ldap.base_DN=<OU=Users,...............> | ||
360 | #use a fixed user to attach to the ldap database, | ||
361 | #the password is not provided with the SSOLdapAuthenicationImpl | ||
362 | xwiki.authentication.ldap.bind_DN=<domain>\\<user> | ||
363 | xwiki.authentication.ldap.bind_pass=<password> | ||
364 | #Microsoft AD configuration | ||
365 | xwiki.authentication.ldap.UID_attr=sAMAccountName | ||
366 | xwiki.authentication.ldap.fields_mapping=name=sAMAccountName,last_name=sn,first_name=givenName,fullname=displayName,mail=mail,ldap_dn=dn | ||
367 | xwiki.authentication.ldap.group_memberfields=member,uniqueMember | ||
368 | #LDAP group mapping | ||
369 | xwiki.authentication.ldap.group_mapping=XWiki.XWikiAdminGroup=CN=WIKI_Admin,............|\ | ||
370 | XWiki.XWikiAllGroup=CN=WIKI_User,........... | ||
371 | {{/code}} | ||
372 | |||
373 | The java code | ||
374 | |||
375 | {{code}} | ||
376 | package com.wiki.sso; | ||
377 | |||
378 | |||
379 | import org.apache.commons.logging.Log; | ||
380 | import org.apache.commons.logging.LogFactory; | ||
381 | |||
382 | import com.xpn.xwiki.XWikiContext; | ||
383 | import com.xpn.xwiki.XWikiException; | ||
384 | import com.xpn.xwiki.user.api.XWikiUser; | ||
385 | import com.xpn.xwiki.user.impl.LDAP.XWikiLDAPAuthServiceImpl; | ||
386 | |||
387 | import java.security.Principal; | ||
388 | |||
389 | public class SSOLdapAuthenicationImpl extends XWikiLDAPAuthServiceImpl { | ||
390 | /** | ||
391 | * Logging tool. | ||
392 | */ | ||
393 | private static final Log LOG = LogFactory.getLog(SSOLdapAuthenicationImpl.class); | ||
394 | |||
395 | |||
396 | public XWikiUser checkAuth(XWikiContext context) throws XWikiException { | ||
397 | String user = getRemoteUser(context); | ||
398 | if ((user != null) || !user.equals("")) { | ||
399 | if (LOG.isInfoEnabled()) | ||
400 | LOG.info("Launching create user for " + user); | ||
401 | if ( authenticate(user, context) != null ) { | ||
402 | if (LOG.isInfoEnabled()) | ||
403 | LOG.info("Create user done for " + user); | ||
404 | user = "XWiki." + user; | ||
405 | context.setUser(user); | ||
406 | System.out.println("User is set to:" + user); | ||
407 | return new XWikiUser(user); | ||
408 | } else { | ||
409 | LOG.error( "User " + user + " can't be authenticated against ldap" ); | ||
410 | } | ||
411 | } | ||
412 | return super.checkAuth(context); | ||
413 | } | ||
414 | |||
415 | /** | ||
416 | * We cannot authenticate locally since we need to trust the app server for | ||
417 | * authentication | ||
418 | * | ||
419 | * @param username | ||
420 | * @param password | ||
421 | * @param context | ||
422 | * @return | ||
423 | * @throws XWikiException | ||
424 | */ | ||
425 | public XWikiUser checkAuth(String username, String password, | ||
426 | String rememberme, XWikiContext context) throws XWikiException { | ||
427 | String user = getRemoteUser(context); | ||
428 | if ((user == null) || user.equals("")) { | ||
429 | return super.checkAuth(username, password, rememberme, context); | ||
430 | } | ||
431 | return checkAuth(context); | ||
432 | } | ||
433 | |||
434 | private String getRemoteUser(XWikiContext context) { | ||
435 | String userName = context.getRequest().getHttpServletRequest() | ||
436 | .getRemoteUser(); | ||
437 | if (userName != null) { | ||
438 | // only take the front of the username@domain | ||
439 | String[] elements = userName.split("@", 2); | ||
440 | userName = elements[0]; | ||
441 | } | ||
442 | return userName; | ||
443 | } | ||
444 | |||
445 | public Principal authenticate(String login, XWikiContext context) throws XWikiException | ||
446 | { | ||
447 | if (LOG.isTraceEnabled()) { | ||
448 | LOG.trace("Starting LDAP authentication"); | ||
449 | } | ||
450 | |||
451 | /* | ||
452 | * TODO: Put the next 4 following "if" in common with XWikiAuthService to ensure coherence This method was | ||
453 | * returning null on failure so I preserved that behaviour, while adding the exact error messages to the context | ||
454 | * given as argument. However, the right way to do this would probably be to throw XWikiException-s. | ||
455 | */ | ||
456 | |||
457 | if (login == null) { | ||
458 | // If we can't find the username field then we are probably on the login screen | ||
459 | |||
460 | if (LOG.isDebugEnabled()) { | ||
461 | LOG.debug("The provided user is null." | ||
462 | + " We don't try to authenticate, it probably means the user is in non logged mode."); | ||
463 | } | ||
464 | |||
465 | return null; | ||
466 | } | ||
467 | |||
468 | // Check for empty usernames | ||
469 | if (login.equals("")) { | ||
470 | context.put("message", "nousername"); | ||
471 | |||
472 | if (LOG.isDebugEnabled()) { | ||
473 | LOG.debug("LDAP authentication failed: login empty"); | ||
474 | } | ||
475 | |||
476 | return null; | ||
477 | } | ||
478 | |||
479 | // If we have the context then we are using direct mode | ||
480 | // then we should specify the database | ||
481 | // This is needed for virtual mode to work | ||
482 | Principal principal = null; | ||
483 | |||
484 | // Try authentication against ldap | ||
485 | principal = ldapAuthenticate(login, "", context); | ||
486 | |||
487 | if (LOG.isDebugEnabled()) { | ||
488 | if (principal != null) { | ||
489 | LOG.debug("LDAP authentication succeed with principal [" + principal.getName() + "]"); | ||
490 | } else { | ||
491 | LOG.debug("LDAP authentication failed for user [" + login + "]"); | ||
492 | } | ||
493 | } | ||
494 | |||
495 | return principal; | ||
496 | } | ||
497 | } | ||
498 | {{/code}} |