TwoFactorMiddleware.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  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, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Core\Middleware;
  26. use Exception;
  27. use OC\Authentication\Exceptions\TwoFactorAuthRequiredException;
  28. use OC\Authentication\Exceptions\UserAlreadyLoggedInException;
  29. use OC\Authentication\TwoFactorAuth\Manager;
  30. use OC\Core\Controller\LoginController;
  31. use OC\Core\Controller\TwoFactorChallengeController;
  32. use OC\User\Session;
  33. use OCA\TwoFactorNextcloudNotification\Controller\APIController;
  34. use OCP\AppFramework\Controller;
  35. use OCP\AppFramework\Http\RedirectResponse;
  36. use OCP\AppFramework\Middleware;
  37. use OCP\AppFramework\Utility\IControllerMethodReflector;
  38. use OCP\Authentication\TwoFactorAuth\ALoginSetupController;
  39. use OCP\IRequest;
  40. use OCP\ISession;
  41. use OCP\IURLGenerator;
  42. use OCP\IUser;
  43. class TwoFactorMiddleware extends Middleware {
  44. /** @var Manager */
  45. private $twoFactorManager;
  46. /** @var Session */
  47. private $userSession;
  48. /** @var ISession */
  49. private $session;
  50. /** @var IURLGenerator */
  51. private $urlGenerator;
  52. /** @var IControllerMethodReflector */
  53. private $reflector;
  54. /** @var IRequest */
  55. private $request;
  56. /**
  57. * @param Manager $twoFactorManager
  58. * @param Session $userSession
  59. * @param ISession $session
  60. * @param IURLGenerator $urlGenerator
  61. */
  62. public function __construct(Manager $twoFactorManager, Session $userSession, ISession $session,
  63. IURLGenerator $urlGenerator, IControllerMethodReflector $reflector, IRequest $request) {
  64. $this->twoFactorManager = $twoFactorManager;
  65. $this->userSession = $userSession;
  66. $this->session = $session;
  67. $this->urlGenerator = $urlGenerator;
  68. $this->reflector = $reflector;
  69. $this->request = $request;
  70. }
  71. /**
  72. * @param Controller $controller
  73. * @param string $methodName
  74. */
  75. public function beforeController($controller, $methodName) {
  76. if ($controller instanceof APIController && $methodName === 'poll') {
  77. // Allow polling the twofactor nextcloud notifications state
  78. return;
  79. }
  80. if ($controller instanceof TwoFactorChallengeController
  81. && $this->userSession->getUser() !== null
  82. && !$this->reflector->hasAnnotation('TwoFactorSetUpDoneRequired')) {
  83. $providers = $this->twoFactorManager->getProviderSet($this->userSession->getUser());
  84. if (!($providers->getPrimaryProviders() === [] && !$providers->isProviderMissing())) {
  85. throw new TwoFactorAuthRequiredException();
  86. }
  87. }
  88. if ($controller instanceof ALoginSetupController
  89. && $this->userSession->getUser() !== null
  90. && $this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) {
  91. $providers = $this->twoFactorManager->getProviderSet($this->userSession->getUser());
  92. if ($providers->getProviders() === [] && !$providers->isProviderMissing()) {
  93. return;
  94. }
  95. }
  96. if ($controller instanceof LoginController && $methodName === 'logout') {
  97. // Don't block the logout page, to allow canceling the 2FA
  98. return;
  99. }
  100. if ($this->userSession->isLoggedIn()) {
  101. $user = $this->userSession->getUser();
  102. if ($this->session->exists('app_password') || $this->twoFactorManager->isTwoFactorAuthenticated($user)) {
  103. $this->checkTwoFactor($controller, $methodName, $user);
  104. } elseif ($controller instanceof TwoFactorChallengeController) {
  105. // Allow access to the two-factor controllers only if two-factor authentication
  106. // is in progress.
  107. throw new UserAlreadyLoggedInException();
  108. }
  109. }
  110. // TODO: dont check/enforce 2FA if a auth token is used
  111. }
  112. private function checkTwoFactor(Controller $controller, $methodName, IUser $user) {
  113. // If two-factor auth is in progress disallow access to any controllers
  114. // defined within "LoginController".
  115. $needsSecondFactor = $this->twoFactorManager->needsSecondFactor($user);
  116. $twoFactor = $controller instanceof TwoFactorChallengeController;
  117. // Disallow access to any controller if 2FA needs to be checked
  118. if ($needsSecondFactor && !$twoFactor) {
  119. throw new TwoFactorAuthRequiredException();
  120. }
  121. // Allow access to the two-factor controllers only if two-factor authentication
  122. // is in progress.
  123. if (!$needsSecondFactor && $twoFactor) {
  124. throw new UserAlreadyLoggedInException();
  125. }
  126. }
  127. public function afterException($controller, $methodName, Exception $exception) {
  128. if ($exception instanceof TwoFactorAuthRequiredException) {
  129. $params = [];
  130. if (isset($this->request->server['REQUEST_URI'])) {
  131. $params['redirect_url'] = $this->request->server['REQUEST_URI'];
  132. }
  133. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge', $params));
  134. }
  135. if ($exception instanceof UserAlreadyLoggedInException) {
  136. return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index'));
  137. }
  138. throw $exception;
  139. }
  140. }