‪TYPO3CMS  ‪main
MfaProviderRegistry.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 
21 
28 {
32  protected array ‪$providers = [];
33 
34  public function ‪registerProvider(‪MfaProviderManifestInterface $provider): void
35  {
36  $this->providers[$provider->‪getIdentifier()] = $provider;
37  }
38 
39  public function ‪hasProvider(string ‪$identifier): bool
40  {
41  return isset($this->providers[‪$identifier]);
42  }
43 
44  public function ‪hasProviders(): bool
45  {
46  return $this->providers !== [];
47  }
48 
50  {
51  if (!$this->‪hasProvider($identifier)) {
52  throw new \InvalidArgumentException('No MFA provider for identifier ' . ‪$identifier . ' found.', 1610994735);
53  }
54  return $this->providers[‪$identifier];
55  }
56 
57  public function ‪getProviders(): array
58  {
59  return ‪$this->providers;
60  }
61 
66  {
67  return $this->‪getActiveProviders($user) !== [];
68  }
69 
76  {
77  return array_filter($this->providers, static function (‪MfaProviderManifestInterface $provider) use ($user): bool {
78  return $provider->‪isActive(‪MfaProviderPropertyManager::create($provider, $user));
79  });
80  }
81 
90  {
91  $activeProviders = $this->‪getActiveProviders($user);
92  // If the user did not activate any provider yet, authentication is not possible
93  if ($activeProviders === []) {
94  return null;
95  }
96  // Check if the user has chosen a default (preferred) provider, which is still active
97  $defaultProvider = (string)($user->uc['mfa']['defaultProvider'] ?? '');
98  if ($defaultProvider !== '' && isset($activeProviders[$defaultProvider])) {
99  return $activeProviders[$defaultProvider];
100  }
101  // If no default provider exists or is not valid, return the first active provider
102  return array_shift($activeProviders);
103  }
104 
109  {
110  return $this->‪getLockedProviders($user) !== [];
111  }
112 
119  {
120  return array_filter($this->providers, static function (‪MfaProviderManifestInterface $provider) use ($user): bool {
121  return $provider->‪isLocked(‪MfaProviderPropertyManager::create($provider, $user));
122  });
123  }
124 
125  public function ‪allowedProvidersItemsProcFunc(array &$parameters): void
126  {
127  foreach ($this->providers as $provider) {
128  $parameters['items'][] = [
129  'label' => $provider->getTitle(),
130  'value' => $provider->getIdentifier(),
131  'icon' => $provider->getIconIdentifier(),
132  'description' => $provider->getDescription(),
133  ];
134  }
135  }
136 }
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\getProvider
‪getProvider(string $identifier)
Definition: MfaProviderRegistry.php:49
‪TYPO3\CMS\Core\Authentication\Mfa
Definition: MfaProviderInterface.php:18
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\getActiveProviders
‪MfaProviderManifestInterface[] getActiveProviders(AbstractUserAuthentication $user)
Definition: MfaProviderRegistry.php:75
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\hasLockedProviders
‪hasLockedProviders(AbstractUserAuthentication $user)
Definition: MfaProviderRegistry.php:108
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderManifestInterface
Definition: MfaProviderManifestInterface.php:26
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\getLockedProviders
‪MfaProviderManifestInterface[] getLockedProviders(AbstractUserAuthentication $user)
Definition: MfaProviderRegistry.php:118
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\hasActiveProviders
‪hasActiveProviders(AbstractUserAuthentication $user)
Definition: MfaProviderRegistry.php:65
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\create
‪static create(MfaProviderManifestInterface $provider, AbstractUserAuthentication $user)
Definition: MfaProviderPropertyManager.php:193
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\registerProvider
‪registerProvider(MfaProviderManifestInterface $provider)
Definition: MfaProviderRegistry.php:34
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\getProviders
‪getProviders()
Definition: MfaProviderRegistry.php:57
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\allowedProvidersItemsProcFunc
‪allowedProvidersItemsProcFunc(array &$parameters)
Definition: MfaProviderRegistry.php:125
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderInterface\isLocked
‪isLocked(MfaProviderPropertyManager $propertyManager)
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderInterface\isActive
‪isActive(MfaProviderPropertyManager $propertyManager)
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\hasProviders
‪hasProviders()
Definition: MfaProviderRegistry.php:44
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\hasProvider
‪hasProvider(string $identifier)
Definition: MfaProviderRegistry.php:39
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\$providers
‪array $providers
Definition: MfaProviderRegistry.php:32
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderManifestInterface\getIdentifier
‪getIdentifier()
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\getFirstAuthenticationAwareProvider
‪MfaProviderManifestInterface getFirstAuthenticationAwareProvider(AbstractUserAuthentication $user)
Definition: MfaProviderRegistry.php:89
‪TYPO3\CMS\Webhooks\Message\$identifier
‪identifier readonly string $identifier
Definition: FileAddedMessage.php:37
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:65
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry
Definition: MfaProviderRegistry.php:28