DokuWiki

It's better when it's simple

User Tools

Site Tools


devel:auth_plugins

This is an old revision of the document!


Authentication Plugins

Info on this page is for 2013-05-10 “Weatherwax” and later

See for info for releases 2012-10-13 “Adora Belle” and older on:
- development of old authentication backends
- old authentication modules auth

Below first version of docs for new auth plugins. It may contain errors! Please report them, all feedback is welcome.

DokuWiki's authentication system is highly modular and can, generally speaking, authenticate using anything that is accessible from PHP. Available Auth Plugins are listed.

In summary, there are two distinct ways of building your authentication plugin. Firstly, you can create a plugin that implements all DokuWiki's methods needed to gather user info and verify users. Secondly, you can use the trustExternal() method, which will replace the Dokuwiki authentication functions. You handle the authentication in this method by calling your own backend.

Synopsis

An Authentication Plugin Example needs:

  • class name auth_plugin_authexample
  • which extends DokuWiki_Auth_Plugin1).
  • to be stored in a file lib/plugins/authexample/auth.php.

Moreover, a plugin.info.txt file is needed. For full details of plugins and their files and how to create more auth components refer to plugin file structure.

Required implementation

You need to implement at least two fields and three methods.

Fields:

  • $success
    This simple boolean needs to be set to true in your constructor if your auth module was correctly initialized. Use this to notify the frontend if anything went wrong by setting it to false.
  • $cando
    The $cando field is an associative array of booleans. You need to set the array fields to true for all functions your backend provides. Here is a list of keys in $cando and their meaning:
    • addUser – can Users be created?
    • delUser – can Users be deleted?
    • modLogin – can login names be changed?
    • modPass – can passwords be changed?
    • modName – can real names be changed?
    • modMail – can emails be changed?
    • modGroups – can groups be changed?
    • getUsers – can a (filtered) list of users be retrieved?
    • getUserCount – can the number of users be retrieved?
    • getGroups – can a list of available groups be retrieved?
    • external – does the module do external auth checking?
    • logout – can the user logout again? (eg. not possible with HTTP auth)

Methods:

Only a few functions need to be implemented. But the more you do the more the frontend will be able to do. See lib/plugins/auth.php for the methods' arguments and return values and for an example implementation see lib/plugins/authplain/auth.php the plain authentication backend, DokuWikis default.

  • __construct()
    Well your class should have a constructor of course :-) Set the fields $success and $cando mentioned above here.
  • checkPass($user, $pass)
    This function need to check if the given userid $user exists and the given plaintext password $pass is correct.
  • getUserData($user)
    Used to return user information like email address and real name, for the requested userid $user
    Return false or an array with at least the keys
    array(
        'name' => string,
        'mail' => string,
        'grps' => array()
    )

Optional implementation

