User Authentication

Last modified by Thomas Mortagne on 2014/02/10 18:32

XWiki supports several different authentication mechanisms for authenticating users:

XWiki currently allows only one method of authentication to be enabled at a time. This will probably be improved in the future.

Also note that XWiki requires cookies to be enable in order to track your session and to keep you logged in between pages.

Form Authentication

Form authentication is the default way to get authenticated within a Wiki. It requires a user and a password.

Basic Authentication

XWiki supports 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:

http://username:password@mywiki.xwiki.com/xwiki/bin/view/Main/WebHome?basicauth=1
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.

Container Authentication

Delegates authentication to the Servlet Container. If it fails it falls back to the standard XWiki authentication.

To configure XWiki to use it, specify:

xwiki.authentication.authclass=com.xpn.xwiki.user.impl.xwiki.AppServerTrustedAuthServiceImpl

Social Login

See the Social Login Application.

LDAP Authentication

If you are going to use the LDAP Admin Extension, which makes it easier to configure LDAP, then you only need to uncomment xwiki.authentication.authclass property and nothing else. Unlike editing xwiki.cfg, which requires you to redeploy xWiki app, LDAP Extension allows you to make changes without restarting. 

Generic LDAP configuration

In order to enable the LDAP support you have to change the authentication method in WEB-INF/xwiki.cfg as follows:

#-# LDAP authentication service
# xwiki.authentication.authclass=com.xpn.xwiki.user.impl.LDAP.XWikiLDAPAuthServiceImpl

#-# Turn LDAP authentication on - otherwise only XWiki authentication
#-# - 0: disable
#-# - 1: enable
#-# The default is 1
# xwiki.authentication.ldap=1

You can setup the LDAP configuration in the xwiki.cfg file by filling the following properties:

#-# LDAP Server (Active Directory, eDirectory, OpenLDAP, etc.)
xwiki.authentication.ldap.server=127.0.0.1
xwiki.authentication.ldap.port=389

#-# LDAP login, empty = anonymous access, otherwise specify full dn
#-# {0} is replaced with the user name, {1} with the password
xwiki.authentication.ldap.bind_DN=cn={0},department=USER,department=INFORMATIK,department=1230,o=MP
xwiki.authentication.ldap.bind_pass={1}

#-# The Base DN used in LDAP searches
xwiki.authentication.ldap.base_DN=

#-# LDAP query to search the user in the LDAP database (in case a static admin user is provided in
#-# xwiki.authentication.ldap.bind_DN)
#-# {0} is replaced with the user uid field name and {1} with the user name
#-# The default is ({0}={1})
# xwiki.authentication.ldap.user_search_fmt=({0}={1})

#-# Only members of the following group can authenticate.
#-# The following kind of groups are supported:
#-# * LDAP static groups (users/subgroups are listed statically in the group object)
#-# * [Since 3.3M1] LDAP organization units (users/subgroups are sub object of the provided organization unit)
#-# * [Since 3.3M1] LDAP filter (users/groups are object found in a search with the provided filter)
# xwiki.authentication.ldap.user_group=cn=developers,ou=groups,o=MegaNova,c=US

#-# [Since 1.5RC1, XWikiLDAPAuthServiceImpl]
#-# Only users not member of the following group can authenticate.
#-# The following kind of groups are supported:
#-# * LDAP static groups (users/subgroups are listed statically in the group object)
#-# * [Since 3.3M1] LDAP organization units (users/subgroups are sub object of the provided organization unit)
#-# * [Since 3.3M1] LDAP filter (users/groups are object found in a search with the provided filter)
# xwiki.authentication.ldap.exclude_group=cn=admin,ou=groups,o=MegaNova,c=US

#-# Specifies the LDAP attribute containing the identifier to be used as the XWiki name
#-# The default is cn
# xwiki.authentication.ldap.UID_attr=cn

#-# [Since 1.5M1, XWikiLDAPAuthServiceImpl]
#-# The potential LDAP groups classes. Separated by commas.
# xwiki.authentication.ldap.group_classes=group,groupOfNames,groupOfUniqueNames,dynamicGroup,dynamicGroupAux,groupWiseDistributionList,posixGroup,apple-group

#-# [Since 1.5M1, XWikiLDAPAuthServiceImpl]
#-# The potential names of the LDAP groups fields containings the members. Separated by commas. 
# xwiki.authentication.ldap.group_memberfields=member,uniqueMember,memberUid

#-# retrieve the following fields from LDAP and store them in the XWiki user object (xwiki-attribute=ldap-attribute)
xwiki.authentication.ldap.fields_mapping=last_name=sn,first_name=givenName,email=mail

