Source for file cookie.php

Documentation is available at cookie.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Authentication.cookie
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * Joomla Authentication plugin
  14.  *
  15.  * @package     Joomla.Plugin
  16.  * @subpackage  Authentication.cookie
  17.  * @since       3.2
  18.  */
  19. {
  20.     /**
  21.      * Application object
  22.      *
  23.      * @var    JApplicationCms 
  24.      * @since  3.2
  25.      */
  26.     protected $app;
  27.  
  28.     /**
  29.      * Database object
  30.      *
  31.      * @var    JDatabaseDriver 
  32.      * @since  3.2
  33.      */
  34.     protected $db;
  35.  
  36.     /**
  37.      * This method should handle any authentication and report back to the subject
  38.      *
  39.      * @param   array   $credentials  Array holding the user credentials
  40.      * @param   array   $options      Array of extra options
  41.      * @param   object  &$response    Authentication response object
  42.      *
  43.      * @return  boolean 
  44.      *
  45.      * @since   3.2
  46.      */
  47.     public function onUserAuthenticate($credentials$options&$response)
  48.     {
  49.         // No remember me for admin
  50.         if ($this->app->isAdmin())
  51.         {
  52.             return false;
  53.         }
  54.  
  55.         JLoader::register('JAuthentication'JPATH_LIBRARIES '/joomla/user/authentication.php');
  56.  
  57.         $response->type 'Cookie';
  58.  
  59.         // We need to validate the cookie data because there may be no Remember Me plugin to do it.
  60.         // Create the cookie name and data.
  61.         $rememberArray JUserHelper::getRememberCookieData();
  62.  
  63.         if ($rememberArray == false)
  64.         {
  65.             return false;
  66.         }
  67.  
  68.         list($privateKey$series$uastring$rememberArray;
  69.  
  70.         // Find the matching record if it exists.
  71.         $query $this->db->getQuery(true)
  72.         ->select($this->db->quoteName(array('user_id''token''series''time''invalid')))
  73.         ->from($this->db->quoteName('#__user_keys'))
  74.         ->where($this->db->quoteName('series'' = ' $this->db->quote(base64_encode($series)))
  75.         ->where($this->db->quoteName('uastring'' = ' $this->db->quote($uastring))
  76.         ->order($this->db->quoteName('time'' DESC');
  77.  
  78.         $results $this->db->setQuery($query)->loadObjectList();
  79.  
  80.         $countResults count($results);
  81.  
  82.         if ($countResults !== 1)
  83.         {
  84.              $response->status  JAuthentication::STATUS_FAILURE;
  85.  
  86.              return;
  87.         }
  88.  
  89.         // We have a user with one cookie with a valid series and a corresponding record in the database.
  90.         else
  91.         {
  92.             if (substr($results[0]->token04=== '$2y$')
  93.             {
  94.                 if (JCrypt::hasStrongPasswordSupport())
  95.                 {
  96.                     $match password_verify($privateKey$results[0]->token);
  97.                 }
  98.             }
  99.             else
  100.             {
  101.                 if (JCrypt::timingSafeCompare($results[0]->token$privateKey))
  102.                 {
  103.                     $match true;
  104.                 }
  105.             }
  106.  
  107.             if (empty($match))
  108.             {
  109.                 JUserHelper::invalidateCookie($results[0]->user_id$uastring);
  110.                 JLog::add(JText::sprintf('PLG_SYSTEM_REMEMBER_ERROR_LOG_LOGIN_FAILED'$user->username)JLog::WARNING'security');
  111.                 $response->status  JAuthentication::STATUS_FAILURE;
  112.  
  113.                 return false;
  114.             }
  115.         }
  116.  
  117.         // Set cookie params.
  118.         if (!empty($options['lifetime']&& !empty($options['length']&& !empty($options['secure']))
  119.         {
  120.             $response->lifetime $options['lifetime'];
  121.             $response->length $options['length'];
  122.             $response->secure $options['secure'];
  123.         }
  124.  
  125.         // Make sure there really is a user with this name and get the data for the session.
  126.         $query $this->db->getQuery(true)
  127.             ->select($this->db->quoteName(array('id''username''password')))
  128.             ->from($this->db->quoteName('#__users'))
  129.             ->where($this->db->quoteName('username'' = ' $this->db->quote($credentials['username']));
  130.  
  131.         $result $this->db->setQuery($query)->loadObject();
  132.  
  133.         if ($result)
  134.         {
  135.             // Bring this in line with the rest of the system
  136.             $user JUser::getInstance($result->id);
  137.             $cookieName JUserHelper::getShortHashedUserAgent();
  138.  
  139.             // If there is no cookie, bail out
  140.             if (!$this->app->input->cookie->get($cookieName))
  141.             {
  142.                 return;
  143.             }
  144.  
  145.             // Set response data.
  146.             $response->username $result->username;
  147.             $response->email    $user->email;
  148.             $response->fullname $user->name;
  149.             $response->password $result->password;
  150.             $response->language $user->getParam('language');
  151.  
  152.             // Set response status.
  153.             $response->status        JAuthentication::STATUS_SUCCESS;
  154.             $response->error_message '';
  155.         }
  156.         else
  157.         {
  158.             $response->status        JAuthentication::STATUS_FAILURE;
  159.             $response->error_message JText::_('JGLOBAL_AUTH_NO_USER');
  160.         }
  161.     }
  162. }

Documentation generated on Tue, 19 Nov 2013 14:57:34 +0100 by phpDocumentor 1.4.3