‪TYPO3CMS  ‪main
NotCurrentPasswordValidator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 
31 {
32  public function ‪validate(string $password, ?‪ContextData $contextData = null): bool
33  {
34  if (!$contextData) {
35  throw new \RuntimeException('ContextData must be supplied to validator.', 1662808782);
36  }
37 
38  if (in_array($contextData->getLoginMode(), ['FE', 'BE'], true)) {
39  $isValid = !$this->‪isCurrentPassword($password, $contextData);
40  } else {
41  throw new \RuntimeException('Unsupported loginMode provided. Ensure, that loginMode is either "FE" or "BE".', 1649846004);
42  }
43 
44  return $isValid;
45  }
46 
47  public function ‪initializeRequirements(): void
48  {
49  $this->‪addRequirement(
50  'notCurrentPassword',
51  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:requirement.notCurrentPassword')
52  );
53  }
54 
58  protected function ‪isCurrentPassword(string $password, ‪ContextData $contextData): bool
59  {
60  $result = false;
61  $saltFactory = GeneralUtility::makeInstance(PasswordHashFactory::class);
62  try {
63  $hashInstance = $saltFactory->get($contextData->‪getCurrentPasswordHash(), $contextData->‪getLoginMode());
64  $result = $hashInstance->checkPassword(
65  $password,
66  $contextData->‪getCurrentPasswordHash()
67  );
68  } catch (‪InvalidPasswordHashException $e) {
69  // Since the password will be updated, we silently ignore, if current password hash can not be checked
70  }
71 
72  if ($result) {
73  $this->‪addErrorMessage(
74  'notCurrentPassword',
75  $this->‪getLanguageService()->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:error.notCurrentPassword')
76  );
77  }
78 
79  return $result;
80  }
81 }
‪TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory
Definition: PasswordHashFactory.php:27
‪TYPO3\CMS\Core\PasswordPolicy\Validator\NotCurrentPasswordValidator\isCurrentPassword
‪isCurrentPassword(string $password, ContextData $contextData)
Definition: NotCurrentPasswordValidator.php:58
‪TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException
Definition: InvalidPasswordHashException.php:25
‪TYPO3\CMS\Core\PasswordPolicy\Validator\NotCurrentPasswordValidator\validate
‪validate(string $password, ?ContextData $contextData=null)
Definition: NotCurrentPasswordValidator.php:32
‪TYPO3\CMS\Core\PasswordPolicy\Validator\AbstractPasswordValidator\addErrorMessage
‪addErrorMessage(string $identifier, string $errorMessage)
Definition: AbstractPasswordValidator.php:93
‪TYPO3\CMS\Core\PasswordPolicy\Validator\NotCurrentPasswordValidator
Definition: NotCurrentPasswordValidator.php:31
‪TYPO3\CMS\Core\PasswordPolicy\Validator\Dto\ContextData\getLoginMode
‪getLoginMode()
Definition: ContextData.php:47
‪TYPO3\CMS\Core\PasswordPolicy\Validator\NotCurrentPasswordValidator\initializeRequirements
‪initializeRequirements()
Definition: NotCurrentPasswordValidator.php:47
‪TYPO3\CMS\Core\PasswordPolicy\Validator\Dto\ContextData\getCurrentPasswordHash
‪getCurrentPasswordHash()
Definition: ContextData.php:52
‪TYPO3\CMS\Core\PasswordPolicy\Validator\AbstractPasswordValidator\getLanguageService
‪getLanguageService()
Definition: AbstractPasswordValidator.php:105
‪TYPO3\CMS\Core\PasswordPolicy\Validator\AbstractPasswordValidator
Definition: AbstractPasswordValidator.php:31
‪TYPO3\CMS\Core\PasswordPolicy\Validator
Definition: AbstractPasswordValidator.php:18
‪TYPO3\CMS\Core\PasswordPolicy\Validator\AbstractPasswordValidator\addRequirement
‪addRequirement(string $identifier, string $message)
Definition: AbstractPasswordValidator.php:73
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Core\PasswordPolicy\Validator\Dto\ContextData
Definition: ContextData.php:28