Wiki source code of User Authentication

Version 122.1 by Thomas Mortagne on 2023/10/10

Hide last authors
Ecaterina Moraru (Valica) 94.4 1 {{box cssClass="floatinginfobox" title="**Contents**"}}
Silvia Macovei 34.2 2 {{toc/}}
Ecaterina Moraru (Valica) 94.4 3 {{/box}}
Silvia Macovei 34.1 4
Ecaterina Moraru (Valica) 94.4 5 XWiki supports several different authentication mechanisms for authenticating users.
6
Silvia Macovei 34.1 7 {{info}}
Manuel Smeria 64.2 8 XWiki currently allows only one method of authentication to be enabled at a time. This will probably be improved in the future.
Silvia Macovei 34.1 9 {{/info}}
Vincent Massol 1.1 10
Thomas Mortagne 120.1 11 Also note that XWiki requires cookies to be enabled in order to track your session and to keep you logged in between pages.
Vincent Massol 72.1 12
Thomas Mortagne 117.1 13 = Choose the authenticator
14
Thomas Mortagne 120.1 15 Historically, the authenticator to use in XWiki is controlled by the property ##xwiki.authentication.authclass## in the ##xwiki.properties## file.
Thomas Mortagne 119.1 16
Thomas Mortagne 117.1 17 {{version since="15.3"}}
18 For authenticators which supports it, it's possible to choose the authenticator to use in the wiki administration.
19
20 {{image reference="authserviceadmin.png"/}}
21 {{/version}}
22
gcoquard 112.1 23 = Authentication Types =
Vincent Massol 1.1 24
gcoquard 112.1 25 == Form Authentication ==
26
Sorin Burjan 38.2 27 Form authentication is the default way to get authenticated within a Wiki. It requires a user and a password.
Vincent Massol 1.1 28
gcoquard 112.1 29 == Basic Authentication ==
Vincent Massol 46.1 30
Vincent Massol 107.2 31 XWiki supports [[basic access authentication>>https://en.wikipedia.org/wiki/Basic_access_authentication]], a method designed to allow a web browser or other client programs 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:
Vincent Massol 46.1 32
33 {{code}}
Vincent Massol 107.2 34 https://username:password@mywiki.xwiki.com/xwiki/bin/view/Main/WebHome?basicauth=1
Vincent Massol 46.1 35 {{/code}}
36
37 {{warning}}
Manuel Smeria 64.2 38 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##.
Vincent Massol 46.1 39 {{/warning}}
40
gcoquard 112.1 41 == Container Authentication ==
Vincent Massol 70.1 42
43 Delegates authentication to the Servlet Container. If it fails it falls back to the standard XWiki authentication.
44
45 To configure XWiki to use it, specify:
46
Thomas Mortagne 79.1 47 {{code language="properties"}}
Vincent Massol 70.1 48 xwiki.authentication.authclass=com.xpn.xwiki.user.impl.xwiki.AppServerTrustedAuthServiceImpl
49 {{/code}}
50
Thomas Mortagne 79.1 51 For users to be actually created and not just set as context user you must set in ##xwiki.cfg## configuration file (if the property does not already exist, create it):
52
53 {{code language="properties"}}
54 xwiki.authentication.createuser=empty
55 {{/code}}
56
gcoquard 112.1 57 == OpenId Connect Authentication ==
Vincent Massol 107.1 58
59 See the [[OpenId Connect Authenticator Extension>>extensions:Extension.OpenID Connect.OpenID Connect Authenticator.WebHome]].
60
gcoquard 112.1 61 == Active Directory ==
Thomas Mortagne 87.1 62
Thomas Mortagne 97.1 63 If you're looking to connect XWiki to an Active Directory server, you currently have 2 options:
Vincent Massol 95.1 64
Thomas Mortagne 97.1 65 * Using the manual and generic approach using the [[LDAP Authenticator extension>>extensions:Extension.LDAP.Authenticator]]
Vincent Massol 107.2 66 * Using the dedicated [[Active Directory Application>>https://store.xwiki.com/xwiki/bin/view/Extension/ActiveDirectoryApplication]] which is a paying application dedicated to simplifying the integration of Active Directory with XWiki. The Active Directory Application allows you to easily connect your Active Directory server to XWiki using a **visual editor**, update **advanced configuration** settings without restarting the application server, **technical support** provided by [[XWiki SAS>>https://xwiki.com/]], etc.
Vincent Massol 96.1 67
gcoquard 112.1 68 == LDAP ==
Vincent Massol 96.1 69
Thomas Mortagne 100.1 70 See the [[LDAP Authenticator extension>>extensions:Extension.LDAP.Authenticator]].
Thomas Mortagne 98.1 71
72 The deprecated LDAP core authenticator (for XWiki < 7.4) can be found on [[OldLDAPAuthenticator]].
73
gcoquard 112.1 74 == Custom Authentication ==
Vincent Massol 1.1 75
gcoquard 115.1 76 You can create your custom authentication by following [[Create a Custom Authenticator>>Documentation.DevGuide.Tutorials.Create a Custom Authenticator.WebHome]] tutorial.
Vincent Massol 1.1 77
gcoquard 115.1 78 Note that it's also possible to customize the right management system, see [[Security Module>>extensions:Extension.Security Module]] for more details.
Silvia Macovei 34.1 79
gcoquard 112.1 80 === Custom Authentication using a Groovy script in a wiki page ===
Vincent Massol 28.1 81
82 Start by specifying you want to use the Groovy Authenticator:
83
Thomas Mortagne 57.1 84 {{code language="properties"}}
Vincent Massol 28.1 85 xwiki.authentication.authclass = com.xpn.xwiki.user.impl.xwiki.GroovyAuthServiceImpl
Silvia Macovei 34.1 86 {{/code}}
Vincent Massol 28.1 87
88 Then add another configuration parameter to specify in which wiki page the authenticator is:
89
Thomas Mortagne 57.1 90 {{code language="properties"}}
Vincent Massol 28.1 91 xwiki.authentication.groovy.pagename = MySpace.MyPage
Silvia Macovei 34.1 92 {{/code}}
Vincent Massol 28.1 93
Manuel Smeria 64.2 94 Then put some Groovy code in a wiki page that returns a XWikiAuthService object.
Vincent Massol 28.1 95
gcoquard 112.1 96 = Configuration =
97
98 == Authentication parameters ==
99
ShawnLauzon 1.14 100 You can set each of these parameters by setting:
101
Silvia Macovei 34.1 102 {{code}}
Vincent Massol 89.2 103 xwiki.authentication.<param_name>=<param_value>
Silvia Macovei 34.1 104 {{/code}}
ShawnLauzon 1.14 105
Silvia Macovei 34.1 106 |=Name|=Optional|=Allowed values|=Default value|=Description
107 |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.
108 |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.
109 |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
110 |cookielife|Yes|Number|14|Number of days cookies take to expire
111 |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//
112 |default_page|Yes|String|/bin/view/ Main/WebHome|Page to redirect to if xredirect parameter is not set
113 |encryptionalgorithm|Yes|?|?|Set the Encryption Algorithm used to encrypt and decrypt cookies
114 |encryptionmode|Yes|?|?|Set the Encryption Mode used to encrypt and decrypt cookies
115 |encryptionpadding|Yes|?|?|Set the Encryption Padding used to encrypt and decrypt cookies
116 |errorpage|Yes|String|/bin/loginerror/ XWiki/XWikiLogin|Page to redirect to if there is an error logging in
117 |loginpage|Yes|String|/bin/login/ XWiki/XWikiLogin|Page to redirect to when not logged in
Caleb James DeLisle 35.1 118 |loginsubmitpage|Yes|String|/loginsubmit/ XWiki/XWikiLogin|The URL where the username and password are posted to when logging in.
Silvia Macovei 34.1 119 |logoutpage|Yes|String|/bin/logout/ XWiki/XWikiLogout|Page to redirect to after logged out
120 |realmname|Yes|String|XWiki|Sets the realm name
121 |protection|Yes|all, validation, encryption, none|all|Protection level for the "remember me" cookie functionality
Thomas Mortagne 101.1 122 |unauthorized_code|Yes|Number|403|The HTTP status code to return when the login has failed.
Silvia Macovei 34.1 123 |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 124
Silvia Macovei 34.1 125 1. Only required if protection = encryption or all (default)
126 1. Only required if protection = validation or all (default)
ShawnLauzon 1.17 127
gcoquard 112.1 128 == Security ==
129
130 Starting with {{code}}XWiki 11.6RC1{{/code}} we provide some authentication strategies that are triggered if a user fails several time in a row to login.
131 You can configure those strategies and when they should be triggered in the Administration > Authentication page.
132
133 {{gallery}}
134 image:authentication-security-administration.png
135 {{/gallery}}
136
137
138 For more information see the [[Authencation Security Module documentation>>extensions:Extension.Authentication Security Module]].
139
140
Silvia Macovei 34.2 141 = Kerberos SSO Authentication =
JasperSiepkes 21.1 142
Silvia Macovei 34.1 143 {{warning}}
Thomas Mortagne 94.1 144 This implementation of SSO is currently under review see: https://jira.xwiki.org/browse/XWIKI-2496 . The class which is described in this segment of documentation, AppServerTrustedKerberosAuthServiceImpl, is not part of the default XWiki distribution!
Silvia Macovei 34.1 145 {{/warning}}
146
Manuel Smeria 64.2 147 The following is an example of **mod_auth_kerb** for Apache being used to easily implement XWiki authentication of users via 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 148
JasperSiepkes 20.1 149 First of all you need to create a principal and keytab for the webserver:
Silvia Macovei 34.1 150
151 {{code}}
JasperSiepkes 20.1 152 # kadmin
153 kadmin> addprinc -randkey HTTP/wiki.example.com
154 kadmin> ktadd -k /etc/apache2/ssl/wiki.keytab HTTP/wiki.example.com
155 kadmin> quit
Silvia Macovei 34.1 156 {{/code}}
Thomas Mortagne 10.1 157
JasperSiepkes 20.1 158 Make sure the keytab has the right permissions and ownership:
Silvia Macovei 34.1 159
160 {{code}}
JasperSiepkes 20.1 161 chown www-data:www-data /etc/apache2/ssl/wiki.keytab
162 chmod 400 /etc/apache2/ssl/wiki.keytab
Silvia Macovei 34.1 163 {{/code}}
Thomas Mortagne 11.1 164
Manuel Smeria 64.2 165 Install **mod_auth_kerb** in your linux installation. On Debian or Ubuntu this would be achieved by running:
Silvia Macovei 34.1 166
167 {{code}}
JasperSiepkes 20.1 168 aptitude install libapache2-mod-auth-kerb
Silvia Macovei 34.1 169 {{/code}}
170
JasperSiepkes 20.1 171 Of course the installation procedure varies per Linux distribution.
172
173 If your xwiki installation is mounted in Apache HTTPD under /xwiki, add the following to the virtual host configuration:
Silvia Macovei 34.1 174
175 {{code}}
Clemens Robbenhaar 116.2 176 <Location "/xwiki">
JasperSiepkes 20.1 177 AuthType Kerberos
178 AuthName "Kerberos Login"
179 KrbAuthRealms EXAMPLE.COM
180 Krb5Keytab "/etc/apache2/ssl/wiki.keytab"
181 KrbMethodK5Passwd off
182 KrbMethodNegotiate on
183 KrbSaveCredentials on
184 require valid-user
185 </Location>
Silvia Macovei 34.1 186 {{/code}}
JasperSiepkes 20.1 187
Manuel Smeria 64.2 188 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 189
190 {{code}}
gcoquard 108.1 191 <Connector port="8009" address="127.0.0.1" enableLookups="false" tomcatAuthentication="false" redirectPort="8443" protocol="AJP/1.3" ></Connector>
Silvia Macovei 34.1 192 {{/code}}
JasperSiepkes 20.1 193
Manuel Smeria 64.2 194 Place the **authkerb.jar** jar in the ##WEB-INF/lib## directory of XWiki in Apache Tomcat.
JasperSiepkes 20.1 195
Manuel Smeria 64.2 196 Have Xwiki use the authentication module by changing the "xwiki.authentication.authclass" property in the ##WEB-INF/lib/xwiki.cfg## file.
Silvia Macovei 34.1 197
198 {{code}}
JasperSiepkes 20.1 199 xwiki.authentication.authclass=com.xpn.xwiki.user.impl.xwiki.AppServerTrustedKerberosAuthServiceImpl
Silvia Macovei 34.1 200 {{/code}}
JasperSiepkes 20.1 201
Manuel Smeria 64.2 202 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 property include: //https:~/~/// for all secured connections or **example.com** for all example.com subdomains.
JasperSiepkes 20.1 203
Manuel Smeria 64.2 204 When I used 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 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 205
Silvia Macovei 34.2 206 The configuration of ldap:
jek 29.1 207
Silvia Macovei 34.1 208 {{code}}
jek 29.1 209 xwiki.authentication.authclass=com.wiki.sso.SSOLdapAuthenicationImpl
210 xwiki.authentication.ldap=1
211 xwiki.authentication.ldap.server=<ad-server>
212 xwiki.authentication.ldap.port=389
213 xwiki.authentication.ldap.base_DN=<OU=Users,...............>
214 #use a fixed user to attach to the ldap database,
215 #the password is not provided with the SSOLdapAuthenicationImpl
216 xwiki.authentication.ldap.bind_DN=<domain>\\<user>
217 xwiki.authentication.ldap.bind_pass=<password>
218 #Microsoft AD configuration
219 xwiki.authentication.ldap.UID_attr=sAMAccountName
220 xwiki.authentication.ldap.fields_mapping=name=sAMAccountName,last_name=sn,first_name=givenName,fullname=displayName,mail=mail,ldap_dn=dn
221 xwiki.authentication.ldap.group_memberfields=member,uniqueMember
222 #LDAP group mapping
223 xwiki.authentication.ldap.group_mapping=XWiki.XWikiAdminGroup=CN=WIKI_Admin,............|\
224 XWiki.XWikiAllGroup=CN=WIKI_User,...........
Silvia Macovei 34.1 225 {{/code}}
jek 29.1 226
Manuel Smeria 64.2 227 The java code:
Silvia Macovei 34.1 228
229 {{code}}
jek 29.1 230 package com.wiki.sso;
231
232
233 import org.apache.commons.logging.Log;
234 import org.apache.commons.logging.LogFactory;
235
236 import com.xpn.xwiki.XWikiContext;
237 import com.xpn.xwiki.XWikiException;
238 import com.xpn.xwiki.user.api.XWikiUser;
239 import com.xpn.xwiki.user.impl.LDAP.XWikiLDAPAuthServiceImpl;
240
241 import java.security.Principal;
242
243 public class SSOLdapAuthenicationImpl extends XWikiLDAPAuthServiceImpl {
244 /**
245 * Logging tool.
246 */
247 private static final Log LOG = LogFactory.getLog(SSOLdapAuthenicationImpl.class);
248
249
Sorin Burjan 64.3 250 public XWikiUser checkAuth(XWikiContext context) throws XWikiException {
251 String user = getRemoteUser(context);
252 if ((user != null) || !user.equals("")) {
253 if (LOG.isInfoEnabled())
254 LOG.info("Launching create user for " + user);
255 if ( authenticate(user, context) != null ) {
256 if (LOG.isInfoEnabled())
257 LOG.info("Create user done for " + user);
258 user = "XWiki." + user;
259 context.setUser(user);
260 System.out.println("User is set to:" + user);
261 return new XWikiUser(user);
262 } else {
263 LOG.error( "User " + user + " can't be authenticated against ldap" );
264 }
265 }
266 return super.checkAuth(context);
267 }
jek 29.1 268
Sorin Burjan 64.3 269 /**
270 * We cannot authenticate locally since we need to trust the app server for
271 * authentication
272 *
273 * @param username
274 * @param password
275 * @param context
276 * @return
277 * @throws XWikiException
278 */
279 public XWikiUser checkAuth(String username, String password,
280 String rememberme, XWikiContext context) throws XWikiException {
281 String user = getRemoteUser(context);
282 if ((user == null) || user.equals("")) {
283 return super.checkAuth(username, password, rememberme, context);
284 }
285 return checkAuth(context);
286 }
jek 29.1 287
Sorin Burjan 64.3 288 private String getRemoteUser(XWikiContext context) {
289 String userName = context.getRequest().getHttpServletRequest()
290 .getRemoteUser();
291 if (userName != null) {
292 // only take the front of the username@domain
293 String[] elements = userName.split("@", 2);
294 userName = elements[0];
295 }
296 return userName;
297 }
jek 29.1 298
299 public Principal authenticate(String login, XWikiContext context) throws XWikiException
300 {
301 if (LOG.isTraceEnabled()) {
302 LOG.trace("Starting LDAP authentication");
303 }
304
305 /*
306 * TODO: Put the next 4 following "if" in common with XWikiAuthService to ensure coherence This method was
307 * returning null on failure so I preserved that behaviour, while adding the exact error messages to the context
308 * given as argument. However, the right way to do this would probably be to throw XWikiException-s.
309 */
310
311 if (login == null) {
312 // If we can't find the username field then we are probably on the login screen
313
314 if (LOG.isDebugEnabled()) {
315 LOG.debug("The provided user is null."
316 + " We don't try to authenticate, it probably means the user is in non logged mode.");
317 }
318
319 return null;
320 }
321
322 // Check for empty usernames
323 if (login.equals("")) {
324 context.put("message", "nousername");
325
326 if (LOG.isDebugEnabled()) {
327 LOG.debug("LDAP authentication failed: login empty");
328 }
329
330 return null;
331 }
332
333 // If we have the context then we are using direct mode
334 // then we should specify the database
335 // This is needed for virtual mode to work
336 Principal principal = null;
337
338 // Try authentication against ldap
339 principal = ldapAuthenticate(login, "", context);
340
341 if (LOG.isDebugEnabled()) {
342 if (principal != null) {
343 LOG.debug("LDAP authentication succeed with principal [" + principal.getName() + "]");
344 } else {
345 LOG.debug("LDAP authentication failed for user [" + login + "]");
346 }
347 }
348
349 return principal;
350 }
351 }
Silvia Macovei 34.1 352 {{/code}}
Vincent Massol 116.1 353
354 = Mail Templates =
355
356 When [[logging in>>Documentation.UserGuide.Features.Authentication#HLoggingin]], you have the option of resetting your user's password if you forgot it, or to find your username based on your email address. When choosing these options, you'll be sent an email. It's possible to control the templates used for these emails and to customize them by editing the following pages in object mode:
357 * ##XWiki.ResetPasswordMailContent##
358 * ##XWiki.ForgotUsernameMailContent##
359

Get Connected