You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TwoFactorCommand.php 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. declare(strict_types=1);
  25. namespace OC\Authentication\Login;
  26. use function array_pop;
  27. use function count;
  28. use OC\Authentication\TwoFactorAuth\Manager;
  29. use OC\Authentication\TwoFactorAuth\MandatoryTwoFactor;
  30. use OCP\Authentication\TwoFactorAuth\IProvider;
  31. use OCP\IURLGenerator;
  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. } else if (!$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. }