PasswordConfirmationMiddleware.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 OC\Authentication\Token\IProvider;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http\Attribute\PasswordConfirmationRequired;
  30. use OCP\AppFramework\Middleware;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\Authentication\Exceptions\ExpiredTokenException;
  33. use OCP\Authentication\Exceptions\InvalidTokenException;
  34. use OCP\Authentication\Exceptions\WipeTokenException;
  35. use OCP\ISession;
  36. use OCP\IUserSession;
  37. use OCP\Session\Exceptions\SessionNotAvailableException;
  38. use OCP\User\Backend\IPasswordConfirmationBackend;
  39. use ReflectionMethod;
  40. class PasswordConfirmationMiddleware extends Middleware {
  41. /** @var ControllerMethodReflector */
  42. private $reflector;
  43. /** @var ISession */
  44. private $session;
  45. /** @var IUserSession */
  46. private $userSession;
  47. /** @var ITimeFactory */
  48. private $timeFactory;
  49. /** @var array */
  50. private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];
  51. private IProvider $tokenProvider;
  52. /**
  53. * PasswordConfirmationMiddleware constructor.
  54. *
  55. * @param ControllerMethodReflector $reflector
  56. * @param ISession $session
  57. * @param IUserSession $userSession
  58. * @param ITimeFactory $timeFactory
  59. */
  60. public function __construct(ControllerMethodReflector $reflector,
  61. ISession $session,
  62. IUserSession $userSession,
  63. ITimeFactory $timeFactory,
  64. IProvider $tokenProvider,
  65. ) {
  66. $this->reflector = $reflector;
  67. $this->session = $session;
  68. $this->userSession = $userSession;
  69. $this->timeFactory = $timeFactory;
  70. $this->tokenProvider = $tokenProvider;
  71. }
  72. /**
  73. * @param Controller $controller
  74. * @param string $methodName
  75. * @throws NotConfirmedException
  76. */
  77. public function beforeController($controller, $methodName) {
  78. $reflectionMethod = new ReflectionMethod($controller, $methodName);
  79. if ($this->hasAnnotationOrAttribute($reflectionMethod, 'PasswordConfirmationRequired', PasswordConfirmationRequired::class)) {
  80. $user = $this->userSession->getUser();
  81. $backendClassName = '';
  82. if ($user !== null) {
  83. $backend = $user->getBackend();
  84. if ($backend instanceof IPasswordConfirmationBackend) {
  85. if (!$backend->canConfirmPassword($user->getUID())) {
  86. return;
  87. }
  88. }
  89. $backendClassName = $user->getBackendClassName();
  90. }
  91. try {
  92. $sessionId = $this->session->getId();
  93. $token = $this->tokenProvider->getToken($sessionId);
  94. } catch (SessionNotAvailableException|InvalidTokenException|WipeTokenException|ExpiredTokenException) {
  95. // States we do not deal with here.
  96. return;
  97. }
  98. $scope = $token->getScopeAsArray();
  99. if (isset($scope['password-unconfirmable']) && $scope['password-unconfirmable'] === true) {
  100. // Users logging in from SSO backends cannot confirm their password by design
  101. return;
  102. }
  103. $lastConfirm = (int) $this->session->get('last-password-confirm');
  104. // TODO: confirm excludedUserBackEnds can go away and remove it
  105. if (!isset($this->excludedUserBackEnds[$backendClassName]) && $lastConfirm < ($this->timeFactory->getTime() - (30 * 60 + 15))) { // allow 15 seconds delay
  106. throw new NotConfirmedException();
  107. }
  108. }
  109. }
  110. /**
  111. * @template T
  112. *
  113. * @param ReflectionMethod $reflectionMethod
  114. * @param string $annotationName
  115. * @param class-string<T> $attributeClass
  116. * @return boolean
  117. */
  118. protected function hasAnnotationOrAttribute(ReflectionMethod $reflectionMethod, string $annotationName, string $attributeClass): bool {
  119. if (!empty($reflectionMethod->getAttributes($attributeClass))) {
  120. return true;
  121. }
  122. if ($this->reflector->hasAnnotation($annotationName)) {
  123. return true;
  124. }
  125. return false;
  126. }
  127. }