All these methods are optional and will only be called if the appropriate $cando fields are set

  • trustExternal() (replaces DokuWiki authentication functions)
    If $cando['external'] is true, this method is used to authenticate a user – all other DokuWiki internals will not be used for authenticating. The function can be used to authenticate against third party cookies or Apache auth mechanisms and replaces the default auth_login() function from inc/auth.php.

    If this method is implemented you may omit the implementation of all the other methods from your module. You only really needs a constructor and this trustExternal() function, however it is strongly recommended to have getUserData() so DokuWiki can display your users nicely and logoff() to permit DokuWiki to communicate the logoff to your backend. The other methods are only needed when you like that some internals of DokuWiki interact with your backend. Search the source code or browse on http://xref.dokuwiki.org/ to check out the connections.

    Have a look at the punbb backend for an example usage of this method. According to the example method in the parent auth_basic class the trustExternal() method has to set the global variables: $USERINFO, $SERVER and _SESSION[DOKU_COOKIE] for the indicated fields.

    The implementation depends very much on your backend, here are some often used parts indicated as example. Look also for other implementations, when it doesn't fit your requirements.
    function trustExternal($user, $pass, $sticky=false) {
        global $USERINFO;
     
        // someone used the login form
        if(!empty($user)){
            //situation: there are user credentials, lets check them
            if( ...try to authenticate again your backend...)
     
                // here you can handle additional post login actions 
                // for your backend
     
            }else{
                //invalid credentials - log off
                msg($lang['badlogin'],-1);
                auth_logoff(); // needs implementation of logOff() method
                return false;
            }
        }
     
        //situation: no login form used or logged in successful 
     
     
        // check where if there is a logged in user e.g from session,
        // $_SERVER or what your auth backend supplies...
     
        if( ...check here if there is a logged in user...) {
     
            $USERINFO['name'] = string
            $USERINFO['mail'] = string
            $USERINFO['grps'] = array()
     
            $_SERVER['REMOTE_USER']                = $user; //userid
            $_SESSION[DOKU_COOKIE]['auth']['user'] = $user; //userid
            $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
     
            return true;
        }else{
            //when needed, logoff explicitly.
        }

    For a description of the $USERINFO variables see the documentation of the getUserData() function. Do not forget to add global $USERINFO to the start of this function, to make the variable accessible.

    Another thing to keep in mind if you're implementing Single Sign On based on a cookie, is that if you want to be able to use DokuWiki's login form when SSO cookie is not present, you need to set that cookie once you verify the credentials, so on next page load you can authenticate based on that SSO cookie as $user and $pass variables will be empty since login form is not submitted. In punbb this is done with pun_setcookie() function call.

    Dokuwiki will not show any message if the login failed, therefore this method shall show some information using msg().

    Examples
    See also this working example of trustExternal().

    Some (old) backends using this function are: punbb, cas, cosign, plaincas, django, extdjango, gforge, http version of ggauth, keeyaiwp, mod_auth_tkt, ssp

  • logOff() (only when required/possible)
    Run in addition to the usual logoff. Useful with trustExternal() to initiate actions for the external backend e.g. use it to clear cookies or similar actions.
  • isCaseSensitive() (optional)
    When your backend is caseinsensitive, override it with a method that returns false.
  • cleanUser($user) (optional)
    Applied when username is given to and return from backend. Enforce here also username restrictions.
  • cleanGroup($group) (optional)
    Applied when groupname is given to and return from backend. Enforce here also groupname restrictions. Groupnames are to be passed without a leading '@' here.
  • useSessionCache($user) (only when required)
    DokuWiki caches user info for a timespan. This function check expiration of this caching.

Inherited methods

  • All the optional methods defined above are inherited methods, mostly these are a fallback warning explaining it's not implemented in your plugin yet.
  • canDo($cap) Checks capabilities set in $cando field, or the pseudo capabilities: Profile and UserMod.
  • triggerUserMod($type, $params) Triggers AUTH_USERDATA_CHANGE, which calls the methods defined on the current authentication plugin for the $type equal to: create, modify and delete where $params are type specific parameters.
  • See common plugin functions for inherited function available to all plugins. e.g. localisation, configuration and introspection.

Notes

Config loading sequence

At the moment, temporary, also the config of old style auth modules are loaded.
The loading order is:

  1. Default config settings
  2. Old style auth module config settings (i.e. $conf['auth']['yourauthbackend']))
  3. The current auth plugin settings (i.e. $conf['plugin']['yourauthplugin']).

Start session customization

(Available since release 2014-05-05 “Ponder Stibbons”)

Dokuwiki starts (in inc/init.php) a session prior to using the authentication plugin. If your framework uses specific session settings, you can define before your own settings in the file conf/local.protected.php. You need to create it when non-existing. In this conf/local.protected.php you can supply one or more of these defines:

  • DOKU_SESSION_NAME
  • DOKU_SESSION_LIFETIME
  • DOKU_SESSION_PATH
  • DOKU_SESSION_DOMAIN

The defines correspond to the arguments of session_set_cookie_params(), please refer to its docs for more details. To use SSL for your cookies, you enable the securecookie configuration setting.

Example
conf/local.protected.php
//settings specific for use of the ... authentication plugin
define ('DOKU_SESSION_NAME', "YOURSESSIONNAME");
define ('DOKU_SESSION_LIFETIME', 0);
//etc...
 
