TwoFactorMiddleware.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\Middleware;
  22. use Exception;
  23. use OC\Authentication\Exceptions\TwoFactorAuthRequiredException;
  24. use OC\Authentication\Exceptions\UserAlreadyLoggedInException;
  25. use OC\Authentication\TwoFactorAuth\Manager;
  26. use OC\Core\Controller\TwoFactorChallengeController;
  27. use OC\User\Session;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http\RedirectResponse;
  30. use OCP\AppFramework\Middleware;
  31. use OCP\AppFramework\Utility\IControllerMethodReflector;
  32. use OCP\IRequest;
  33. use OCP\ISession;
  34. use OCP\IURLGenerator;
  35. class TwoFactorMiddleware extends Middleware {
  36. /** @var Manager */
  37. private $twoFactorManager;
  38. /** @var Session */
  39. private $userSession;
  40. /** @var ISession */
  41. private $session;
  42. /** @var IURLGenerator */
  43. private $urlGenerator;
  44. /** @var IControllerMethodReflector */
  45. private $reflector;
  46. /** @var IRequest */
  47. private $request;
  48. /**
  49. * @param Manager $twoFactorManager
  50. * @param Session $userSession
  51. * @param ISession $session
  52. * @param IURLGenerator $urlGenerator
  53. */
  54. public function __construct(Manager $twoFactorManager, Session $userSession, ISession $session,
  55. IURLGenerator $urlGenerator, IControllerMethodReflector $reflector, IRequest $request) {
  56. $this->twoFactorManager = $twoFactorManager;
  57. $this->userSession = $userSession;
  58. $this->session = $session;
  59. $this->urlGenerator = $urlGenerator;
  60. $this->reflector = $reflector;
  61. $this->request = $request;
  62. }
  63. /**
  64. * @param Controller $controller
  65. * @param string $methodName
  66. */
  67. public function beforeController($controller, $methodName) {
  68. if ($this->reflector->hasAnnotation('PublicPage')) {
  69. // Don't block public pages
  70. return;
  71. }
  72. if ($controller instanceof \OC\Core\Controller\LoginController && $methodName === 'logout') {
  73. // Don't block the logout page, to allow canceling the 2FA
  74. return;
  75. }
  76. if ($this->userSession->isLoggedIn()) {
  77. $user = $this->userSession->getUser();
  78. if ($this->twoFactorManager->isTwoFactorAuthenticated($user)) {
  79. $this->checkTwoFactor($controller, $methodName);
  80. } else if ($controller instanceof TwoFactorChallengeController) {
  81. // Allow access to the two-factor controllers only if two-factor authentication
  82. // is in progress.
  83. throw new UserAlreadyLoggedInException();
  84. }
  85. }
  86. // TODO: dont check/enforce 2FA if a auth token is used
  87. }
  88. private function checkTwoFactor($controller, $methodName) {
  89. // If two-factor auth is in progress disallow access to any controllers
  90. // defined within "LoginController".
  91. $needsSecondFactor = $this->twoFactorManager->needsSecondFactor();
  92. $twoFactor = $controller instanceof TwoFactorChallengeController;
  93. // Disallow access to any controller if 2FA needs to be checked
  94. if ($needsSecondFactor && !$twoFactor) {
  95. throw new TwoFactorAuthRequiredException();
  96. }
  97. // Allow access to the two-factor controllers only if two-factor authentication
  98. // is in progress.
  99. if (!$needsSecondFactor && $twoFactor) {
  100. throw new UserAlreadyLoggedInException();
  101. }
  102. }
  103. public function afterException($controller, $methodName, Exception $exception) {
  104. if ($exception instanceof TwoFactorAuthRequiredException) {
  105. return new RedirectResponse($this->urlGenerator->linkToRoute('core.TwoFactorChallenge.selectChallenge', [
  106. 'redirect_url' => urlencode($this->request->server['REQUEST_URI']),
  107. ]));
  108. }
  109. if ($exception instanceof UserAlreadyLoggedInException) {
  110. return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index'));
  111. }
  112. }
  113. }