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.

TwoFactorChallengeController.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@owncloud.com>
  6. * @author Cornelius Kölbel <cornelius.koelbel@netknights.it>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Core\Controller;
  27. use OC\Authentication\TwoFactorAuth\Manager;
  28. use OC_User;
  29. use OC_Util;
  30. use OCP\AppFramework\Controller;
  31. use OCP\AppFramework\Http\RedirectResponse;
  32. use OCP\AppFramework\Http\TemplateResponse;
  33. use OCP\Authentication\TwoFactorAuth\IProvidesCustomCSP;
  34. use OCP\Authentication\TwoFactorAuth\TwoFactorException;
  35. use OCP\IRequest;
  36. use OCP\ISession;
  37. use OCP\IURLGenerator;
  38. use OCP\IUserSession;
  39. class TwoFactorChallengeController extends Controller {
  40. /** @var Manager */
  41. private $twoFactorManager;
  42. /** @var IUserSession */
  43. private $userSession;
  44. /** @var ISession */
  45. private $session;
  46. /** @var IURLGenerator */
  47. private $urlGenerator;
  48. /**
  49. * @param string $appName
  50. * @param IRequest $request
  51. * @param Manager $twoFactorManager
  52. * @param IUserSession $userSession
  53. * @param ISession $session
  54. * @param IURLGenerator $urlGenerator
  55. */
  56. public function __construct($appName, IRequest $request, Manager $twoFactorManager, IUserSession $userSession,
  57. ISession $session, IURLGenerator $urlGenerator) {
  58. parent::__construct($appName, $request);
  59. $this->twoFactorManager = $twoFactorManager;
  60. $this->userSession = $userSession;
  61. $this->session = $session;
  62. $this->urlGenerator = $urlGenerator;
  63. }
  64. /**
  65. * @return string
  66. */
  67. protected function getLogoutUrl() {
  68. return OC_User::getLogoutUrl($this->urlGenerator);
  69. }
  70. /**
  71. * @NoAdminRequired
  72. * @NoCSRFRequired
  73. *
  74. * @param string $redirect_url
  75. * @return TemplateResponse
  76. */
  77. public function selectChallenge($redirect_url) {
  78. $user = $this->userSession->getUser();
  79. $providers = $this->twoFactorManager->getProviders($user);
  80. $backupProvider = $this->twoFactorManager->getBackupProvider($user);
  81. $data = [
  82. 'providers' => $providers,
  83. 'backupProvider' => $backupProvider,
  84. 'redirect_url' => $redirect_url,
  85. 'logout_url' => $this->getLogoutUrl(),
  86. ];
  87. return new TemplateResponse($this->appName, 'twofactorselectchallenge', $data, 'guest');
  88. }
  89. /**
  90. * @NoAdminRequired
  91. * @NoCSRFRequired
  92. * @UseSession
  93. *
  94. * @param string $challengeProviderId
  95. * @param string $redirect_url
  96. * @return TemplateResponse|RedirectResponse
  97. */
  98. public function showChallenge($challengeProviderId, $redirect_url) {
  99. $user = $this->userSession->getUser();
  100. $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
  101. if (is_null($provider)) {
  102. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
  103. }
  104. $backupProvider = $this->twoFactorManager->getBackupProvider($user);
  105. if (!is_null($backupProvider) && $backupProvider->getId() === $provider->getId()) {
  106. // Don't show the backup provider link if we're already showing that provider's challenge
  107. $backupProvider = null;
  108. }
  109. $errorMessage = '';
  110. $error = false;
  111. if ($this->session->exists('two_factor_auth_error')) {
  112. $this->session->remove('two_factor_auth_error');
  113. $error = true;
  114. $errorMessage = $this->session->get("two_factor_auth_error_message");
  115. $this->session->remove('two_factor_auth_error_message');
  116. }
  117. $tmpl = $provider->getTemplate($user);
  118. $tmpl->assign('redirect_url', $redirect_url);
  119. $data = [
  120. 'error' => $error,
  121. 'error_message' => $errorMessage,
  122. 'provider' => $provider,
  123. 'backupProvider' => $backupProvider,
  124. 'logout_url' => $this->getLogoutUrl(),
  125. 'redirect_url' => $redirect_url,
  126. 'template' => $tmpl->fetchPage(),
  127. ];
  128. $response = new TemplateResponse($this->appName, 'twofactorshowchallenge', $data, 'guest');
  129. if ($provider instanceof IProvidesCustomCSP) {
  130. $response->setContentSecurityPolicy($provider->getCSP());
  131. }
  132. return $response;
  133. }
  134. /**
  135. * @NoAdminRequired
  136. * @NoCSRFRequired
  137. * @UseSession
  138. *
  139. * @UserRateThrottle(limit=5, period=100)
  140. *
  141. * @param string $challengeProviderId
  142. * @param string $challenge
  143. * @param string $redirect_url
  144. * @return RedirectResponse
  145. */
  146. public function solveChallenge($challengeProviderId, $challenge, $redirect_url = null) {
  147. $user = $this->userSession->getUser();
  148. $provider = $this->twoFactorManager->getProvider($user, $challengeProviderId);
  149. if (is_null($provider)) {
  150. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge'));
  151. }
  152. try {
  153. if ($this->twoFactorManager->verifyChallenge($challengeProviderId, $user, $challenge)) {
  154. if (!is_null($redirect_url)) {
  155. return new RedirectResponse($this->urlGenerator->getAbsoluteURL(urldecode($redirect_url)));
  156. }
  157. return new RedirectResponse(OC_Util::getDefaultPageUrl());
  158. }
  159. } catch (TwoFactorException $e) {
  160. /*
  161. * The 2FA App threw an TwoFactorException. Now we display more
  162. * information to the user. The exception text is stored in the
  163. * session to be used in showChallenge()
  164. */
  165. $this->session->set('two_factor_auth_error_message', $e->getMessage());
  166. }
  167. $this->session->set('two_factor_auth_error', true);
  168. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.showChallenge', [
  169. 'challengeProviderId' => $provider->getId(),
  170. 'redirect_url' => $redirect_url,
  171. ]));
  172. }
  173. }