You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
1.5 KiB

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\AppFramework\Middleware;
  8. use OC\AppFramework\Utility\ControllerMethodReflector;
  9. use OC\Core\Controller\ClientFlowLoginV2Controller;
  10. use OC\Core\Controller\TwoFactorChallengeController;
  11. use OCP\AppFramework\Controller;
  12. use OCP\AppFramework\Http\Attribute\PublicPage;
  13. use OCP\AppFramework\Middleware;
  14. use OCP\ISession;
  15. use OCP\IUserSession;
  16. use ReflectionMethod;
  17. // Will close the session if the user session is ephemeral.
  18. // Happens when the user logs in via the login flow v2.
  19. class FlowV2EphemeralSessionsMiddleware extends Middleware {
  20. public function __construct(
  21. private ISession $session,
  22. private IUserSession $userSession,
  23. private ControllerMethodReflector $reflector,
  24. ) {
  25. }
  26. public function beforeController(Controller $controller, string $methodName) {
  27. if (!$this->session->get(ClientFlowLoginV2Controller::EPHEMERAL_NAME)) {
  28. return;
  29. }
  30. if (
  31. $controller instanceof ClientFlowLoginV2Controller &&
  32. ($methodName === 'grantPage' || $methodName === 'generateAppPassword')
  33. ) {
  34. return;
  35. }
  36. if ($controller instanceof TwoFactorChallengeController) {
  37. return;
  38. }
  39. $reflectionMethod = new ReflectionMethod($controller, $methodName);
  40. if (!empty($reflectionMethod->getAttributes(PublicPage::class))) {
  41. return;
  42. }
  43. if ($this->reflector->hasAnnotation('PublicPage')) {
  44. return;
  45. }
  46. $this->userSession->logout();
  47. $this->session->close();
  48. }
  49. }