#-# [Since 1.3M2, XWikiLDAPAuthServiceImpl]
#-# On every login update the mapped attributes from LDAP to XWiki otherwise this happens only once when the XWiki
#-# account is created.
#-# - 0: only when creating user
#-# - 1: at each authentication
#-# The default is 0
xwiki.authentication.ldap.update_user=1

#-# [Since 1.3M2, XWikiLDAPAuthServiceImpl]
#-# Maps XWiki groups to LDAP groups, separator is "|".
#-# The following kind of groups are supported:
#-# * LDAP static groups (users/subgroups are listed statically in the group object)
#-# * [Since 3.3M1] LDAP organization units (users/subgroups are sub object of the provided organization unit)
#-# * [Since 3.3M1] LDAP filter (users/groups are object found in a search with the provided filter),
#-#   | character in the filter need to be escaped with backslash (\).
#-#
#-# Here is an example:
# xwiki.authentication.ldap.group_mapping=XWiki.XWikiAdminGroup=cn=AdminRole,ou=groups,o=domain,c=com|\
#                                         XWiki.LDAPUsers=ou=groups,o=domain,c=com|\
#                                         XWiki.Organisation=(cn=testers)

#-# [Since 1.3M2, XWikiLDAPAuthServiceImpl]
#-# Time in s after which the list of members in a group is refreshed from LDAP
#-# The default is 21600 (6 hours)
# xwiki.authentication.ldap.groupcache_expiration=21600

#-# [Since 1.3M2, XWikiLDAPAuthServiceImpl]
#-# - create : synchronize group membership only when the user is first created
#-# - always: synchronize on every login
#-# The default is always
# xwiki.authentication.ldap.mode_group_sync=always

#-# [Since 1.3M2, XWikiLDAPAuthServiceImpl]
#-# If ldap authentication fails for any reason, try XWiki DB authentication with the same credentials
#-# - 0: disable
#-# - 1: enable
#-# The default is 0
xwiki.authentication.ldap.trylocal=1

#-# [Since 1.3M2, XWikiLDAPAuthServiceImpl]
#-# SSL connection to LDAP server
#-# - 0: normal
#-# - 1: SSL
#-# The default is 0
# xwiki.authentication.ldap.ssl=0

#-# [Since 1.3M2, XWikiLDAPAuthServiceImpl]
#-# The keystore file to use in SSL connection
# xwiki.authentication.ldap.ssl.keystore=

#-# [Since 1.5M1, XWikiLDAPAuthServiceImpl]
#-# The java secure provider used in SSL connection
#-# The default is com.sun.net.ssl.internal.ssl.Provider
# xwiki.authentication.ldap.ssl.secure_provider=com.sun.net.ssl.internal.ssl.Provider

#-# Bypass standard LDAP bind validation by doing a direct password comparison.
#-# If you don't know what you do, don't use that. It's covering very rare and bad use cases.
#-# - 0: disable
#-# - 1: enable
#-# The default is 0
# xwiki.authentication.ldap.validate_password=0

#-# [Since 1.5M1, XWikiLDAPAuthServiceImpl]
#-# Specifies the LDAP attribute containing the password to be used "when xwiki.authentication.ldap.validate_password"
#-# is set to 1
# xwiki.authentication.ldap.password_field=userPassword

#-# [Since 4.3M1, XWikiLDAPAuthServiceImpl]
#-# The maximum number of milliseconds the client waits for any operation under these constraints to complete.
#-# The default is 1000
# xwiki.authentication.ldap.timeout=1000
You can also setup the LDAP configuration in the XWiki.XWikiPreferences page by going to the object editor. Simply replace xwiki.authentication.ldap. with ldap_. For example xwiki.authentication.ldap.base_DN becomes ldap_base_DN.

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.

Here are some LDAP clients for checking your configuration:

Java based and Open Source:

Windows only:

Detailed use cases

See the LDAP configuration uses cases for some detailed use cases.

Enable LDAP debug log

See Logging.

The specific packages to track for LDAP are com.xpn.xwiki.plugin.ldap and com.xpn.xwiki.user.impl.LDAP.

Starting with XWiki 4.2 we added a new Logging UI from the Administration section, which allows logging to be enabled at runtime, directly from the UI, without the need to restart the wiki.

In XWiki 3.4 you need to add the following in WEB-INF/classes/logback.xml:

<!-- LDAP debugging -->
<logger name="com.xpn.xwiki.plugin.ldap" level="trace"/>
<logger name="com.xpn.xwiki.user.impl.LDAP" level="trace"/>

Before 3.1, add the following to the log4j configuration file:

