userFactory = $userFactory;
$this->userResource = $userResource;
$this->tfa = $tfa;
$this->tokenManager = $tokenManager;
$this->json = $json;
}
/**
* Obtain a user with an id and a tfa token
*
* @param string $tfaToken
* @param string $providerCode
* @return User
* @throws AuthorizationException
* @throws LocalizedException
*/
public function authenticateWithTokenAndProvider(string $tfaToken, string $providerCode): User
{
try {
// phpcs:ignore Magento2.Functions.DiscouragedFunction
['user_id' => $userId] = $this->json->unserialize(explode('.', base64_decode($tfaToken))[0]);
} catch (\Throwable $e) {
throw new AuthorizationException(
__('Invalid two-factor authorization token')
);
}
if (!$this->tfa->getProviderIsAllowed($userId, $providerCode)) {
throw new LocalizedException(__('Provider is not allowed.'));
} elseif ($this->tfa->getProviderByCode($providerCode)->isActive($userId)) {
throw new LocalizedException(__('Provider is already configured.'));
} elseif (!$this->tokenManager->isValidFor($userId, $tfaToken)) {
throw new AuthorizationException(
__('Invalid two-factor authorization token')
);
}
$user = $this->userFactory->create();
$this->userResource->load($user, $userId);
return $user;
}
/**
* Validate the user is allowed to use the provider
*
* @param int $userId
* @param string $providerCode
* @throws LocalizedException
*/
public function assertProviderIsValidForUser(int $userId, string $providerCode): void
{
if (!$this->tfa->getProviderIsAllowed($userId, $providerCode)) {
throw new LocalizedException(__('Provider is not allowed.'));
} elseif (!$this->tfa->getProviderByCode($providerCode)->isActive($userId)) {
throw new LocalizedException(__('Provider is not configured.'));
}
}
}