Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix potential login -> logout loop when allow_multiple_user_backends is 0 #761

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public function boot(IBootContext $context): void {
}

private function registerRedirect(IRequest $request, IURLGenerator $urlGenerator, SettingsService $settings, ProviderMapper $providerMapper): void {
// TODO when min supported version is >=28 :
// run this in a listener of OCP\AppFramework\Http\Events\BeforeLoginTemplateRenderedEvent
// to avoid doing useless stuff on data requests and template requests that are not the login page

$providers = $this->getCachedProviders($providerMapper);
$redirectUrl = $request->getParam('redirect_url');

Expand All @@ -98,6 +102,16 @@ private function registerRedirect(IRequest $request, IURLGenerator $urlGenerator
// in case any errors happen when checking for the path do not apply redirect logic as it is only needed for the login
}
if ($isDefaultLogin && !$settings->getAllowMultipleUserBackEnds() && count($providers) === 1) {
// To avoid login/logout loop if the IdP session is still alive:
// if the login page's redirect_url GET param is the logout page, just use the base URL instead
$logoutUrl = $urlGenerator->linkToRoute('core.login.logout');
$userOidcLogoutUrl = $urlGenerator->linkToRoute(self::APP_ID . '.login.singleLogoutService');
if (
$redirectUrl
&& (strpos($redirectUrl, $logoutUrl) !== false || strpos($redirectUrl, $userOidcLogoutUrl) !== false)
) {
$redirectUrl = $urlGenerator->getBaseUrl();
}
$targetUrl = $urlGenerator->linkToRoute(self::APP_ID . '.login.login', [
'providerId' => $providers[0]->getId(),
'redirectUrl' => $redirectUrl
Expand Down
10 changes: 10 additions & 0 deletions lib/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,16 @@ private function buildProtocolErrorResponse(?bool $throttle = null): TemplateRes
* @return DataDisplayResponse|RedirectResponse|TemplateResponse
*/
public function login(int $providerId, string $redirectUrl = null) {
// to be safe, avoid redirecting to logout or single-logout
$logoutUrl = $this->urlGenerator->linkToRoute('core.login.logout');
$userOidcLogoutUrl = $this->urlGenerator->linkToRoute(Application::APP_ID . '.login.singleLogoutService');
if (
$redirectUrl
&& (strpos($redirectUrl, $logoutUrl) !== false || strpos($redirectUrl, $userOidcLogoutUrl) !== false)
) {
$redirectUrl = $this->urlGenerator->getBaseUrl();
}

if ($this->userSession->isLoggedIn()) {
return new RedirectResponse($redirectUrl);
}
Expand Down
Loading