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.

154 lines
4.5 KiB

  1. <?php
  2. /**
  3. * @author Christoph Wurst <christoph@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OC\Core\Controller;
  22. use OC\Authentication\TwoFactorAuth\Manager;
  23. use OCP\AppFramework\Controller;
  24. use OCP\AppFramework\Http\RedirectResponse;
  25. use OCP\AppFramework\Http\TemplateResponse;
  26. use OCP\IRequest;
  27. use OCP\ISession;
  28. use OCP\IURLGenerator;
  29. use OCP\IUserSession;
  30. class TwoFactorChallengeController extends Controller {
  31. /** @var Manager */
  32. private $twoFactorManager;
  33. /** @var IUserSession */
  34. private $userSession;
  35. /** @var ISession */
  36. private $session;
  37. /** @var IURLGenerator */
  38. private $urlGenerator;
  39. /**
  40. * @param string $appName
  41. * @param IRequest $request
  42. * @param Manager $twoFactorManager
  43. * @param IUserSession $userSession
  44. * @param ISession $session
  45. * @param IURLGenerator $urlGenerator
  46. */
  47. public function __construct($appName, IRequest $request, Manager $twoFactorManager, IUserSession $userSession,
  48. ISession $session, IURLGenerator $urlGenerator) {
  49. parent::__construct($appName, $request);
  50. $this->twoFactorManager = $twoFactorManager;
  51. $this->userSession = $userSession;
  52. $this->session = $session;
  53. $this->urlGenerator = $urlGenerator;
  54. }
  55. /**
  56. * @return string
  57. */
  58. protected function getLogoutAttribute() {
  59. return \OC_User::getLogoutAttribute();
  60. }
  61. /**
  62. * @NoAdminRequired
  63. * @NoCSRFRequired
  64. *
  65. * @param string $redirect_url
  66. * @return TemplateResponse
  67. */
  68. public function selectChallenge($redirect_url) {
  69. $user = $this->userSession->getUser();
  70. $providers = $this->twoFactorManager->getProviders($user);
  71. $data = [
  72. 'providers' => $providers,
  73. 'redirect_url' => $redirect_url,
  74. 'logout_attribute' => $this->getLogoutAttribute(),
  75. ];
  76. return new TemplateResponse($this->appName, 'twofactorselectchallenge', $data, 'guest');
  77. }
  78. /**
  79. * @NoAdminRequired
  80. * @NoCSRFRequired
  81. * @UseSession
  82. *
  83. * @param string $challengeProviderId
  84. * @param string $redirect_url
  85. * @return TemplateResponse
  86. */
  87. public function showChallenge($challengeProviderId, $redirect_url) {
  88. $user = $this->userSession->getUser();
  89. $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
  90. if (is_null($provider)) {
  91. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
  92. }
  93. if ($this->session->exists('two_factor_auth_error')) {
  94. $this->session->remove('two_factor_auth_error');
  95. $error = true;
  96. } else {
  97. $error = false;
  98. }
  99. $tmpl = $provider->getTemplate($user);
  100. $tmpl->assign('redirect_url', $redirect_url);
  101. $data = [
  102. 'error' => $error,
  103. 'provider' => $provider,
  104. 'logout_attribute' => $this->getLogoutAttribute(),
  105. 'template' => $tmpl->fetchPage(),
  106. ];
  107. return new TemplateResponse($this->appName, 'twofactorshowchallenge', $data, 'guest');
  108. }
  109. /**
  110. * @NoAdminRequired
  111. * @NoCSRFRequired
  112. * @UseSession
  113. *
  114. * @param string $challengeProviderId
  115. * @param string $challenge
  116. * @param string $redirect_url
  117. * @return RedirectResponse
  118. */
  119. public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) {
  120. $user = $this->userSession->getUser();
  121. $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
  122. if (is_null($provider)) {
  123. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
  124. }
  125. if ($this->twoFactorManager->verifyChallenge($challengeProviderId, $user, $challenge)) {
  126. if (!is_null($redirect_url)) {
  127. return new RedirectResponse($this->urlGenerator->getAbsoluteURL(urldecode($redirect_url)));
  128. }
  129. return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index'));
  130. }
  131. $this->session->set('two_factor_auth_error', true);
  132. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
  133. 'challengeProviderId' => $provider->getId(),
  134. 'redirect_url' => $redirect_url,
  135. ]));
  136. }
  137. }