TYPO3  7.6
SaltFactory.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Saltedpasswords\Salt;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 {
29  protected static $instance = null;
30 
37  public static function getRegisteredSaltedHashingMethods()
38  {
39  $saltMethods = static::getDefaultSaltMethods();
40  if (isset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/saltedpasswords']['saltMethods'])) {
41  $configuredMethods = (array)$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/saltedpasswords']['saltMethods'];
42  if (!empty($configuredMethods)) {
43  if (isset($configuredMethods[0])) {
44  // ensure the key of the array is not numeric, but a class name
45  foreach ($configuredMethods as $method) {
46  $saltMethods[$method] = $method;
47  }
48  } else {
49  $saltMethods = array_merge($saltMethods, $configuredMethods);
50  }
51  }
52  }
53  return $saltMethods;
54  }
55 
61  protected static function getDefaultSaltMethods()
62  {
63  return array(
64  \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt::class => \TYPO3\CMS\Saltedpasswords\Salt\Md5Salt::class,
65  \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt::class => \TYPO3\CMS\Saltedpasswords\Salt\BlowfishSalt::class,
66  \TYPO3\CMS\Saltedpasswords\Salt\PhpassSalt::class => \TYPO3\CMS\Saltedpasswords\Salt\PhpassSalt::class
67  );
68  }
69 
70 
83  public static function getSaltingInstance($saltedHash = '', $mode = TYPO3_MODE)
84  {
85  // Creating new instance when
86  // * no instance existing
87  // * a salted hash given to determine salted hashing method from
88  // * a NULL parameter given to reset instance back to default method
89  if (!is_object(self::$instance) || !empty($saltedHash) || $saltedHash === null) {
90  // Determine method by checking the given hash
91  if (!empty($saltedHash)) {
92  $result = self::determineSaltingHashingMethod($saltedHash, $mode);
93  if (!$result) {
94  self::$instance = null;
95  }
96  } else {
97  $classNameToUse = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::getDefaultSaltingHashingMethod($mode);
98  $availableClasses = static::getRegisteredSaltedHashingMethods();
99  self::$instance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($availableClasses[$classNameToUse]);
100  }
101  }
102  return self::$instance;
103  }
104 
114  public static function determineSaltingHashingMethod($saltedHash, $mode = TYPO3_MODE)
115  {
116  $registeredMethods = static::getRegisteredSaltedHashingMethods();
117  $defaultClassName = \TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility::getDefaultSaltingHashingMethod($mode);
118  $defaultReference = $registeredMethods[$defaultClassName];
119  unset($registeredMethods[$defaultClassName]);
120  // place the default method first in the order
121  $registeredMethods = array($defaultClassName => $defaultReference) + $registeredMethods;
122  $methodFound = false;
123  foreach ($registeredMethods as $method) {
124  $objectInstance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($method);
125  if ($objectInstance instanceof SaltInterface) {
126  $methodFound = $objectInstance->isValidSaltedPW($saltedHash);
127  if ($methodFound) {
128  self::$instance = $objectInstance;
129  break;
130  }
131  }
132  }
133  return $methodFound;
134  }
135 
142  public static function setPreferredHashingMethod($resource)
143  {
144  self::$instance = null;
145  $objectInstance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($resource);
146  if (is_object($objectInstance) && is_subclass_of($objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt::class)) {
147  self::$instance = $objectInstance;
148  }
149  return self::$instance;
150  }
151 }