Authentication Tutorial

From KnowledgeTree Document Management Made Simple

Jump to: navigation, search

Introduction

This tutorial describes how to write a basic authentication module.

Requirements

We assume familiarity with PHP. More information is available on the [PHP documentation site].

Testing the example

Create a folder plugins/myauthentication.

Create a file MyAuthentication.php and MyAuthenticationPlugin.php or download [Myauthentication-example.zip]

MyAuthentication.php

All files should have <?php at the top of the files.

1  require_once(KT_LIB_DIR . '/authentication/authenticationprovider.inc.php');
2  require_once(KT_LIB_DIR . '/authentication/Authenticator.inc');
3 
4  class MyAuthenticationProvider extends KTAuthenticationProvider 
5  {
6      var $sName = "My authentication provider";
7      var $sNamespace = "my.authentication.provider";
8    
9	function &getAuthenticator($oSource) 
10	{
11        return new MyAuthenticator($oSource);
12    }
13      
14    function autoSignup($sUsername, $sPassword, $aExtra, $oSource) 
15    {   	
16        // automatically create a new user on the system
17        $oUser = User::createFromArray(array( 
18            'Username' => $sUsername,
19            'Name' => $sUsername,
20            'EmailNotification' => false,
21            'SmsNotification' => false,
22            'MaxSessions' => 3,
23            'authenticationsourceid' => $oSource->getId(),
24            'authenticationdetails' => md5($sPassword),
25            'password'=>'
26        ));
27       
28        return $oUser;
29    }   
30 }
31
32 class MyAuthenticator extends Authenticator 
33 {
34    function checkPassword($oUser, $sPassword) 
35    {
36        $info = $oUser->getAuthenticationDetails();
37        return (md5($sPassword) == $info);
38    }
39 }

Line 14: This is an optional. Here we do a simple automatic enrolment into KnowledgeTree. We would typically add some logic to validate the user via the relavant authentication source, followed by creating the user ass above. Normally, you would just not implement this function.

Line 24: The password must match the password on line 37.

Line 34: We introduce the main function that does the authentication for us.

Additional functions such as login(), logout(), verify(), do_editSourceProvider(), do_performEditSourceProvider() may be defined and will be discussed in the advanced authentication tutorial.

MyAuthenticationPlugin.php

We need to register the authentication provider so it is used by the system.

1   require_once(KT_LIB_DIR . '/plugins/plugin.inc.php');
2  
3   class MyAuthenticationPlugin extends KTPlugin 
4   {
5      var $sNamespace = "my.authentication.plugin";
6      var $autoRegister = true;
7      
8      function MyAuthenticationPlugin($sFilename = null) 
9      {
10         $res = parent::KTPlugin($sFilename);
11         $this->sFriendlyName = _kt('My Authentication Plugin');
12         return $res;
13     }
14 
15     function setup()
16     {
17          $this->registerAuthenticationProvider(
18             _kt('My Authentication'),
19             'MyAuthenticationProvider', 'my.authentication.provider', 'MyAuthentication.php');
20     }
21 }
22 
23 $oPluginRegistry =& KTPluginRegistry::getSingleton();
24 $oPluginRegistry->registerPlugin('MyAuthenticationPlugin', 'my.authentication.plugin', __FILE__);

Administration

Now you need to log in as the administrator.

Go to Miscellaneous -> Manage Plugins.

Select Reread Plugins.

Go to Users and Groups -> Authentication -> Authentication Sources.

Finally, Add an authentication source. Select My Authentication and give it a name like My First Authenticator.

Personal tools