@@ -26,92 +26,92 @@ |
||
| 26 | 26 | use ReflectionMethod; |
| 27 | 27 | |
| 28 | 28 | class PasswordConfirmationMiddleware extends Middleware { |
| 29 | - private array $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true]; |
|
| 29 | + private array $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true]; |
|
| 30 | 30 | |
| 31 | - public function __construct( |
|
| 32 | - private ControllerMethodReflector $reflector, |
|
| 33 | - private ISession $session, |
|
| 34 | - private IUserSession $userSession, |
|
| 35 | - private ITimeFactory $timeFactory, |
|
| 36 | - private IProvider $tokenProvider, |
|
| 37 | - private readonly LoggerInterface $logger, |
|
| 38 | - private readonly IRequest $request, |
|
| 39 | - private readonly Manager $userManager, |
|
| 40 | - ) { |
|
| 41 | - } |
|
| 31 | + public function __construct( |
|
| 32 | + private ControllerMethodReflector $reflector, |
|
| 33 | + private ISession $session, |
|
| 34 | + private IUserSession $userSession, |
|
| 35 | + private ITimeFactory $timeFactory, |
|
| 36 | + private IProvider $tokenProvider, |
|
| 37 | + private readonly LoggerInterface $logger, |
|
| 38 | + private readonly IRequest $request, |
|
| 39 | + private readonly Manager $userManager, |
|
| 40 | + ) { |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @throws NotConfirmedException |
|
| 45 | - */ |
|
| 46 | - public function beforeController(Controller $controller, string $methodName) { |
|
| 47 | - $reflectionMethod = new ReflectionMethod($controller, $methodName); |
|
| 43 | + /** |
|
| 44 | + * @throws NotConfirmedException |
|
| 45 | + */ |
|
| 46 | + public function beforeController(Controller $controller, string $methodName) { |
|
| 47 | + $reflectionMethod = new ReflectionMethod($controller, $methodName); |
|
| 48 | 48 | |
| 49 | - if (!$this->needsPasswordConfirmation($reflectionMethod)) { |
|
| 50 | - return; |
|
| 51 | - } |
|
| 49 | + if (!$this->needsPasswordConfirmation($reflectionMethod)) { |
|
| 50 | + return; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - $user = $this->userSession->getUser(); |
|
| 54 | - $backendClassName = ''; |
|
| 55 | - if ($user !== null) { |
|
| 56 | - $backend = $user->getBackend(); |
|
| 57 | - if ($backend instanceof IPasswordConfirmationBackend) { |
|
| 58 | - if (!$backend->canConfirmPassword($user->getUID())) { |
|
| 59 | - return; |
|
| 60 | - } |
|
| 61 | - } |
|
| 53 | + $user = $this->userSession->getUser(); |
|
| 54 | + $backendClassName = ''; |
|
| 55 | + if ($user !== null) { |
|
| 56 | + $backend = $user->getBackend(); |
|
| 57 | + if ($backend instanceof IPasswordConfirmationBackend) { |
|
| 58 | + if (!$backend->canConfirmPassword($user->getUID())) { |
|
| 59 | + return; |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - $backendClassName = $user->getBackendClassName(); |
|
| 64 | - } |
|
| 63 | + $backendClassName = $user->getBackendClassName(); |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - try { |
|
| 67 | - $sessionId = $this->session->getId(); |
|
| 68 | - $token = $this->tokenProvider->getToken($sessionId); |
|
| 69 | - } catch (SessionNotAvailableException|InvalidTokenException|WipeTokenException|ExpiredTokenException) { |
|
| 70 | - // States we do not deal with here. |
|
| 71 | - return; |
|
| 72 | - } |
|
| 66 | + try { |
|
| 67 | + $sessionId = $this->session->getId(); |
|
| 68 | + $token = $this->tokenProvider->getToken($sessionId); |
|
| 69 | + } catch (SessionNotAvailableException|InvalidTokenException|WipeTokenException|ExpiredTokenException) { |
|
| 70 | + // States we do not deal with here. |
|
| 71 | + return; |
|
| 72 | + } |
|
| 73 | 73 | |
| 74 | - $scope = $token->getScopeAsArray(); |
|
| 75 | - if (isset($scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION]) && $scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION] === true) { |
|
| 76 | - // Users logging in from SSO backends cannot confirm their password by design |
|
| 77 | - return; |
|
| 78 | - } |
|
| 74 | + $scope = $token->getScopeAsArray(); |
|
| 75 | + if (isset($scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION]) && $scope[IToken::SCOPE_SKIP_PASSWORD_VALIDATION] === true) { |
|
| 76 | + // Users logging in from SSO backends cannot confirm their password by design |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - if ($this->isPasswordConfirmationStrict($reflectionMethod)) { |
|
| 81 | - $authHeader = $this->request->getHeader('Authorization'); |
|
| 82 | - [, $password] = explode(':', base64_decode(substr($authHeader, 6)), 2); |
|
| 83 | - $loginName = $this->session->get('loginname'); |
|
| 84 | - $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
| 85 | - if ($loginResult === false) { |
|
| 86 | - throw new NotConfirmedException(); |
|
| 87 | - } |
|
| 80 | + if ($this->isPasswordConfirmationStrict($reflectionMethod)) { |
|
| 81 | + $authHeader = $this->request->getHeader('Authorization'); |
|
| 82 | + [, $password] = explode(':', base64_decode(substr($authHeader, 6)), 2); |
|
| 83 | + $loginName = $this->session->get('loginname'); |
|
| 84 | + $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
| 85 | + if ($loginResult === false) { |
|
| 86 | + throw new NotConfirmedException(); |
|
| 87 | + } |
|
| 88 | 88 | |
| 89 | - $this->session->set('last-password-confirm', $this->timeFactory->getTime()); |
|
| 90 | - } else { |
|
| 91 | - $lastConfirm = (int)$this->session->get('last-password-confirm'); |
|
| 92 | - // TODO: confirm excludedUserBackEnds can go away and remove it |
|
| 93 | - if (!isset($this->excludedUserBackEnds[$backendClassName]) && $lastConfirm < ($this->timeFactory->getTime() - (30 * 60 + 15))) { // allow 15 seconds delay |
|
| 94 | - throw new NotConfirmedException(); |
|
| 95 | - } |
|
| 96 | - } |
|
| 97 | - } |
|
| 89 | + $this->session->set('last-password-confirm', $this->timeFactory->getTime()); |
|
| 90 | + } else { |
|
| 91 | + $lastConfirm = (int)$this->session->get('last-password-confirm'); |
|
| 92 | + // TODO: confirm excludedUserBackEnds can go away and remove it |
|
| 93 | + if (!isset($this->excludedUserBackEnds[$backendClassName]) && $lastConfirm < ($this->timeFactory->getTime() - (30 * 60 + 15))) { // allow 15 seconds delay |
|
| 94 | + throw new NotConfirmedException(); |
|
| 95 | + } |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - private function needsPasswordConfirmation(ReflectionMethod $reflectionMethod): bool { |
|
| 100 | - $attributes = $reflectionMethod->getAttributes(PasswordConfirmationRequired::class); |
|
| 101 | - if (!empty($attributes)) { |
|
| 102 | - return true; |
|
| 103 | - } |
|
| 99 | + private function needsPasswordConfirmation(ReflectionMethod $reflectionMethod): bool { |
|
| 100 | + $attributes = $reflectionMethod->getAttributes(PasswordConfirmationRequired::class); |
|
| 101 | + if (!empty($attributes)) { |
|
| 102 | + return true; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) { |
|
| 106 | - $this->logger->debug($reflectionMethod->getDeclaringClass()->getName() . '::' . $reflectionMethod->getName() . ' uses the @' . 'PasswordConfirmationRequired' . ' annotation and should use the #[PasswordConfirmationRequired] attribute instead'); |
|
| 107 | - return true; |
|
| 108 | - } |
|
| 105 | + if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) { |
|
| 106 | + $this->logger->debug($reflectionMethod->getDeclaringClass()->getName() . '::' . $reflectionMethod->getName() . ' uses the @' . 'PasswordConfirmationRequired' . ' annotation and should use the #[PasswordConfirmationRequired] attribute instead'); |
|
| 107 | + return true; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - return false; |
|
| 111 | - } |
|
| 110 | + return false; |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - private function isPasswordConfirmationStrict(ReflectionMethod $reflectionMethod): bool { |
|
| 114 | - $attributes = $reflectionMethod->getAttributes(PasswordConfirmationRequired::class); |
|
| 115 | - return !empty($attributes) && ($attributes[0]->newInstance()->getStrict()); |
|
| 116 | - } |
|
| 113 | + private function isPasswordConfirmationStrict(ReflectionMethod $reflectionMethod): bool { |
|
| 114 | + $attributes = $reflectionMethod->getAttributes(PasswordConfirmationRequired::class); |
|
| 115 | + return !empty($attributes) && ($attributes[0]->newInstance()->getStrict()); |
|
| 116 | + } |
|
| 117 | 117 | } |