TwoFactorMiddleware.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 ($this->reflector->hasAnnotation('NoTwoFactorRequired')) {
  77. // Route handler explicitly marked to work without finished 2FA are
  78. // not blocked
  79. return;
  80. }
  81. if ($controller instanceof APIController && $methodName === 'poll') {
  82. // Allow polling the twofactor nextcloud notifications state
  83. return;
  84. }
  85. if ($controller instanceof TwoFactorChallengeController
  86. && $this->userSession->getUser() !== null
  87. && !$this->reflector->hasAnnotation('TwoFactorSetUpDoneRequired')) {
  88. $providers = $this->twoFactorManager->getProviderSet($this->userSession->getUser());
  89. if (!($providers->getPrimaryProviders() === [] && !$providers->isProviderMissing())) {
  90. throw new TwoFactorAuthRequiredException();
  91. }
  92. }
  93. if ($controller instanceof ALoginSetupController
  94. && $this->userSession->getUser() !== null
  95. && $this->twoFactorManager->needsSecondFactor($this->userSession->getUser())) {
  96. $providers = $this->twoFactorManager->getProviderSet($this->userSession->getUser());
  97. if ($providers->getPrimaryProviders() === [] && !$providers->isProviderMissing()) {
  98. return;
  99. }
  100. }
  101. if ($controller instanceof LoginController && $methodName === 'logout') {
  102. // Don't block the logout page, to allow canceling the 2FA
  103. return;
  104. }
  105. if ($this->userSession->isLoggedIn()) {
  106. $user = $this->userSession->getUser();
  107. if ($this->session->exists('app_password') || $this->twoFactorManager->isTwoFactorAuthenticated($user)) {
  108. $this->checkTwoFactor($controller, $methodName, $user);
  109. } elseif ($controller instanceof TwoFactorChallengeController) {
  110. // Allow access to the two-factor controllers only if two-factor authentication
  111. // is in progress.
  112. throw new UserAlreadyLoggedInException();
  113. }
  114. }
  115. // TODO: dont check/enforce 2FA if a auth token is used
  116. }
  117. private function checkTwoFactor(Controller $controller, $methodName, IUser $user) {
  118. // If two-factor auth is in progress disallow access to any controllers
  119. // defined within "LoginController".
  120. $needsSecondFactor = $this->twoFactorManager->needsSecondFactor($user);
  121. $twoFactor = $controller instanceof TwoFactorChallengeController;
  122. // Disallow access to any controller if 2FA needs to be checked
  123. if ($needsSecondFactor && !$twoFactor) {
  124. throw new TwoFactorAuthRequiredException();
  125. }
  126. // Allow access to the two-factor controllers only if two-factor authentication
  127. // is in progress.
  128. if (!$needsSecondFactor && $twoFactor) {
  129. throw new UserAlreadyLoggedInException();
  130. }
  131. }
  132. public function afterException($controller, $methodName, Exception $exception) {
  133. if ($exception instanceof TwoFactorAuthRequiredException) {
  134. $params = [];
  135. if (isset($this->request->server['REQUEST_URI'])) {
  136. $params['redirect_url'] = $this->request->server['REQUEST_URI'];
  137. }
  138. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge', $params));
  139. }
  140. if ($exception instanceof UserAlreadyLoggedInException) {
  141. return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index'));
  142. }
  143. throw $exception;
  144. }
  145. }