log4j.logger.com.xpn.xwiki.plugin.ldap=trace
log4j.logger.com.xpn.xwiki.user.impl.LDAP=trace

Custom Authentication

This allows plugging to any existing authentication mechanism such as SiteMinder, etc. To configure a custom authentication do the following:

  1. Implement the XWikiAuthService interface.
  2. Edit the WEB-INF/xwiki.cfg file and add a xwiki.authentication.authclass property pointing to your class. For example:
xwiki.authentication.authclass = com.acme.MyCustomAuthenticationService

You can find various authenticators examples.

Here's a tutorial on implementing a custom authentication class for authenticating against Oracle's SSO.

Note, that you can also implement own right management service by implementing the XWikiRightService interface:

xwiki.authentication.rightsclass = com.acme.MyCustomRightsService

and Group Service by implementing XWikiGroupService:

xwiki.authentication.groupclass = com.acme.MyCustomGroupService

Custom Authentication using a Groovy script in a wiki page

Start by specifying you want to use the Groovy Authenticator:

xwiki.authentication.authclass = com.xpn.xwiki.user.impl.xwiki.GroovyAuthServiceImpl

Then add another configuration parameter to specify in which wiki page the authenticator is:

xwiki.authentication.groovy.pagename = MySpace.MyPage

Then put some Groovy code in a wiki page that returns a XWikiAuthService object.

Authentication parameters

You can set each of these parameters by setting:

xwiki.authentication.~~param_name~~=~~param_value~~
NameOptionalAllowed valuesDefault valueDescription
encryptionKeyNo(1)?n/aSet 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.
validationKeyNo(2)?n/aSet 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.
cookiedomainsYesStringServer host nameWhich host(s) should your cookies be sent to; use only if you want to share cookies across domains, otherwise should be commented out
cookielifeYesNumber14Number of days cookies take to expire
cookiepathYesString/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
default_pageYesString/bin/view/ Main/WebHomePage to redirect to if xredirect parameter is not set
encryptionalgorithmYes??Set the Encryption Algorithm used to encrypt and decrypt cookies
encryptionmodeYes??Set the Encryption Mode used to encrypt and decrypt cookies
encryptionpaddingYes??Set the Encryption Padding used to encrypt and decrypt cookies
errorpageYesString/bin/loginerror/ XWiki/XWikiLoginPage to redirect to if there is an error logging in
loginpageYesString/bin/login/ XWiki/XWikiLoginPage to redirect to when not logged in
loginsubmitpageYesString/loginsubmit/ XWiki/XWikiLoginThe URL where the username and password are posted to when logging in.
logoutpageYesString/bin/logout/ XWiki/XWikiLogoutPage to redirect to after logged out
realmnameYesStringXWikiSets the realm name
protectionYesall, validation, encryption, noneallProtection level for the "remember me" cookie functionality
unauthorized_codeYesNumber401The HTTP status code to return when the login has failed.
useipYestrue / falsetrueSpecify to use the IP address when encrypting the cookie data; if IP address changes will need to re-login.
  1. Only required if protection = encryption or all (default)
  2. Only required if protection = validation or all (default)

Kerberos SSO Authentication

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!

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.

First of all you need to create a principal and keytab for the webserver:

# kadmin
kadmin> addprinc -randkey HTTP/wiki.example.com
kadmin> ktadd -k /etc/apache2/ssl/wiki.keytab HTTP/wiki.example.com
kadmin> quit

Make sure the keytab has the right permissions and ownership:

chown www-data:www-data /etc/apache2/ssl/wiki.keytab
chmod 400 /etc/apache2/ssl/wiki.keytab

Install mod_auth_kerb in your linux installation. On Debian or Ubuntu this would be achieved by running:

aptitude install libapache2-mod-auth-kerb

Of course the installation procedure varies per Linux distribution.

If your xwiki installation is mounted in Apache HTTPD under /xwiki, add the following to the virtual host configuration:

<Location /xwiki/>
  AuthType Kerberos
  AuthName "Kerberos Login"
  KrbAuthRealms EXAMPLE.COM
  Krb5Keytab "/etc/apache2/ssl/wiki.keytab"
  KrbMethodK5Passwd off
  KrbMethodNegotiate on
  KrbSaveCredentials on
  require valid-user
</Location>

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):

<Connector port="8009" address="127.0.0.1" enableLookups="false" tomcatAuthentication="false" redirectPort="8443" protocol="AJP/1.3" />

Place the authkerb.jar jar in the WEB-INF/lib directory of XWiki in Apache Tomcat.

Have Xwiki use the authentication module by changing the "xwiki.authentication.authclass" property in the WEB-INF/lib/xwiki.cfg file.

