‪TYPO3CMS  ‪main
AuthenticationService.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 
20 use Psr\Http\Message\ServerRequestInterface;
21 use Symfony\Component\Mailer\Exception\TransportException;
22 use Symfony\Component\Mime\Address;
23 use Symfony\Component\Mime\Exception\RfcComplianceException;
24 use Symfony\Component\Mime\RawMessage;
32 use TYPO3\CMS\Install\Service\SessionService;
33 
39 {
41 
42  public function ‪__construct(protected readonly ‪MailerInterface $mailer)
43  {
44  $templateConfiguration = ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'];
45  $templateConfiguration['templateRootPaths'][20] = 'EXT:install/Resources/Private/Templates/Email/';
46  $this->templatePaths = new ‪TemplatePaths($templateConfiguration);
47  }
48 
56  public function ‪loginWithPassword($password, ServerRequestInterface $request, SessionService $session): bool
57  {
58  $validPassword = false;
59  if ($password !== null && $password !== '') {
60  $installToolPassword = ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['installToolPassword'];
61  $hashFactory = GeneralUtility::makeInstance(PasswordHashFactory::class);
62  // Throws an InvalidPasswordHashException if no hash mechanism for the stored password is found
63  $hashInstance = $hashFactory->get($installToolPassword, 'BE');
64  // @todo: This code should check required hash updates and update the hash if needed
65  $validPassword = $hashInstance->checkPassword($password, $installToolPassword);
66  }
67  if ($validPassword) {
68  $session->setAuthorized();
69  $this->‪sendLoginSuccessfulMail($request);
70  return true;
71  }
72  $this->‪sendLoginFailedMail($request);
73  return false;
74  }
75 
79  protected function ‪sendLoginSuccessfulMail(ServerRequestInterface $request)
80  {
81  $warningEmailAddress = ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'];
82  if (!$warningEmailAddress) {
83  return;
84  }
85  $email = GeneralUtility::makeInstance(FluidEmail::class, $this->templatePaths);
86  $email
87  ->to($warningEmailAddress)
88  ->subject('Install Tool Login at \'' . ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '\'')
89  ->from(new Address($this->‪getSenderEmailAddress(), $this->‪getSenderEmailName()))
90  ->setTemplate('Security/InstallToolLogin')
91  ->setRequest($request);
92  $this->‪sendEmail($email);
93  }
94 
98  protected function ‪sendLoginFailedMail(ServerRequestInterface $request)
99  {
100  $warningEmailAddress = ‪$GLOBALS['TYPO3_CONF_VARS']['BE']['warning_email_addr'];
101  if (!$warningEmailAddress) {
102  return;
103  }
104  $formValues = $request->getParsedBody()['install'] ?? $request->getQueryParams()['install'] ?? null;
105  $email = GeneralUtility::makeInstance(FluidEmail::class, $this->templatePaths);
106  $email
107  ->to($warningEmailAddress)
108  ->subject('Install Tool Login ATTEMPT at \'' . ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'] . '\'')
109  ->from(new Address($this->‪getSenderEmailAddress(), $this->‪getSenderEmailName()))
110  ->setTemplate('Security/InstallToolLoginAttempt')
111  ->assign('lastCharactersOfPassword', substr(md5($formValues['password']), -5))
112  ->setRequest($request);
113  $this->‪sendEmail($email);
114  }
115 
121  protected function ‪sendEmail(RawMessage $email): void
122  {
123  try {
124  $this->mailer->send($email);
125  } catch (TransportException $e) {
126  $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
127  $logger->warning('Could not send notification email to ' . $this->‪getSenderEmailAddress() . ' due to mailer settings error', [
128  'recipientList' => $this->‪getSenderEmailAddress(),
129  'exception' => $e,
130  ]);
131  } catch (RfcComplianceException $e) {
132  $logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
133  $logger->warning('Could not send notification email to ' . $this->‪getSenderEmailAddress() . ' due to invalid email address', [
134  'recipientList' => $this->‪getSenderEmailAddress(),
135  'exception' => $e,
136  ]);
137  }
138  }
139 
147  protected function ‪getSenderEmailAddress()
148  {
150  }
151 
159  protected function ‪getSenderEmailName()
160  {
161  return ‪MailUtility::getSystemFromName() ?: 'TYPO3 CMS install tool';
162  }
163 }
‪TYPO3\CMS\Install\Authentication\AuthenticationService\sendLoginFailedMail
‪sendLoginFailedMail(ServerRequestInterface $request)
Definition: AuthenticationService.php:98
‪TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory
Definition: PasswordHashFactory.php:27
‪TYPO3\CMS\Fluid\View\TemplatePaths
Definition: TemplatePaths.php:39
‪TYPO3\CMS\Install\Authentication
Definition: AuthenticationService.php:18
‪TYPO3\CMS\Install\Authentication\AuthenticationService\sendEmail
‪sendEmail(RawMessage $email)
Definition: AuthenticationService.php:121
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromAddress
‪static string getSystemFromAddress()
Definition: MailUtility.php:79
‪TYPO3\CMS\Core\Mail\MailerInterface
Definition: MailerInterface.php:28
‪TYPO3\CMS\Install\Authentication\AuthenticationService\__construct
‪__construct(protected readonly MailerInterface $mailer)
Definition: AuthenticationService.php:42
‪TYPO3\CMS\Install\Authentication\AuthenticationService\$templatePaths
‪TemplatePaths $templatePaths
Definition: AuthenticationService.php:40
‪TYPO3\CMS\Core\Mail\FluidEmail
Definition: FluidEmail.php:35
‪TYPO3\CMS\Core\Utility\MailUtility
Definition: MailUtility.php:26
‪TYPO3\CMS\Install\Authentication\AuthenticationService\getSenderEmailName
‪string getSenderEmailName()
Definition: AuthenticationService.php:159
‪TYPO3\CMS\Install\Authentication\AuthenticationService\loginWithPassword
‪bool loginWithPassword($password, ServerRequestInterface $request, SessionService $session)
Definition: AuthenticationService.php:56
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Log\LogManager
Definition: LogManager.php:33
‪TYPO3\CMS\Install\Authentication\AuthenticationService
Definition: AuthenticationService.php:39
‪TYPO3\CMS\Install\Authentication\AuthenticationService\getSenderEmailAddress
‪string getSenderEmailAddress()
Definition: AuthenticationService.php:147
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Install\Authentication\AuthenticationService\sendLoginSuccessfulMail
‪sendLoginSuccessfulMail(ServerRequestInterface $request)
Definition: AuthenticationService.php:79
‪TYPO3\CMS\Core\Utility\MailUtility\getSystemFromName
‪static string null getSystemFromName()
Definition: MailUtility.php:59