‪TYPO3CMS  11.5
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 $identifer): bool
40  {
41  return isset($this->providers[$identifer]);
42  }
43 
44  public function ‪hasProviders(): bool
45  {
46  return $this->providers !== [];
47  }
48 
49  public function ‪getProvider(string $identifier): ‪MfaProviderManifestInterface
50  {
51  if (!$this->‪hasProvider($identifier)) {
52  throw new \InvalidArgumentException('No MFA provider for identifier ' . $identifier . ' found.', 1610994735);
53  }
54 
55  return $this->providers[$identifier];
56  }
57 
58  public function ‪getProviders(): array
59  {
60  return ‪$this->providers;
61  }
62 
70  {
71  return $this->‪getActiveProviders($user) !== [];
72  }
73 
81  {
82  return array_filter($this->providers, static function ($provider) use ($user) {
83  return $provider->isActive(‪MfaProviderPropertyManager::create($provider, $user));
84  });
85  }
86 
96  {
97  // Since the user is not fully authenticated we need to unpack UC here to be
98  // able to retrieve a possible defined default (preferred) provider.
99  $user->‪unpack_uc();
100 
101  $activeProviders = $this->‪getActiveProviders($user);
102  // If the user did not activate any provider yet, authentication is not possible
103  if ($activeProviders === []) {
104  return null;
105  }
106  // Check if the user has chosen a default (preferred) provider, which is still active
107  $defaultProvider = (string)($user->uc['mfa']['defaultProvider'] ?? '');
108  if ($defaultProvider !== '' && isset($activeProviders[$defaultProvider])) {
109  return $activeProviders[$defaultProvider];
110  }
111  // If no default provider exists or is not valid, return the first active provider
112  return array_shift($activeProviders);
113  }
114 
122  {
123  return $this->‪getLockedProviders($user) !== [];
124  }
125 
133  {
134  return array_filter($this->providers, static function ($provider) use ($user) {
135  return $provider->isLocked(‪MfaProviderPropertyManager::create($provider, $user));
136  });
137  }
138 
139  public function ‪allowedProvidersItemsProcFunc(array &$parameters): void
140  {
141  foreach ($this->providers as $provider) {
142  $parameters['items'][] = [
143  $provider->getTitle(),
144  $provider->getIdentifier(),
145  $provider->getIconIdentifier(),
146  null,
147  $provider->getDescription(),
148  ];
149  }
150  }
151 }
‪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\AbstractUserAuthentication\unpack_uc
‪unpack_uc($theUC='')
Definition: AbstractUserAuthentication.php:1012
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderManifestInterface\getIdentifier
‪string getIdentifier()
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\getActiveProviders
‪MfaProviderManifestInterface[] getActiveProviders(AbstractUserAuthentication $user)
Definition: MfaProviderRegistry.php:80
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderManifestInterface
Definition: MfaProviderManifestInterface.php:26
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\hasActiveProviders
‪bool hasActiveProviders(AbstractUserAuthentication $user)
Definition: MfaProviderRegistry.php:69
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\getLockedProviders
‪MfaProviderManifestInterface[] getLockedProviders(AbstractUserAuthentication $user)
Definition: MfaProviderRegistry.php:132
‪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:58
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\allowedProvidersItemsProcFunc
‪allowedProvidersItemsProcFunc(array &$parameters)
Definition: MfaProviderRegistry.php:139
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\hasProviders
‪hasProviders()
Definition: MfaProviderRegistry.php:44
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\$providers
‪array $providers
Definition: MfaProviderRegistry.php:32
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderPropertyManager\create
‪static MfaProviderPropertyManager create(MfaProviderManifestInterface $provider, AbstractUserAuthentication $user)
Definition: MfaProviderPropertyManager.php:224
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\hasLockedProviders
‪bool hasLockedProviders(AbstractUserAuthentication $user)
Definition: MfaProviderRegistry.php:121
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\getFirstAuthenticationAwareProvider
‪MfaProviderManifestInterface getFirstAuthenticationAwareProvider(AbstractUserAuthentication $user)
Definition: MfaProviderRegistry.php:95
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:56
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry
Definition: MfaProviderRegistry.php:28
‪TYPO3\CMS\Core\Authentication\Mfa\MfaProviderRegistry\hasProvider
‪hasProvider(string $identifer)
Definition: MfaProviderRegistry.php:39