xwiki.authentication.authclass=com.xpn.xwiki.user.impl.xwiki.AppServerTrustedKerberosAuthServiceImpl

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.

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.

The configuration of ldap:

xwiki.authentication.authclass=com.wiki.sso.SSOLdapAuthenicationImpl
xwiki.authentication.ldap=1
xwiki.authentication.ldap.server=<ad-server>
xwiki.authentication.ldap.port=389
xwiki.authentication.ldap.base_DN=<OU=Users,...............>
#use a fixed user to attach to the ldap database,
#the password is not provided with the SSOLdapAuthenicationImpl
xwiki.authentication.ldap.bind_DN=<domain>\\<user>
xwiki.authentication.ldap.bind_pass=<password>
#Microsoft AD configuration
xwiki.authentication.ldap.UID_attr=sAMAccountName
xwiki.authentication.ldap.fields_mapping=name=sAMAccountName,last_name=sn,first_name=givenName,fullname=displayName,mail=mail,ldap_dn=dn
xwiki.authentication.ldap.group_memberfields=member,uniqueMember
#LDAP group mapping
xwiki.authentication.ldap.group_mapping=XWiki.XWikiAdminGroup=CN=WIKI_Admin,............|\
                                        XWiki.XWikiAllGroup=CN=WIKI_User,...........

The java code:

package com.wiki.sso;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.user.api.XWikiUser;
import com.xpn.xwiki.user.impl.LDAP.XWikiLDAPAuthServiceImpl;

import java.security.Principal;

public class SSOLdapAuthenicationImpl extends XWikiLDAPAuthServiceImpl {
   /**
     * Logging tool.
     */

    private static final Log LOG = LogFactory.getLog(SSOLdapAuthenicationImpl.class);


 public XWikiUser checkAuth(XWikiContext context) throws XWikiException {
  String user = getRemoteUser(context);
 if ((user != null) || !user.equals("")) {
  if (LOG.isInfoEnabled())
    LOG.info("Launching create user for " + user);
  if ( authenticate(user, context) != null ) {
   if (LOG.isInfoEnabled())
     LOG.info("Create user done for " + user);
    user = "XWiki." + user;
    context.setUser(user);
    System.out.println("User is set to:" + user);
   return new XWikiUser(user);
   } else {
    LOG.error( "User " + user + " can't be authenticated against ldap" );
   }
  }
 return super.checkAuth(context);
 }

/**
  * We cannot authenticate locally since we need to trust the app server for
  * authentication
  *
  * @param username
  * @param password
  * @param context
  * @return
  * @throws XWikiException
  */

 public XWikiUser checkAuth(String username, String password,
   String rememberme, XWikiContext context) throws XWikiException {
  String user = getRemoteUser(context);
 if ((user == null) || user.equals("")) {
  return super.checkAuth(username, password, rememberme, context);
  }
 return checkAuth(context);
 }

 private String getRemoteUser(XWikiContext context) {
  String userName = context.getRequest().getHttpServletRequest()
    .getRemoteUser();
 if (userName != null) {
  // only take the front of the username@domain
  String[] elements = userName.split("@", 2);
   userName = elements[0];
  }
 return userName;
 }

    public Principal authenticate(String login, XWikiContext context) throws XWikiException
    {
       if (LOG.isTraceEnabled()) {
            LOG.trace("Starting LDAP authentication");
        }

       /*
         * TODO: Put the next 4 following "if" in common with XWikiAuthService to ensure coherence This method was
         * returning null on failure so I preserved that behaviour, while adding the exact error messages to the context
         * given as argument. However, the right way to do this would probably be to throw XWikiException-s.
         */


       if (login == null) {
           // If we can't find the username field then we are probably on the login screen

           if (LOG.isDebugEnabled()) {
                LOG.debug("The provided user is null."
                   + " We don't try to authenticate, it probably means the user is in non logged mode.");
            }

           return null;
        }

       // Check for empty usernames
       if (login.equals("")) {
            context.put("message", "nousername");

           if (LOG.isDebugEnabled()) {
                LOG.debug("LDAP authentication failed: login empty");
            }

           return null;
        }

       // If we have the context then we are using direct mode
       // then we should specify the database
       // This is needed for virtual mode to work
       Principal principal = null;

       // Try authentication against ldap
       principal = ldapAuthenticate(login, "", context);

       if (LOG.isDebugEnabled()) {
           if (principal != null) {
                LOG.debug("LDAP authentication succeed with principal [" + principal.getName() + "]");
            } else {
                LOG.debug("LDAP authentication failed for user [" + login + "]");
            }
        }

       return principal;
    }
}
Tags:
Created by VincentMassol on 2007/03/24 21:18

Get Connected