Difference between revisions of "Authentication"

From Dolibarr ERP CRM Wiki
Jump to navigation Jump to search
Line 8: Line 8:
 
= Processus =
 
= Processus =
  
Le processus démarre par l'appel de la page que l'on souhaite voir. Par exemple, la page d'accueil htdocs/index.php. Mais ce n'est pas ce fichier assure la demande d'authentification. En fait toute page de Dolibarr inclut un fichier pre.inc.php qui lui même inclut  le fichier main.php qui inclut master.php.
+
Process start when you call the page you want to see. For example index.php (htdocs/index.php). But this is not this file that ask for authentication. Each Dolibarr php page include at the beginning a file called pre.inc.php and this file include a file called main.php that include master.php.
Nous avons donc:
+
So we have:
<pre>
+
<source lang="php">
 
  <index.php>
 
  <index.php>
 
   <pre.inc.php>
 
   <pre.inc.php>
Line 18: Line 18:
 
       </master.inc.php>
 
       </master.inc.php>
 
       #2#
 
       #2#
 +
      if (! isset($_SESSION["dol_login"]))
 +
      {
 +
        if (isset($_POST["username"]))
 +
        {
 +
          ...
 +
          // if user and pass are ok, we set $login
 +
          $login=
 +
        }
 +
        if (! $login)
 +
        {
 +
  // We show login page
 +
  include_once(DOL_DOCUMENT_ROOT."/lib/security.lib.php");
 +
  dol_loginfunction($langs,$conf,$mysoc);
 +
  exit;
 +
        }
 +
        ...
 +
      }
 
     </main.inc.php>
 
     </main.inc.php>
 
   </pre.inc.php>
 
   </pre.inc.php>
 
  </index.php>
 
  </index.php>
</pre>
+
</source>
  
Le #1# représente le chargement de tout un tas de librairie que nous utiliserons par la suite, ainsi que l'initialisation du contexte d'exécution du code PHP (langue, configuration, utilisateur vierge).
+
The #1# represents the loading of a lot of libraries that we will use later, but also initialize objects and variables containing the running PHP context (load configuration file, load language files and create an empty user not yet authenticated).
  
Le #2# représente l'exécution de code propre à l'authentification: La verification que l'on ait dans une session loguée et si ce n'est pas le cas l'affichage de l'écran de login. C'est la que l'objet utilisateur est initialisé:
+
The #2# represents the code for authentication: Program check if we are inside a logged session (this means the $_SESSION["dol_login"] exists). If not, we check if we receives data from a login and password form. After first call, this is not the case since we haven't yet show the login form. So we continue and $login is false so we just output the login HTML form and we end the script.
  
L'exécution du login, elle, se présente comme suit:
+
After submission of login, the same page (so still index.php is called), we still go inside #1#, then #2# and now the $_POST["username"] is defined. So we check if user and pass are ok (check into database, LDAP, it depends on $dolibarr_main_authentication value in config file). If it's ok, the $login variable is set, so we don't show the form anymore and we set the $_SESSION["dol_login"] so next time we call a page, we never go inside the "if (! isset($_SESSION["dol_login"]))".
 
 
<source lang="php">
 
$authmode=array('http','dolibarr');
 
if (isset($dolibarr_auto_user)) $authmode=array('auto');
 
// Si la demande du login a déjà eu lieu, on le récupère depuis la session
 
// sinon appel du module qui réalise sa demande.
 
// A l'issu de cette phase, la variable $login sera définie.
 
$login='';
 
if (! session_id() || ! isset($_SESSION["dol_user"]))
 
{
 
    # Procédure de login. Affiche page login #
 
}
 
else
 
{
 
    // On est déjà en session
 
    $login=$_SESSION["dol_user"];
 
}
 
// Charge l'objet user depuis son login
 
$result=$user->fetch($login);
 
if ($result <= 0)
 
{
 
    dolibarr_print_error($db,$langs->trans("ErrorCantLoadUserFromDolibarrDatabase"));
 
    exit;
 
}
 
// Est-ce une nouvelle session
 
if (! isset($_SESSION["dol_user"]))
 
{
 
    // Nouvelle session pour ce login
 
    dolibarr_syslog("New session in DOLSESSID_".$dolibarr_main_db_name.": ".session_id());
 
    $user->update_last_login_date();
 
    $_SESSION["dol_user"]=$user;
 
}
 
</source>
 
 
 
