‪TYPO3CMS  ‪main
CorePasswordValidator.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 
21 
34 {
35  public function ‪validate(string $password, ?‪ContextData $contextData = null): bool
36  {
37  $isValid = true;
38  $lang = $this->‪getLanguageService();
39 
40  if (strlen($password) < $this->‪getMinLength()) {
41  $this->‪addErrorMessage(
42  'minimumLength',
43  sprintf(
44  $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:error.minimumLength'),
45  $this->getMinLength()
46  )
47  );
48  $isValid = false;
49  }
50 
51  if ($this->‪isCheckEnabled('upperCaseCharacterRequired') &&
52  !$this->‪evaluatePasswordRequirement($password, 'upperCaseCharacterRequired')
53  ) {
54  $this->‪addErrorMessage(
55  'upperCaseCharacterRequired',
56  $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:error.upperCaseCharacterRequired')
57  );
58  $isValid = false;
59  }
60 
61  if ($this->‪isCheckEnabled('lowerCaseCharacterRequired') &&
62  !$this->‪evaluatePasswordRequirement($password, 'lowerCaseCharacterRequired')
63  ) {
64  $this->‪addErrorMessage(
65  'lowerCaseCharacterRequired',
66  $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:error.lowerCaseCharacterRequired')
67  );
68  $isValid = false;
69  }
70 
71  if ($this->‪isCheckEnabled('digitCharacterRequired') &&
72  !$this->‪evaluatePasswordRequirement($password, 'digitCharacterRequired')
73  ) {
74  $this->‪addErrorMessage(
75  'digitCharacterRequired',
76  $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:error.digitCharacterRequired')
77  );
78  $isValid = false;
79  }
80 
81  if ($this->‪isCheckEnabled('specialCharacterRequired') &&
82  !$this->‪evaluatePasswordRequirement($password, 'specialCharacterRequired')
83  ) {
84  $this->‪addErrorMessage(
85  'specialCharacterRequired',
86  $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:error.specialCharacterRequired')
87  );
88  $isValid = false;
89  }
90 
91  return $isValid;
92  }
93 
94  public function ‪initializeRequirements(): void
95  {
96  $lang = $this->‪getLanguageService();
97  $this->‪addRequirement(
98  'minimumLength',
99  sprintf(
100  $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:requirement.minimumLength'),
101  $this->getMinLength()
102  ),
103  );
104 
105  if ($this->‪isCheckEnabled('upperCaseCharacterRequired')) {
106  $this->‪addRequirement(
107  'upperCaseCharacterRequired',
108  $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:requirement.upperCaseCharacterRequired')
109  );
110  }
111 
112  if ($this->‪isCheckEnabled('lowerCaseCharacterRequired')) {
113  $this->‪addRequirement(
114  'lowerCaseCharacterRequired',
115  $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:requirement.lowerCaseCharacterRequired')
116  );
117  }
118 
119  if ($this->‪isCheckEnabled('digitCharacterRequired')) {
120  $this->‪addRequirement(
121  'digitCharacterRequired',
122  $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:requirement.digitCharacterRequired')
123  );
124  }
125 
126  if ($this->‪isCheckEnabled('specialCharacterRequired')) {
127  $this->‪addRequirement(
128  'specialCharacterRequired',
129  $lang->sL('LLL:EXT:core/Resources/Private/Language/locallang_password_policy.xlf:requirement.specialCharacterRequired')
130  );
131  }
132  }
133 
134  private function ‪getMinLength(): int
135  {
136  return (int)($this->options['minimumLength'] ?? 8);
137  }
138 
139  private function ‪isCheckEnabled(string $checkIdentifier): bool
140  {
141  return $this->options[$checkIdentifier] ?? false;
142  }
143 
147  private function ‪evaluatePasswordRequirement(string $password, string $requirement): bool
148  {
149  $result = true;
150 
151  $patterns = [
152  'upperCaseCharacterRequired' => '/[A-Z]/',
153  'lowerCaseCharacterRequired' => '/[a-z]/',
154  'digitCharacterRequired' => '/[0-9]/',
155  'specialCharacterRequired' => '/[^0-9a-z]/i',
156  ];
157 
158  if (isset($patterns[$requirement]) && !preg_match($patterns[$requirement], $password) > 0) {
159  $result = false;
160  }
161 
162  return $result;
163  }
164 }
‪TYPO3\CMS\Core\PasswordPolicy\Validator\CorePasswordValidator\initializeRequirements
‪initializeRequirements()
Definition: CorePasswordValidator.php:94
‪TYPO3\CMS\Core\PasswordPolicy\Validator\CorePasswordValidator\getMinLength
‪getMinLength()
Definition: CorePasswordValidator.php:134
‪TYPO3\CMS\Core\PasswordPolicy\Validator\AbstractPasswordValidator\addErrorMessage
‪addErrorMessage(string $identifier, string $errorMessage)
Definition: AbstractPasswordValidator.php:93
‪TYPO3\CMS\Core\PasswordPolicy\Validator\CorePasswordValidator\evaluatePasswordRequirement
‪evaluatePasswordRequirement(string $password, string $requirement)
Definition: CorePasswordValidator.php:147
‪TYPO3\CMS\Core\PasswordPolicy\Validator\CorePasswordValidator
Definition: CorePasswordValidator.php:34
‪TYPO3\CMS\Core\PasswordPolicy\Validator\CorePasswordValidator\validate
‪validate(string $password, ?ContextData $contextData=null)
Definition: CorePasswordValidator.php:35
‪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\PasswordPolicy\Validator\CorePasswordValidator\isCheckEnabled
‪isCheckEnabled(string $checkIdentifier)
Definition: CorePasswordValidator.php:139
‪TYPO3\CMS\Core\PasswordPolicy\Validator\Dto\ContextData
Definition: ContextData.php:28