1
0

TwoFactorCommand.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Authentication\Login;
  26. use OC\Authentication\TwoFactorAuth\Manager;
  27. use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
  28. use OCP\Authentication\TwoFactorAuth\IProvider;
  29. use OCP\IURLGenerator;
  30. use function array_pop;
  31. use function count;
  32. class TwoFactorCommand extends ALoginCommand {
  33. /** @var Manager */
  34. private $twoFactorManager;
  35. /** @var MandatoryTwoFactor */
  36. private $mandatoryTwoFactor;
  37. /** @var IURLGenerator */
  38. private $urlGenerator;
  39. public function __construct(Manager $twoFactorManager,
  40. MandatoryTwoFactor $mandatoryTwoFactor,
  41. IURLGenerator $urlGenerator) {
  42. $this->twoFactorManager = $twoFactorManager;
  43. $this->mandatoryTwoFactor = $mandatoryTwoFactor;
  44. $this->urlGenerator = $urlGenerator;
  45. }
  46. public function process(LoginData $loginData): LoginResult {
  47. if (!$this->twoFactorManager->isTwoFactorAuthenticated($loginData->getUser())) {
  48. return $this->processNextOrFinishSuccessfully($loginData);
  49. }
  50. $this->twoFactorManager->prepareTwoFactorLogin($loginData->getUser(), $loginData->isRememberLogin());
  51. $providerSet = $this->twoFactorManager->getProviderSet($loginData->getUser());
  52. $loginProviders = $this->twoFactorManager->getLoginSetupProviders($loginData->getUser());
  53. $providers = $providerSet->getPrimaryProviders();
  54. if (empty($providers)
  55. && !$providerSet->isProviderMissing()
  56. && !empty($loginProviders)
  57. && $this->mandatoryTwoFactor->isEnforcedFor($loginData->getUser())) {
  58. // No providers set up, but 2FA is enforced and setup providers are available
  59. $url = 'core.TwoFactorChallenge.setupProviders';
  60. $urlParams = [];
  61. } elseif (!$providerSet->isProviderMissing() && count($providers) === 1) {
  62. // Single provider (and no missing ones), hence we can redirect to that provider's challenge page directly
  63. /* @var $provider IProvider */
  64. $provider = array_pop($providers);
  65. $url = 'core.TwoFactorChallenge.showChallenge';
  66. $urlParams = [
  67. 'challengeProviderId' => $provider->getId(),
  68. ];
  69. } else {
  70. $url = 'core.TwoFactorChallenge.selectChallenge';
  71. $urlParams = [];
  72. }
  73. if ($loginData->getRedirectUrl() !== null) {
  74. $urlParams['redirect_url'] = $loginData->getRedirectUrl();
  75. }
  76. return LoginResult::success(
  77. $loginData,
  78. $this->urlGenerator->linkToRoute($url, $urlParams)
  79. );
  80. }
  81. }