Mais analysons plus en détail le code d'appel de la méthode de vérification du couple user/password (il y a plusieurs méthodes disponibles, donc plusieurs appels possibles et qui devraient être mutuellement exclusifs).
 
 
 
<source lang="php">
 
    session_name("DOLSESSID_".$dolibarr_main_db_name);
 
    session_start();
 
    // Si on rentre ici suite a soumission d'un couple user/password alors
 
    // Selon la valeur dolibarr_main_authentication, on appelle la fonction
 
    // dans le bon fichier qui verifie si un couple user/mot de passe est correcte
 
    // Sinon, on affiche la page de login
 
</source>
 
  
 
= Les modules de login =
 
= Les modules de login =

Revision as of 17:14, 25 August 2009

En verysmall.png Page waiting for translation. To translate, create an account, go back and clic on "Modify".
Fr verysmall.png Page en attente de traduction. Pour traduire, créez un compte, revenez et cliquez sur "Modifier".
Es verysmall.png Página a traducir. Para traducirla, cree una cuenta, vuelva a la página y haga clic en "editar".
It verysmall.png Pagina da tradurre. Per tradurla, crea un utente, torna indietro e clicca su "modifica".
Pt verysmall.png Página a aguardar por tradução. Para traduzir, crie uma conta, volte atrás e clique em "Modificar".
De verysmall.png Seite wartet auf Übersetzung. Um Übersetzung zu erstellen, richte einen Account ein, geh zurück und klicke auf "bearbeiten".
Zh verysmall.png 页面等待翻译。若要翻译本页,先创建一个帐户、登录并返回本页后单击“编辑”。

Introduction

This page describe how the Dolibarr authentication process works. You must be familiar with PHP session and redirect to understand correctly what happen.

Processus

Process start when you call the page you want to see. For example index.php (htdocs/index.php). But this is not this file that ask for authentication. Each Dolibarr php page include at the beginning a file called pre.inc.php and this file include a file called main.php that include master.php. So we have:

 <index.php>
   <pre.inc.php>
     <main.inc.php>
       <master.inc.php>
         #1#
       </master.inc.php>
       #2#
       if (! isset($_SESSION["dol_login"]))
       {
         if (isset($_POST["username"]))
         {
           ...
           // if user and pass are ok, we set $login
           $login=
         }
         if (! $login)
         { 
	   // We show login page
	   include_once(DOL_DOCUMENT_ROOT."/lib/security.lib.php");
	   dol_loginfunction($langs,$conf,$mysoc);
	   exit;
         }
         ...
       }
     </main.inc.php>
   </pre.inc.php>
 </index.php>

The #1# represents the loading of a lot of libraries that we will use later, but also initialize objects and variables containing the running PHP context (load configuration file, load language files and create an empty user not yet authenticated).

The #2# represents the code for authentication: Program check if we are inside a logged session (this means the $_SESSION["dol_login"] exists). If not, we check if we receives data from a login and password form. After first call, this is not the case since we haven't yet show the login form. So we continue and $login is false so we just output the login HTML form and we end the script.

After submission of login, the same page (so still index.php is called), we still go inside #1#, then #2# and now the $_POST["username"] is defined. So we check if user and pass are ok (check into database, LDAP, it depends on $dolibarr_main_authentication value in config file). If it's ok, the $login variable is set, so we don't show the form anymore and we set the $_SESSION["dol_login"] so next time we call a page, we never go inside the "if (! isset($_SESSION["dol_login"]))".

Les modules de login

Les modules de login sont les fichiers qui contiennent les fonctions qui controlent la validite d'un couple user/password. Il y a un fichier par module. Chaque fichier assure un type de controle différent.

  • Le fichier htdocs/include/login/functions_http.php controle la validite du couple user/mot de passe par une authentification de type http Basic.
  • Le fichier htdocs/include/login/functions_ldap.php verifie la validite d'un couple user/mot de passe dans un annuaire LDAP.
  • Le fichier htdocs/include/login/functions_dolibarr.php veririe la validite d'un couple user/mot de passe dans la base de donnee Dolibarr.

Chaque fichier contient en fait uniquement une fonction check_user_password_xxx mais Dolibarr ne va en utiliser qu'un. Ce sera celui dont la valeur xxx correspond a la valeur de la variable dolibarr_main_authentication. Dans ce fichier, Dolibarr sollicite la seule fonction qui s'y trouve en envoyant comme parametres le user et mot de passe.La fonction renvoie vrai si le couple est valide.