Wiki source code of User Authentication

Version 126.1 by slauriere on 2024/10/01

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

Get Connected