‪TYPO3CMS  11.5
MfaProviderPropertyManager.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\Log\LoggerAwareInterface;
21 use Psr\Log\LoggerAwareTrait;
27 
32 class ‪MfaProviderPropertyManager implements LoggerAwareInterface
33 {
34  use LoggerAwareTrait;
35 
37  protected array ‪$mfa;
38  protected string ‪$providerIdentifier;
39  protected array ‪$providerProperties;
40  protected const ‪DATABASE_FIELD_NAME = 'mfa';
41 
42  public function ‪__construct(‪AbstractUserAuthentication ‪$user, string $provider)
43  {
44  $this->user = ‪$user;
45  $this->mfa = json_decode(‪$user->user[self::DATABASE_FIELD_NAME] ?? '', true) ?? [];
46  $this->providerIdentifier = $provider;
47  $this->providerProperties = $this->mfa[$provider] ?? [];
48  }
49 
55  public function ‪hasProviderEntry(): bool
56  {
57  return isset($this->mfa[$this->providerIdentifier]);
58  }
59 
66  public function ‪hasProperty(string $key): bool
67  {
68  return isset($this->providerProperties[$key]);
69  }
70 
79  public function ‪getProperty(string $key, $default = null)
80  {
81  return $this->providerProperties[$key] ?? $default;
82  }
83 
89  public function ‪getProperties(): array
90  {
92  }
93 
102  public function ‪updateProperties(array $properties): bool
103  {
104  // This is to prevent provider data inconsistency
105  if (!$this->‪hasProviderEntry()) {
106  throw new \InvalidArgumentException(
107  'No entry for provider ' . $this->providerIdentifier . ' exists yet. Use createProviderEntry() instead.',
108  1613993188
109  );
110  }
111 
112  if (!isset($properties['updated'])) {
113  $properties['updated'] = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
114  }
115 
116  $this->providerProperties = array_replace($this->providerProperties, $properties);
118  return $this->‪storeProperties();
119  }
120 
129  public function ‪createProviderEntry(array $properties): bool
130  {
131  // This is to prevent unintentional overwriting of provider entries
132  if ($this->‪hasProviderEntry()) {
133  throw new \InvalidArgumentException(
134  'A entry for provider ' . $this->providerIdentifier . ' already exists. Use updateProperties() instead.',
135  1612781782
136  );
137  }
138 
139  if (!isset($properties['created'])) {
140  $properties['created'] = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
141  }
142 
143  if (!isset($properties['updated'])) {
144  $properties['updated'] = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('date', 'timestamp');
145  }
146 
147  $this->providerProperties = $properties;
149  return $this->‪storeProperties();
150  }
151 
158  public function ‪deleteProviderEntry(): bool
159  {
160  $this->providerProperties = [];
161  unset($this->mfa[$this->providerIdentifier]);
162  return $this->‪storeProperties();
163  }
164 
171  protected function ‪storeProperties(): bool
172  {
173  // encode the mfa properties to store them in the database and the user array
174  ‪$mfa = json_encode($this->mfa, JSON_THROW_ON_ERROR) ?: '';
175 
176  // Write back the updated mfa properties to the user array
177  $this->user->user[‪self::DATABASE_FIELD_NAME] = ‪$mfa;
178 
179  // Log MFA update
180  $this->logger->debug('MFA properties updated', [
181  'provider' => $this->providerIdentifier,
182  'user' => [
183  'uid' => $this->user->user[$this->user->userid_column],
184  'username' => $this->user->user[$this->user->username_column],
185  ],
186  ]);
187 
188  // Store updated mfa properties in the database
189  return (bool)GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->user->user_table)->update(
190  $this->user->user_table,
191  [self::DATABASE_FIELD_NAME => ‪$mfa],
192  [$this->user->userid_column => (int)$this->user->user[$this->user->userid_column]],
193  [self::DATABASE_FIELD_NAME => ‪Connection::PARAM_LOB]
194  );
195  }
196 
203  {
204  return ‪$this->user;
205  }
206 
212  public function ‪getIdentifier(): string
213  {
215  }
216 
225  {
226  return GeneralUtility::makeInstance(self::class, ‪$user, $provider->‪getIdentifier());
227  }
228 }
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\DATABASE_FIELD_NAME
‪const DATABASE_FIELD_NAME
Definition: MfaProviderPropertyManager.php:40
‪TYPO3\CMS\Core\Authentication\Mfa
Definition: MfaProviderInterface.php:18
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\$mfa
‪array $mfa
Definition: MfaProviderPropertyManager.php:37
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderManifestInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\getProperty
‪mixed null getProperty(string $key, $default=null)
Definition: MfaProviderPropertyManager.php:79
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderManifestInterface
Definition: MfaProviderManifestInterface.php:26
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\hasProviderEntry
‪bool hasProviderEntry()
Definition: MfaProviderPropertyManager.php:55
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\deleteProviderEntry
‪bool deleteProviderEntry()
Definition: MfaProviderPropertyManager.php:158
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\updateProperties
‪bool updateProperties(array $properties)
Definition: MfaProviderPropertyManager.php:102
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\__construct
‪__construct(AbstractUserAuthentication $user, string $provider)
Definition: MfaProviderPropertyManager.php:42
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\$providerProperties
‪array $providerProperties
Definition: MfaProviderPropertyManager.php:39
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\getUser
‪AbstractUserAuthentication getUser()
Definition: MfaProviderPropertyManager.php:202
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager
Definition: MfaProviderPropertyManager.php:33
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\$user
‪AbstractUserAuthentication $user
Definition: MfaProviderPropertyManager.php:36
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\create
‪static MfaProviderPropertyManager create(MfaProviderManifestInterface $provider, AbstractUserAuthentication $user)
Definition: MfaProviderPropertyManager.php:224
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\$providerIdentifier
‪string $providerIdentifier
Definition: MfaProviderPropertyManager.php:38
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\createProviderEntry
‪bool createProviderEntry(array $properties)
Definition: MfaProviderPropertyManager.php:129
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\getIdentifier
‪string getIdentifier()
Definition: MfaProviderPropertyManager.php:212
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\hasProperty
‪bool hasProperty(string $key)
Definition: MfaProviderPropertyManager.php:66
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\storeProperties
‪bool storeProperties()
Definition: MfaProviderPropertyManager.php:171
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\getProperties
‪array getProperties()
Definition: MfaProviderPropertyManager.php:89
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:56
‪TYPO3\CMS\Core\Database\Connection\PARAM_LOB
‪const PARAM_LOB
Definition: Connection.php:59