[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Authentication/Password/ -> ChainedHandler.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright  (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license    GNU General Public License version 2 or later; see LICENSE.txt
   8   */
   9  
  10  namespace Joomla\CMS\Authentication\Password;
  11  
  12  use Joomla\Authentication\Password\HandlerInterface;
  13  
  14  // phpcs:disable PSR1.Files.SideEffects
  15  \defined('JPATH_PLATFORM') or die;
  16  // phpcs:enable PSR1.Files.SideEffects
  17  
  18  /**
  19   * Password handler supporting testing against a chain of handlers
  20   *
  21   * @since  4.0.0
  22   */
  23  class ChainedHandler implements HandlerInterface, CheckIfRehashNeededHandlerInterface
  24  {
  25      /**
  26       * The password handlers in use by this chain.
  27       *
  28       * @var    HandlerInterface[]
  29       * @since  4.0.0
  30       */
  31      private $handlers = [];
  32  
  33      /**
  34       * Add a handler to the chain
  35       *
  36       * @param   HandlerInterface  $handler  The password handler to add
  37       *
  38       * @return  void
  39       *
  40       * @since   4.0.0
  41       */
  42      public function addHandler(HandlerInterface $handler)
  43      {
  44          $this->handlers[] = $handler;
  45      }
  46  
  47      /**
  48       * Check if the password requires rehashing
  49       *
  50       * @param   string  $hash  The password hash to check
  51       *
  52       * @return  boolean
  53       *
  54       * @since   4.0.0
  55       */
  56      public function checkIfRehashNeeded(string $hash): bool
  57      {
  58          foreach ($this->handlers as $handler) {
  59              if ($handler instanceof CheckIfRehashNeededHandlerInterface && $handler->isSupported() && $handler->checkIfRehashNeeded($hash)) {
  60                  return true;
  61              }
  62          }
  63  
  64          return false;
  65      }
  66  
  67      /**
  68       * Generate a hash for a plaintext password
  69       *
  70       * @param   string  $plaintext  The plaintext password to validate
  71       * @param   array   $options    Options for the hashing operation
  72       *
  73       * @return  void
  74       *
  75       * @since   4.0.0
  76       * @throws  \RuntimeException
  77       */
  78      public function hashPassword($plaintext, array $options = [])
  79      {
  80          throw new \RuntimeException('The chained password handler cannot be used to hash a password');
  81      }
  82  
  83      /**
  84       * Check that the password handler is supported in this environment
  85       *
  86       * @return  boolean
  87       *
  88       * @since   4.0.0
  89       */
  90      public static function isSupported()
  91      {
  92          return true;
  93      }
  94  
  95      /**
  96       * Validate a password
  97       *
  98       * @param   string  $plaintext  The plain text password to validate
  99       * @param   string  $hashed     The password hash to validate against
 100       *
 101       * @return  boolean
 102       *
 103       * @since   4.0.0
 104       */
 105      public function validatePassword($plaintext, $hashed)
 106      {
 107          foreach ($this->handlers as $handler) {
 108              if ($handler->isSupported() && $handler->validatePassword($plaintext, $hashed)) {
 109                  return true;
 110              }
 111          }
 112  
 113          return false;
 114      }
 115  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer