PasswordConfirmationMiddleware.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\AppFramework\Middleware\Security;
  25. use OC\AppFramework\Middleware\Security\Exceptions\NotConfirmedException;
  26. use OC\AppFramework\Utility\ControllerMethodReflector;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Middleware;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\ISession;
  31. use OCP\IUserSession;
  32. use OCP\User\Backend\IPasswordConfirmationBackend;
  33. class PasswordConfirmationMiddleware extends Middleware {
  34. /** @var ControllerMethodReflector */
  35. private $reflector;
  36. /** @var ISession */
  37. private $session;
  38. /** @var IUserSession */
  39. private $userSession;
  40. /** @var ITimeFactory */
  41. private $timeFactory;
  42. /** @var array */
  43. private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];
  44. /**
  45. * PasswordConfirmationMiddleware constructor.
  46. *
  47. * @param ControllerMethodReflector $reflector
  48. * @param ISession $session
  49. * @param IUserSession $userSession
  50. * @param ITimeFactory $timeFactory
  51. */
  52. public function __construct(ControllerMethodReflector $reflector,
  53. ISession $session,
  54. IUserSession $userSession,
  55. ITimeFactory $timeFactory) {
  56. $this->reflector = $reflector;
  57. $this->session = $session;
  58. $this->userSession = $userSession;
  59. $this->timeFactory = $timeFactory;
  60. }
  61. /**
  62. * @param Controller $controller
  63. * @param string $methodName
  64. * @throws NotConfirmedException
  65. */
  66. public function beforeController($controller, $methodName) {
  67. if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) {
  68. $user = $this->userSession->getUser();
  69. $backendClassName = '';
  70. if ($user !== null) {
  71. $backend = $user->getBackend();
  72. if ($backend instanceof IPasswordConfirmationBackend) {
  73. if (!$backend->canConfirmPassword($user->getUID())) {
  74. return;
  75. }
  76. }
  77. $backendClassName = $user->getBackendClassName();
  78. }
  79. $lastConfirm = (int) $this->session->get('last-password-confirm');
  80. // we can't check the password against a SAML backend, so skip password confirmation in this case
  81. if (!isset($this->excludedUserBackEnds[$backendClassName]) && $lastConfirm < ($this->timeFactory->getTime() - (30 * 60 + 15))) { // allow 15 seconds delay
  82. throw new NotConfirmedException();
  83. }
  84. }
  85. }
  86. }