TYPO3 CMS  TYPO3_7-6
SaltFactory.php
Go to the documentation of this file.
1 <?php
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 [
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 
82  public static function getSaltingInstance($saltedHash = '', $mode = TYPO3_MODE)
83  {
84  // Creating new instance when
85  // * no instance existing
86  // * a salted hash given to determine salted hashing method from
87  // * a NULL parameter given to reset instance back to default method
88  if (!is_object(self::$instance) || !empty($saltedHash) || $saltedHash === null) {
89  // Determine method by checking the given hash
90  if (!empty($saltedHash)) {
91  $result = self::determineSaltingHashingMethod($saltedHash, $mode);
92  if (!$result) {
93  self::$instance = null;
94  }
95  } else {
97  $availableClasses = static::getRegisteredSaltedHashingMethods();
98  self::$instance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($availableClasses[$classNameToUse]);
99  }
100  }
101  return self::$instance;
102  }
103 
113  public static function determineSaltingHashingMethod($saltedHash, $mode = TYPO3_MODE)
114  {
115  $registeredMethods = static::getRegisteredSaltedHashingMethods();
117  $defaultReference = $registeredMethods[$defaultClassName];
118  unset($registeredMethods[$defaultClassName]);
119  // place the default method first in the order
120  $registeredMethods = [$defaultClassName => $defaultReference] + $registeredMethods;
121  $methodFound = false;
122  foreach ($registeredMethods as $method) {
123  $objectInstance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($method);
124  if ($objectInstance instanceof SaltInterface) {
125  $methodFound = $objectInstance->isValidSaltedPW($saltedHash);
126  if ($methodFound) {
127  self::$instance = $objectInstance;
128  break;
129  }
130  }
131  }
132  return $methodFound;
133  }
134 
141  public static function setPreferredHashingMethod($resource)
142  {
143  self::$instance = null;
144  $objectInstance = \TYPO3\CMS\Core\Utility\GeneralUtility::getUserObj($resource);
145  if (is_object($objectInstance) && is_subclass_of($objectInstance, \TYPO3\CMS\Saltedpasswords\Salt\AbstractSalt::class)) {
146  self::$instance = $objectInstance;
147  }
148  return self::$instance;
149  }
150 }
static determineSaltingHashingMethod($saltedHash, $mode=TYPO3_MODE)
static getSaltingInstance($saltedHash='', $mode=TYPO3_MODE)
Definition: SaltFactory.php:82
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']