PasswordConfirmationMiddleware.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Http\Attribute\PasswordConfirmationRequired;
  29. use OCP\AppFramework\Middleware;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\ISession;
  32. use OCP\IUserSession;
  33. use OCP\User\Backend\IPasswordConfirmationBackend;
  34. use ReflectionMethod;
  35. class PasswordConfirmationMiddleware extends Middleware {
  36. /** @var ControllerMethodReflector */
  37. private $reflector;
  38. /** @var ISession */
  39. private $session;
  40. /** @var IUserSession */
  41. private $userSession;
  42. /** @var ITimeFactory */
  43. private $timeFactory;
  44. /** @var array */
  45. private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];
  46. /**
  47. * PasswordConfirmationMiddleware constructor.
  48. *
  49. * @param ControllerMethodReflector $reflector
  50. * @param ISession $session
  51. * @param IUserSession $userSession
  52. * @param ITimeFactory $timeFactory
  53. */
  54. public function __construct(ControllerMethodReflector $reflector,
  55. ISession $session,
  56. IUserSession $userSession,
  57. ITimeFactory $timeFactory) {
  58. $this->reflector = $reflector;
  59. $this->session = $session;
  60. $this->userSession = $userSession;
  61. $this->timeFactory = $timeFactory;
  62. }
  63. /**
  64. * @param Controller $controller
  65. * @param string $methodName
  66. * @throws NotConfirmedException
  67. */
  68. public function beforeController($controller, $methodName) {
  69. $reflectionMethod = new ReflectionMethod($controller, $methodName);
  70. if ($this->hasAnnotationOrAttribute($reflectionMethod, 'PasswordConfirmationRequired', PasswordConfirmationRequired::class)) {
  71. $user = $this->userSession->getUser();
  72. $backendClassName = '';
  73. if ($user !== null) {
  74. $backend = $user->getBackend();
  75. if ($backend instanceof IPasswordConfirmationBackend) {
  76. if (!$backend->canConfirmPassword($user->getUID())) {
  77. return;
  78. }
  79. }
  80. $backendClassName = $user->getBackendClassName();
  81. }
  82. $lastConfirm = (int) $this->session->get('last-password-confirm');
  83. // we can't check the password against a SAML backend, so skip password confirmation in this case
  84. if (!isset($this->excludedUserBackEnds[$backendClassName]) && $lastConfirm < ($this->timeFactory->getTime() - (30 * 60 + 15))) { // allow 15 seconds delay
  85. throw new NotConfirmedException();
  86. }
  87. }
  88. }
  89. /**
  90. * @template T
  91. *
  92. * @param ReflectionMethod $reflectionMethod
  93. * @param string $annotationName
  94. * @param class-string<T> $attributeClass
  95. * @return boolean
  96. */
  97. protected function hasAnnotationOrAttribute(ReflectionMethod $reflectionMethod, string $annotationName, string $attributeClass): bool {
  98. if (!empty($reflectionMethod->getAttributes($attributeClass))) {
  99. return true;
  100. }
  101. if ($this->reflector->hasAnnotation($annotationName)) {
  102. return true;
  103. }
  104. return false;
  105. }
  106. }