TwoFactorMiddleware.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@owncloud.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Core\Middleware;
  24. use Exception;
  25. use OC\Authentication\Exceptions\TwoFactorAuthRequiredException;
  26. use OC\Authentication\Exceptions\UserAlreadyLoggedInException;
  27. use OC\Authentication\TwoFactorAuth\Manager;
  28. use OC\Core\Controller\LoginController;
  29. use OC\Core\Controller\TwoFactorChallengeController;
  30. use OC\User\Session;
  31. use OCP\AppFramework\Controller;
  32. use OCP\AppFramework\Http\RedirectResponse;
  33. use OCP\AppFramework\Middleware;
  34. use OCP\AppFramework\Utility\IControllerMethodReflector;
  35. use OCP\IRequest;
  36. use OCP\ISession;
  37. use OCP\IURLGenerator;
  38. use OCP\IUser;
  39. class TwoFactorMiddleware extends Middleware {
  40. /** @var Manager */
  41. private $twoFactorManager;
  42. /** @var Session */
  43. private $userSession;
  44. /** @var ISession */
  45. private $session;
  46. /** @var IURLGenerator */
  47. private $urlGenerator;
  48. /** @var IControllerMethodReflector */
  49. private $reflector;
  50. /** @var IRequest */
  51. private $request;
  52. /**
  53. * @param Manager $twoFactorManager
  54. * @param Session $userSession
  55. * @param ISession $session
  56. * @param IURLGenerator $urlGenerator
  57. */
  58. public function __construct(Manager $twoFactorManager, Session $userSession, ISession $session,
  59. IURLGenerator $urlGenerator, IControllerMethodReflector $reflector, IRequest $request) {
  60. $this->twoFactorManager = $twoFactorManager;
  61. $this->userSession = $userSession;
  62. $this->session = $session;
  63. $this->urlGenerator = $urlGenerator;
  64. $this->reflector = $reflector;
  65. $this->request = $request;
  66. }
  67. /**
  68. * @param Controller $controller
  69. * @param string $methodName
  70. */
  71. public function beforeController($controller, $methodName) {
  72. if ($this->reflector->hasAnnotation('PublicPage')) {
  73. // Don't block public pages
  74. return;
  75. }
  76. if ($controller instanceof LoginController && $methodName === 'logout') {
  77. // Don't block the logout page, to allow canceling the 2FA
  78. return;
  79. }
  80. if ($this->userSession->isLoggedIn()) {
  81. $user = $this->userSession->getUser();
  82. if ($this->twoFactorManager->isTwoFactorAuthenticated($user)) {
  83. $this->checkTwoFactor($controller, $methodName, $user);
  84. } else if ($controller instanceof TwoFactorChallengeController) {
  85. // Allow access to the two-factor controllers only if two-factor authentication
  86. // is in progress.
  87. throw new UserAlreadyLoggedInException();
  88. }
  89. }
  90. // TODO: dont check/enforce 2FA if a auth token is used
  91. }
  92. private function checkTwoFactor($controller, $methodName, IUser $user) {
  93. // If two-factor auth is in progress disallow access to any controllers
  94. // defined within "LoginController".
  95. $needsSecondFactor = $this->twoFactorManager->needsSecondFactor($user);
  96. $twoFactor = $controller instanceof TwoFactorChallengeController;
  97. // Disallow access to any controller if 2FA needs to be checked
  98. if ($needsSecondFactor && !$twoFactor) {
  99. throw new TwoFactorAuthRequiredException();
  100. }
  101. // Allow access to the two-factor controllers only if two-factor authentication
  102. // is in progress.
  103. if (!$needsSecondFactor && $twoFactor) {
  104. throw new UserAlreadyLoggedInException();
  105. }
  106. }
  107. public function afterException($controller, $methodName, Exception $exception) {
  108. if ($exception instanceof TwoFactorAuthRequiredException) {
  109. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge', [
  110. 'redirect_url' => urlencode($this->request->server['REQUEST_URI']),
  111. ]));
  112. }
  113. if ($exception instanceof UserAlreadyLoggedInException) {
  114. return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index'));
  115. }
  116. throw $exception;
  117. }
  118. }