‪TYPO3CMS  9.5
AuthenticationService.php
Go to the documentation of this file.
1 <?php
2 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 
18 use TYPO3\CMS\Core\Configuration\ConfigurationManager;
24 
30 {
34  protected ‪$sessionService;
35 
40  {
41  $this->sessionService = ‪$sessionService;
42  }
43 
50  public function ‪loginWithPassword($password = null): bool
51  {
52  $validPassword = false;
53  if ($password !== null && $password !== '') {
54  $installToolPassword = ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['installToolPassword'];
55  $hashFactory = GeneralUtility::makeInstance(PasswordHashFactory::class);
56  try {
57  $hashInstance = $hashFactory->get($installToolPassword, 'BE');
58  $validPassword = $hashInstance->checkPassword($password, $installToolPassword);
59  } catch (‪InvalidPasswordHashException $invalidPasswordHashException) {
60  // Given hash in global configuration is not a valid salted password
61  if (md5($password) === $installToolPassword) {
62  // Update configured install tool hash if it is still "MD5" and password matches
63  // @todo: This should be removed in TYPO3 v10.0 with a dedicated breaking patch
64  // @todo: Additionally, this code should check required hash updates and update the hash if needed
65  $hashInstance = $hashFactory->getDefaultHashInstance('BE');
66  $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
67  $configurationManager->setLocalConfigurationValueByPath(
68  'BE/installToolPassword',
69  $hashInstance->getHashedPassword($password)
70  );
71  $validPassword = true;
72  } else {
73  // Still no valid hash instance could be found. Probably the stored hash used a mechanism
74  // that is not available on current system. We throw the previous exception again to be
75  // handled on a higher level. The install tool will render an according exception message
76  // that links to the documentation.
77  throw $invalidPasswordHashException;
78  }
79  }
80  }
81  if ($validPassword) {
82  $this->sessionService->setAuthorized();
84  return true;
85  }
86  $this->‪sendLoginFailedMail();
87  return false;
88  }
89 
93  protected function ‪sendLoginSuccessfulMail()
94  {
95  $warningEmailAddress = ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'];
96  if ($warningEmailAddress) {
97  $mailMessage = GeneralUtility::makeInstance(MailMessage::class);
98  $mailMessage
99  ->addTo($warningEmailAddress)
100  ->setSubject('Install Tool Login at \'' . ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '\'')
101  ->addFrom($this->‪getSenderEmailAddress(), $this->‪getSenderEmailName())
102  ->setBody('There has been an Install Tool login at TYPO3 site'
103  . ' \'' . ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '\''
104  . ' (' . GeneralUtility::getIndpEnv('HTTP_HOST') . ')'
105  . ' from remote address \'' . GeneralUtility::getIndpEnv('REMOTE_ADDR') . '\'')
106  ->send();
107  }
108  }
109 
113  protected function ‪sendLoginFailedMail()
114  {
115  $formValues = GeneralUtility::_GP('install');
116  $warningEmailAddress = ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'];
117  if ($warningEmailAddress) {
118  $mailMessage = GeneralUtility::makeInstance(MailMessage::class);
119  $mailMessage
120  ->addTo($warningEmailAddress)
121  ->setSubject('Install Tool Login ATTEMPT at \'' . ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '\'')
122  ->addFrom($this->‪getSenderEmailAddress(), $this->‪getSenderEmailName())
123  ->setBody('There has been an Install Tool login attempt at TYPO3 site'
124  . ' \'' . ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '\''
125  . ' (' . GeneralUtility::getIndpEnv('HTTP_HOST') . ')'
126  . ' The last 5 characters of the MD5 hash of the password tried was \'' . substr(md5($formValues['password']), -5) . '\''
127  . ' remote address was \'' . GeneralUtility::getIndpEnv('REMOTE_ADDR') . '\'')
128  ->send();
129  }
130  }
131 
139  protected function ‪getSenderEmailAddress()
140  {
141  return !empty(‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'])
142  ? ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress']
143  : 'no-reply@example.com';
144  }
145 
153  protected function ‪getSenderEmailName()
154  {
155  return !empty(‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'])
156  ? ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName']
157  : 'TYPO3 CMS install tool';
158  }
159 }
‪TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory
Definition: PasswordHashFactory.php:25
‪TYPO3\CMS\Install\Authentication\AuthenticationService\$sessionService
‪SessionService $sessionService
Definition: AuthenticationService.php:33
‪TYPO3\CMS\Install\Authentication
Definition: AuthenticationService.php:3
‪TYPO3\CMS\Core\Crypto\PasswordHashing\InvalidPasswordHashException
Definition: InvalidPasswordHashException.php:22
‪TYPO3\CMS\Core\Mail\MailMessage
Definition: MailMessage.php:23
‪TYPO3\CMS\Install\Authentication\AuthenticationService\sendLoginFailedMail
‪sendLoginFailedMail()
Definition: AuthenticationService.php:112
‪TYPO3\CMS\Install\Authentication\AuthenticationService\getSenderEmailName
‪string getSenderEmailName()
Definition: AuthenticationService.php:152
‪TYPO3\CMS\Install\Authentication\AuthenticationService\__construct
‪__construct(SessionService $sessionService)
Definition: AuthenticationService.php:38
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Install\Authentication\AuthenticationService\loginWithPassword
‪bool loginWithPassword($password=null)
Definition: AuthenticationService.php:49
‪TYPO3\CMS\Install\Authentication\AuthenticationService
Definition: AuthenticationService.php:30
‪TYPO3\CMS\Install\Authentication\AuthenticationService\sendLoginSuccessfulMail
‪sendLoginSuccessfulMail()
Definition: AuthenticationService.php:92
‪TYPO3\CMS\Install\Authentication\AuthenticationService\getSenderEmailAddress
‪string getSenderEmailAddress()
Definition: AuthenticationService.php:138
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Install\Service\SessionService
Definition: SessionService.php:30