Wiki source code of User Authentication

Version 58.1 by Valdis Vitolins on 2012/10/11

Hide last authors
jmcettrick 1.18 1 XWiki supports several different authentication mechanisms for authenticating users:
Vincent Massol 1.1 2
Silvia Macovei 34.2 3 {{toc/}}
Silvia Macovei 34.1 4
Vincent Massol 1.1 5 The form authentication is the default mechanism.
6
Silvia Macovei 34.1 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}}
Vincent Massol 1.1 10
Silvia Macovei 34.2 11 = Form Authentication =
Vincent Massol 1.1 12
Sorin Burjan 38.2 13 Form authentication is the default way to get authenticated within a Wiki. It requires a user and a password.
Vincent Massol 1.1 14
Vincent Massol 46.1 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
Valdis Vitolins 58.1 27 = Social login extension =
28
29 See [[http://extensions.xwiki.org/xwiki/bin/view/Extension/Social+Login]]
30
Silvia Macovei 34.2 31 = LDAP Authentication =
Vincent Massol 1.1 32
Silvia Macovei 34.1 33 {{warning}}
34 New LDAP implementation since XWiki Platform 1.3M2, see [[previous LDAP authentication service documentation>>AuthenticationLdapOld]]
35 {{/warning}}
Vincent Massol 17.3 36
Silvia Macovei 34.2 37 == Generic LDAP configuration ==
Vincent Massol 1.1 38
Silvia Macovei 34.1 39 In order to enable the LDAP support you have to change the authentication method in //WEB-INF/xwiki.cfg// as follows:
40
Thomas Mortagne 54.1 41 {{code language="properties"}}
Thomas Mortagne 50.1 42 #-# LDAP authentication service
43 # xwiki.authentication.authclass=com.xpn.xwiki.user.impl.LDAP.XWikiLDAPAuthServiceImpl
Thomas Mortagne 7.1 44
Thomas Mortagne 50.1 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
Silvia Macovei 34.1 50 {{/code}}
Thomas Mortagne 18.1 51
Silvia Macovei 34.1 52 You can setup the LDAP configuration in the //xwiki.cfg// file by filling the following properties:
Vincent Massol 1.2 53
Thomas Mortagne 54.1 54 {{code language="properties"}}
Thomas Mortagne 36.1 55 #-# LDAP Server (Active Directory, eDirectory, OpenLDAP, etc.)
56 xwiki.authentication.ldap.server=127.0.0.1
Vincent Massol 1.1 57 xwiki.authentication.ldap.port=389
Thomas Mortagne 6.1 58
Thomas Mortagne 36.1 59 #-# LDAP login, empty = anonymous access, otherwise specify full dn
Thomas Mortagne 50.1 60 #-# {0} is replaced with the user name, {1} with the password
Vincent Massol 1.1 61 xwiki.authentication.ldap.bind_DN=cn={0},department=USER,department=INFORMATIK,department=1230,o=MP
62 xwiki.authentication.ldap.bind_pass={1}
Thomas Mortagne 6.1 63
Thomas Mortagne 50.1 64 #-# The Base DN used in LDAP searches
65 xwiki.authentication.ldap.base_DN=
Thomas Mortagne 12.1 66
Thomas Mortagne 50.1 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})
Thomas Mortagne 55.1 71 # xwiki.authentication.ldap.user_search_fmt=({0}={1})
Thomas Mortagne 50.1 72
73 #-# Only members of the following group will be verified in the LDAP
Thomas Mortagne 36.1 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
Thomas Mortagne 6.1 76
Thomas Mortagne 50.1 77 #-# [Since 1.5RC1, XWikiLDAPAuthServiceImpl]
78 #-# Only users not member of the following group can autheticate
Thomas Mortagne 36.1 79 # xwiki.authentication.ldap.exclude_group=cn=admin,ou=groups,o=MegaNova,c=US
Thomas Mortagne 22.1 80
Thomas Mortagne 50.1 81 #-# Specifies the LDAP attribute containing the identifier to be used as the XWiki name
82 #-# The default is cn
Thomas Mortagne 36.1 83 # xwiki.authentication.ldap.UID_attr=cn
Thomas Mortagne 6.1 84
Thomas Mortagne 50.1 85 #-# [Since 1.5M1, XWikiLDAPAuthServiceImpl]
Thomas Mortagne 36.1 86 #-# The potential LDAP groups classes. Separated by commas.
87 # xwiki.authentication.ldap.group_classes=group,groupOfNames,groupOfUniqueNames,dynamicGroup,dynamicGroupAux,groupWiseDistributionList
88
Thomas Mortagne 50.1 89 #-# [Since 1.5M1, XWikiLDAPAuthServiceImpl]
Thomas Mortagne 55.1 90 #-# The potential names of the LDAP groups fields containings the members. Separated by commas.
Thomas Mortagne 36.1 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
Thomas Mortagne 50.1 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
Thomas Mortagne 54.1 101 #-# The default is 0
102 xwiki.authentication.ldap.update_user=1
Thomas Mortagne 6.1 103
Thomas Mortagne 50.1 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),
Thomas Mortagne 55.1 109 #-# | character in the filter need to be escaped with backslash (\).
Thomas Mortagne 50.1 110 #-#
111 #-# Here is an example:
112 # xwiki.authentication.ldap.group_mapping=XWiki.XWikiAdminGroup=cn=AdminRole,ou=groups,o=domain,c=com|\
Thomas Mortagne 55.1 113 # XWiki.LDAPUsers=ou=groups,o=domain,c=com|\
114 # XWiki.Organisation=(cn=testers)
Thomas Mortagne 6.1 115
Thomas Mortagne 50.1 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
Thomas Mortagne 6.1 120
Thomas Mortagne 50.1 121 #-# [Since 1.3M2, XWikiLDAPAuthServiceImpl]
Thomas Mortagne 36.1 122 #-# - create : synchronize group membership only when the user is first created
123 #-# - always: synchronize on every login
Thomas Mortagne 50.1 124 #-# The default is always
Thomas Mortagne 36.1 125 # xwiki.authentication.ldap.mode_group_sync=always
Thomas Mortagne 6.1 126
Thomas Mortagne 50.1 127 #-# [Since 1.3M2, XWikiLDAPAuthServiceImpl]
Thomas Mortagne 54.1 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
Thomas Mortagne 6.1 132 xwiki.authentication.ldap.trylocal=1
Thomas Mortagne 8.1 133
Thomas Mortagne 50.1 134 #-# [Since 1.3M2, XWikiLDAPAuthServiceImpl]
Thomas Mortagne 36.1 135 #-# SSL connection to LDAP server
Thomas Mortagne 50.1 136 #-# - 0: normal
137 #-# - 1: SSL
138 #-# The default is 0
Thomas Mortagne 36.1 139 # xwiki.authentication.ldap.ssl=0
Thomas Mortagne 8.1 140
Thomas Mortagne 50.1 141 #-# [Since 1.3M2, XWikiLDAPAuthServiceImpl]
Thomas Mortagne 36.1 142 #-# The keystore file to use in SSL connection
143 # xwiki.authentication.ldap.ssl.keystore=
144
Thomas Mortagne 50.1 145 #-# [Since 1.5M1, XWikiLDAPAuthServiceImpl]
Thomas Mortagne 36.1 146 #-# The java secure provider used in SSL connection
Thomas Mortagne 50.1 147 #-# The default is com.sun.net.ssl.internal.ssl.Provider
Thomas Mortagne 36.1 148 # xwiki.authentication.ldap.ssl.secure_provider=com.sun.net.ssl.internal.ssl.Provider
Thomas Mortagne 50.1 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
Thomas Mortagne 55.1 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
Silvia Macovei 34.1 166 {{/code}}
Vincent Massol 3.1 167
Silvia Macovei 34.1 168 {{info}}
Thomas Mortagne 48.1 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##.
Silvia Macovei 34.1 170 {{/info}}
Thomas Mortagne 9.1 171
jmcettrick 1.19 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
Thomas Mortagne 31.1 174 Here are some LDAP client for checking your configuration:
jmcettrick 1.19 175
Thomas Mortagne 52.1 176 Java based and Open Source:
Thomas Mortagne 56.1 177
Silvia Macovei 34.1 178 * [[Apache Directory Studio>>http://directory.apache.org/studio/]]
Thomas Mortagne 51.1 179 * [[JXplorer>>http://jxplorer.org]]
jmcettrick 1.19 180
Thomas Mortagne 52.1 181 Windows only:
Thomas Mortagne 56.1 182
Thomas Mortagne 52.1 183 * [[Softerra LDAP Browser>>http://www.ldapbrowser.com/download.htm]]
184
Silvia Macovei 34.2 185 == Detailed use cases ==
Thomas Mortagne 12.1 186
Silvia Macovei 34.1 187 See [[LDAP configuration uses cases>>LDAPAuthenticationUseCases]] for some detailed use cases.
Thomas Mortagne 25.1 188
Silvia Macovei 34.2 189 == Enable LDAP debug log ==
Silvia Macovei 34.1 190
Thomas Mortagne 42.1 191 See [[AdminGuide.Logging]].
Silvia Macovei 34.1 192
Thomas Mortagne 45.1 193 The specific packages to track for LDAP are ##com.xpn.xwiki.plugin.ldap## and ##com.xpn.xwiki.user.impl.LDAP##.
Thomas Mortagne 27.1 194
AdrianFita 49.1 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
Thomas Mortagne 42.1 203 Before 3.1, add the following to the log4j configuration file:
204
Thomas Mortagne 44.1 205 {{code}}
206 log4j.logger.com.xpn.xwiki.plugin.ldap=trace
207 log4j.logger.com.xpn.xwiki.user.impl.LDAP=trace
208 {{/code}}
209
Silvia Macovei 34.2 210 = eXo Authentication =
hexC0DE 30.1 211
Silvia Macovei 34.1 212 The eXo authentication is used automatically by adding/editing the //xwiki.exo=1// property in //WEB-INF/xwiki.cfg//.
Vincent Massol 1.1 213
Silvia Macovei 34.2 214 = Custom Authentication =
Vincent Massol 1.1 215
216 This allows plugging to any existing authentication mechanism such as SiteMinder, etc. To configure a custom authentication do the following:
217
Sergiu Dumitriu 47.2 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.
Silvia Macovei 34.1 219 1. Edit the //WEB-INF/xwiki.cfg// file and add a //xwiki.authentication.authclass// property pointing to your class. For example:
220
Thomas Mortagne 56.1 221 {{code language="properties"}}
Vincent Massol 1.1 222 xwiki.authentication.authclass = com.acme.MyCustomAuthenticationService
Silvia Macovei 34.1 223 {{/code}}
Vincent Massol 1.1 224
Thomas Mortagne 56.1 225 You can find various authenticators examples on https://github.com/xwiki-contrib/sandbox/blob/master/authenticators/.
226
Silvia Macovei 34.1 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/]].
Vincent Massol 27.2 228
Sergiu Dumitriu 47.2 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:
Silvia Macovei 34.1 230
Thomas Mortagne 57.1 231 {{code language="properties"}}
rssh 5.1 232 xwiki.authentication.rightsclass = com.acme.MyCustomRightsService
Silvia Macovei 34.1 233 {{/code}}
rssh 4.1 234
Sergiu Dumitriu 47.2 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}}:
rssh 4.1 236
Thomas Mortagne 57.1 237 {{code language="properties"}}
Vincent Massol 27.3 238 xwiki.authentication.groupclass = com.acme.MyCustomGroupService
Silvia Macovei 34.1 239 {{/code}}
rssh 4.1 240
Silvia Macovei 34.2 241 == Custom Authentication using a Groovy script in a wiki page ==
Vincent Massol 28.1 242
243 Start by specifying you want to use the Groovy Authenticator:
244
Thomas Mortagne 57.1 245 {{code language="properties"}}
Vincent Massol 28.1 246 xwiki.authentication.authclass = com.xpn.xwiki.user.impl.xwiki.GroovyAuthServiceImpl
Silvia Macovei 34.1 247 {{/code}}
Vincent Massol 28.1 248
249 Then add another configuration parameter to specify in which wiki page the authenticator is:
250
Thomas Mortagne 57.1 251 {{code language="properties"}}
Vincent Massol 28.1 252 xwiki.authentication.groovy.pagename = MySpace.MyPage
Silvia Macovei 34.1 253 {{/code}}
Vincent Massol 28.1 254
255 Then in a wiki page put some Groovy code that returns a XWikiAuthService object.
256
Thomas Mortagne 37.1 257 = Authentication parameters =
ShawnLauzon 1.14 258
259 You can set each of these parameters by setting:
260
Silvia Macovei 34.1 261 {{code}}
ShawnLauzon 1.14 262 xwiki.authentication.~~param_name~~=~~param_value~~
Silvia Macovei 34.1 263 {{/code}}
ShawnLauzon 1.14 264
Silvia Macovei 34.1 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
Caleb James DeLisle 35.1 277 |loginsubmitpage|Yes|String|/loginsubmit/ XWiki/XWikiLogin|The URL where the username and password are posted to when logging in.
Silvia Macovei 34.1 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
Caleb James DeLisle 35.1 281 |unauthorized_code|Yes|Number|401|The HTTP status code to return when the login has failed.
Silvia Macovei 34.1 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.
ShawnLauzon 1.16 283
Silvia Macovei 34.1 284 1. Only required if protection = encryption or all (default)
285 1. Only required if protection = validation or all (default)
ShawnLauzon 1.17 286
Silvia Macovei 34.2 287 = Kerberos SSO Authentication =
JasperSiepkes 21.1 288
Silvia Macovei 34.1 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
JasperSiepkes 20.1 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.
jmcettrick 1.18 294
JasperSiepkes 20.1 295 First of all you need to create a principal and keytab for the webserver:
Silvia Macovei 34.1 296
297 {{code}}
JasperSiepkes 20.1 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
Silvia Macovei 34.1 302 {{/code}}
Thomas Mortagne 10.1 303
JasperSiepkes 20.1 304 Make sure the keytab has the right permissions and ownership:
Silvia Macovei 34.1 305
306 {{code}}
JasperSiepkes 20.1 307 chown www-data:www-data /etc/apache2/ssl/wiki.keytab
308 chmod 400 /etc/apache2/ssl/wiki.keytab
Silvia Macovei 34.1 309 {{/code}}
Thomas Mortagne 11.1 310
JasperSiepkes 20.1 311 Install mod_auth_kerb in your linux installation. On Debian or Ubuntu this would be achieved by running:
Silvia Macovei 34.1 312
313 {{code}}
JasperSiepkes 20.1 314 aptitude install libapache2-mod-auth-kerb
Silvia Macovei 34.1 315 {{/code}}
316
JasperSiepkes 20.1 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:
Silvia Macovei 34.1 320
321 {{code}}
JasperSiepkes 20.1 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>
Silvia Macovei 34.1 332 {{/code}}
JasperSiepkes 20.1 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):
Silvia Macovei 34.1 335
336 {{code}}
JasperSiepkes 20.1 337 <Connector port="8009" address="127.0.0.1" enableLookups="false" tomcatAuthentication="false" redirectPort="8443" protocol="AJP/1.3" />
Silvia Macovei 34.1 338 {{/code}}
JasperSiepkes 20.1 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.
Silvia Macovei 34.1 343
344 {{code}}
JasperSiepkes 20.1 345 xwiki.authentication.authclass=com.xpn.xwiki.user.impl.xwiki.AppServerTrustedKerberosAuthServiceImpl
Silvia Macovei 34.1 346 {{/code}}
JasperSiepkes 20.1 347
Silvia Macovei 34.1 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.
JasperSiepkes 20.1 349
Silvia Macovei 34.1 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.
jek 29.1 351
Silvia Macovei 34.2 352 The configuration of ldap:
jek 29.1 353
Silvia Macovei 34.1 354 {{code}}
jek 29.1 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,...........
Silvia Macovei 34.1 371 {{/code}}
jek 29.1 372
373 The java code
Silvia Macovei 34.1 374
375 {{code}}
jek 29.1 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 }
Silvia Macovei 34.1 498 {{/code}}

Get Connected