CoreFiles by mySites.guru
ChainedHandler.php116 lines, 2,469 bytes, from Joomla_4.0.0-alpha3-Alpha-Full_Package.zip
Raw History Compare with 4.0.0-alpha2
md5
f7cecd29a7cafc7f251cc7997b002c61
sha1
e1c2f3085e0947f61d205349b630588cf4effab4
sha256
febceb05bd2589019b84c21a1a43096902f7557d2e360a0dd10b47781d975fbb
<?php
/**
 * Joomla! Content Management System
 *
 * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

namespace Joomla\CMS\Authentication\Password;

defined('JPATH_PLATFORM') or die;

use Joomla\Authentication\Password\HandlerInterface;

/**
 * Password handler supporting testing against a chain of handlers
 *
 * @since  4.0.0
 */
class ChainedHandler implements HandlerInterface, CheckIfRehashNeededHandlerInterface
{
	/**
	 * The password handlers in use by this chain.
	 *
	 * @var    HandlerInterface[]
	 * @since  4.0.0
	 */
	private $handlers = [];

	/**
	 * Add a handler to the chain
	 *
	 * @param   HandlerInterface  $handler  The password handler to add
	 *
	 * @return  void
	 *
	 * @since   4.0.0
	 */
	public function addHandler(HandlerInterface $handler)
	{
		$this->handlers[] = $handler;
	}

	/**
	 * Check if the password requires rehashing
	 *
	 * @param   string  $hash  The password hash to check
	 *
	 * @return  boolean
	 *
	 * @since   4.0.0
	 */
	public function checkIfRehashNeeded(string $hash): bool
	{
		foreach ($this->handlers as $handler)
		{
			if ($handler instanceof CheckIfRehashNeededHandlerInterface && $handler->isSupported() && $handler->checkIfRehashNeeded($hash))
			{
				return true;
			}
		}

		return false;
	}

	/**
	 * Generate a hash for a plaintext password
	 *
	 * @param   string  $plaintext  The plaintext password to validate
	 * @param   array   $options    Options for the hashing operation
	 *
	 * @return  string
	 *
	 * @since   4.0.0
	 * @throws  \RuntimeException
	 */
	public function hashPassword($plaintext, array $options = [])
	{
		throw new \RuntimeException('The chained password handler cannot be used to hash a password');
	}

	/**
	 * Check that the password handler is supported in this environment
	 *
	 * @return  boolean
	 *
	 * @since   4.0.0
	 */
	public static function isSupported()
	{
		return true;
	}

	/**
	 * Validate a password
	 *
	 * @param   string  $plaintext  The plain text password to validate
	 * @param   string  $hashed     The password hash to validate against
	 *
	 * @return  boolean
	 *
	 * @since   4.0.0
	 */
	public function validatePassword($plaintext, $hashed)
	{
		foreach ($this->handlers as $handler)
		{
			if ($handler->isSupported() && $handler->validatePassword($plaintext, $hashed))
			{
				return true;
			}
		}

		return false;
	}
}

The same content is in 3 releases, Joomla 4.0.0-alpha3 to 4.0.0-alpha6. See them all, or every version of this file.