Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/Attributes/OnlyUnauthenticatedUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

// SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
// SPDX-License-Identifier: AGPL-3.0-or-later

namespace OCA\User_SAML\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_METHOD)]
class OnlyUnauthenticatedUsers {
}
22 changes: 8 additions & 14 deletions lib/Controller/SAMLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OC\Core\Controller\ClientFlowLoginController;
use OC\Core\Controller\ClientFlowLoginV2Controller;
use OC\Security\CSRF\CsrfTokenManager;
use OCA\User_SAML\Attributes\OnlyUnauthenticatedUsers;
use OCA\User_SAML\Exceptions\NoUserFoundException;
use OCA\User_SAML\Exceptions\UserFilterViolationException;
use OCA\User_SAML\Helper\TXmlHelper;
Expand Down Expand Up @@ -145,12 +146,12 @@ protected function assertGroupMemberships(): void {
}

/**
* @OnlyUnauthenticatedUsers
* @throws Exception
*/
#[PublicPage]
#[UseSession]
#[NoCSRFRequired]
#[OnlyUnauthenticatedUsers]
public function login(int $idp = 1): Http\RedirectResponse|Http\TemplateResponse {
$originalUrl = (string)$this->request->getParam('originalUrl', '');
if (!$this->trustedDomainHelper->isTrustedUrl($originalUrl)) {
Expand Down Expand Up @@ -287,6 +288,7 @@ public function login(int $idp = 1): Http\RedirectResponse|Http\TemplateResponse
*/
#[PublicPage]
#[NoCSRFRequired]
#[OnlyUnauthenticatedUsers]
public function getMetadata(int $idp = 1): Http\DataDownloadResponse {
$settings = new Settings($this->samlSettings->getOneLoginSettingsArray($idp));
$metadata = $settings->getSPMetadata();
Expand All @@ -302,7 +304,6 @@ public function getMetadata(int $idp = 1): Http\DataDownloadResponse {
}

/**
* @OnlyUnauthenticatedUsers
* @NoSameSiteCookieRequired
*
* @return Http\RedirectResponse
Expand All @@ -312,6 +313,7 @@ public function getMetadata(int $idp = 1): Http\DataDownloadResponse {
#[PublicPage]
#[NoCSRFRequired]
#[UseSession]
#[OnlyUnauthenticatedUsers]
public function assertionConsumerService(): Http\RedirectResponse {
// Fetch and decrypt the cookie
$cookie = $this->request->getCookie('saml_data');
Expand Down Expand Up @@ -536,41 +538,33 @@ private function tryProcessSLOResponse(?int $idp): array {
return [null, null];
}

/**
* @OnlyUnauthenticatedUsers
*/
#[PublicPage]
#[NoCSRFRequired]
#[OnlyUnauthenticatedUsers]
public function notProvisioned(): Http\TemplateResponse {
return new Http\TemplateResponse($this->appName, 'notProvisioned', [], 'guest');
}

/**
* @OnlyUnauthenticatedUsers
*/
#[PublicPage]
#[NoCSRFRequired]
#[OnlyUnauthenticatedUsers]
public function notPermitted(): Http\TemplateResponse {
return new Http\TemplateResponse($this->appName, 'notPermitted', [], 'guest');
}

/**
* @OnlyUnauthenticatedUsers
*/
#[PublicPage]
#[NoCSRFRequired]
#[OnlyUnauthenticatedUsers]
public function genericError(string $message): Http\TemplateResponse {
if (empty($message)) {
$message = $this->l->t('Unknown error, please check the log file for more details.');
}
return new Http\TemplateResponse($this->appName, 'error', ['message' => $message], 'guest');
}

/**
* @OnlyUnauthenticatedUsers
*/
#[PublicPage]
#[NoCSRFRequired]
#[OnlyUnauthenticatedUsers]
public function selectUserBackEnd(string $redirectUrl = ''): Http\TemplateResponse {
$attributes = ['loginUrls' => []];

Expand Down
10 changes: 7 additions & 3 deletions lib/Middleware/OnlyLoggedInMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace OCA\User_SAML\Middleware;

use OCA\User_SAML\Attributes\OnlyUnauthenticatedUsers;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Middleware;
use OCP\AppFramework\Utility\IControllerMethodReflector;
use OCP\IURLGenerator;
use OCP\IUserSession;
use Override;
Expand All @@ -23,7 +23,6 @@
class OnlyLoggedInMiddleware extends Middleware {

public function __construct(
private readonly IControllerMethodReflector $reflector,
private readonly IUserSession $userSession,
private readonly IURLGenerator $urlGenerator,
) {
Expand All @@ -36,7 +35,7 @@ public function __construct(
*/
#[Override]
public function beforeController($controller, $methodName): void {
if ($this->reflector->hasAnnotation('OnlyUnauthenticatedUsers') && $this->userSession->isLoggedIn()) {
if ($this->hasAttribute($controller, $methodName) && $this->userSession->isLoggedIn()) {
throw new \Exception('User is already logged-in');
}
}
Expand All @@ -55,4 +54,9 @@ public function afterException($controller, $methodName, \Exception $exception):

throw $exception;
}

protected function hasAttribute(object $controller, string $methodName): bool {
$reflectionMethod = new \ReflectionMethod($controller, $methodName);
return !empty($reflectionMethod->getAttributes(OnlyUnauthenticatedUsers::class));
}
}
5 changes: 0 additions & 5 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
<code><![CDATA[GroupBackend]]></code>
</DeprecatedInterface>
</file>
<file src="lib/Middleware/OnlyLoggedInMiddleware.php">
<DeprecatedMethod>
<code><![CDATA[hasAnnotation]]></code>
</DeprecatedMethod>
</file>
<file src="lib/UserBackend.php">
<DeprecatedInterface>
<code><![CDATA[UserBackend]]></code>
Expand Down
44 changes: 23 additions & 21 deletions tests/unit/Middleware/OnlyLoggedInMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,64 +11,66 @@
use OCA\User_SAML\Middleware\OnlyLoggedInMiddleware;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http\RedirectResponse;
use OCP\AppFramework\Utility\IControllerMethodReflector;
use OCP\IURLGenerator;
use OCP\IUserSession;
use Override;
use PHPUnit\Framework\MockObject\MockObject;

class OnlyLoggedInMiddlewareTest extends \Test\TestCase {
protected IURLGenerator&MockObject $urlGenerator;
private IControllerMethodReflector&MockObject $reflector;
private IUserSession&MockObject $userSession;
private OnlyLoggedInMiddleware $onlyLoggedInMiddleware;
private OnlyLoggedInMiddleware&MockObject $onlyLoggedInMiddleware;

#[Override]
protected function setUp(): void {
$this->reflector = $this->createMock(IControllerMethodReflector::class);
$this->userSession = $this->createMock(IUserSession::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->onlyLoggedInMiddleware = new OnlyLoggedInMiddleware(
$this->reflector,
$this->userSession,
$this->urlGenerator
);
$this->onlyLoggedInMiddleware = $this->getMockBuilder(OnlyLoggedInMiddleware::class)
->setConstructorArgs([
$this->userSession,
$this->urlGenerator,
])
->onlyMethods(['hasAttribute'])
->getMock();

parent::setUp();
}

public function testBeforeControllerWithoutAnnotation(): void {
$this->reflector
$controller = $this->createMock(Controller::class);
$this->onlyLoggedInMiddleware
->expects($this->once())
->method('hasAnnotation')
->with('OnlyUnauthenticatedUsers')
->method('hasAttribute')
->with($controller, 'bar')
->willReturn(false);
$this->userSession
->expects($this->never())
->method('isLoggedIn');

$this->onlyLoggedInMiddleware->beforeController($this->createMock(Controller::class), 'bar');
$this->onlyLoggedInMiddleware->beforeController($controller, 'bar');
}

public function testBeforeControllerWithAnnotationAndNotLoggedIn(): void {
$this->reflector
$controller = $this->createMock(Controller::class);
$this->onlyLoggedInMiddleware
->expects($this->once())
->method('hasAnnotation')
->with('OnlyUnauthenticatedUsers')
->method('hasAttribute')
->with($controller, 'bar')
->willReturn(true);
$this->userSession
->expects($this->once())
->method('isLoggedIn')
->willReturn(false);

$this->onlyLoggedInMiddleware->beforeController($this->createMock(Controller::class), 'bar');
$this->onlyLoggedInMiddleware->beforeController($controller, 'bar');
}

public function testBeforeControllerWithAnnotationAndLoggedIn(): void {
$this->reflector
$controller = $this->createMock(Controller::class);
$this->onlyLoggedInMiddleware
->expects($this->once())
->method('hasAnnotation')
->with('OnlyUnauthenticatedUsers')
->method('hasAttribute')
->with($controller, 'bar')
->willReturn(true);
$this->userSession
->expects($this->once())
Expand All @@ -78,7 +80,7 @@ public function testBeforeControllerWithAnnotationAndLoggedIn(): void {
$this->expectException(Exception::class);
$this->expectExceptionMessage('User is already logged-in');

$this->onlyLoggedInMiddleware->beforeController($this->createMock(Controller::class), 'bar');
$this->onlyLoggedInMiddleware->beforeController($controller, 'bar');
}

public function testAfterExceptionWithNormalException(): void {
Expand Down
Loading