//a custom session path
$sessiepath = fullpath(dirname(__FILE__) . '/../../') . '/session';
session_save_path($sessiepath);

About autoloading

Your backend (or its framework) cannot use __autoload to include further classes, those classes must be loaded manually via require()

Handling of old auth backends

When you update your wiki to the 2013-05-10 “Weatherwax” release, you need an auth plugin for the authentication, because the authentication backends aren't supported anymore. The previous bundled authentication backends are already converted to auth plugins: AuthPlain, AuthMySQL, AuthPgSQL, AuthAD and AuthLDAP.

When you use another plugin than the bundled one, you need to check if someone has already shared the auth plugin version in the plugin repository. You can filter the plugins by Auth plugintype. Or you should rewrite the authentication backend yourself. See Howto update your old backend below.

Update wiki to new backend

When you used the plain authentication backend before, DokuWiki will pick up the new authplain due to DokuWiki's defaults are updated too. For all other values of the authtype configuration, you get the warning User authentication is temporarily unavailable. If this situation persists, please inform your Wiki Admin. and you cannot login anymore. This can be solved by updating the authtype configuration to the auth plugin version of your desired backend. When you use a not bundled auth plugin you should first install this one.

When your desired auth plugin is installed you can modify your the authtype configuration setting in conf/local.php (or conf/local.protected.php) by an editor on your server by prefixing your <authentication backend name> with auth to auth<authentication backend name>:

conf/local.php
...
// $conf['authtype'] = 'example';  //auth backend
$conf['authtype'] = 'authexample'; //auth plugin
...

Howto install an auth plugin via plugin manager without working backend?

When you prefer to install an auth plugin by the DokuWiki plugin manager, you need to switch to the plain authentication backend. You need access to the configuration file conf/local.php on you server. Open it in an editor and remove the line from conf/local.php or conf/local.protected.php:

// $conf['authtype'] = '...'; //disable your authtype config

or change that line to:

$conf['authtype'] = 'authplain';

and save the file. Now your wiki uses the AuthPlain plugin. Next you login as superuser. Hint: Probably you can login by the user you define on installation (the installer creates default that users as superuser). Now you can use the plugin manager as usually.

Next you can configure the plugin settings via the configuration manager (these settings are stored in conf/local.php) or you can save these protected against changes from the configuration manager by creating and editing the file conf/local.protected.php. Lastly, you change the authtype configuration to your new auth plugin and save. When your wiki becomes inaccessible again, you can modify the configuration settings via an editor on your server again.

See farther for more info about the configuration files and the config manager.

Old configurations

When auth plugin is activated, and there is an old config available, then first the old auth backend is loaded, next the new auth plugin config is loaded. So when auth plugin configuration settings are set these overwrite the old auth backend values.

Complete load sequence of plugin config settings:

  1. settings from lib/plugins/<pluginname>/conf/default.php
  2. settings from conf/local.php (or possibly overwritten by conf/local.protected.php) where:
    1. first settings of $config['auth']['<authbackendname>'] are read
    2. and next settings of $config['plugin']['auth<authbackendname>']

Tip:
When you start changing settings of auth plugin, especially when you reset a setting to its plugin default, it is recommended to remove the old $config['auth']['<authbackendname>'] settings from the local.php and/or local.protected.php.

Howto update your old backend

Some tips on updating your backend from inc/auth/<authname>.class.php to an Auth plugin.

Simple approach:

  1. Create a plugin skelet corresponding to plugin file structure and the synopsis at top of this page. (skelet generator: http://pluginwiz.dokuwiki.org/)
    • Please prefix the plugin name of your Auth plugin by auth e.g. authexample.
  2. You can reuse the code from inc/auth/<authbackendname>.class.php in your new lib/plugins/auth<authbackendname>/auth.php.
    • Be aware you can load other plugins or helper plugins
    • There are some inherited functions for localisation, configuration and more, see inherited methods above.

FIXME more relevant directions??

Further reading

1)
defined in lib/plugins/auth.php
devel/auth_plugins.1421662717.txt.gz · Last modified: 2015-01-19 11:18 by 2001:41d0:1:b8a1::1

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki