-
Notifications
You must be signed in to change notification settings - Fork 84
feat: Implement IAlternativeLoginProvider #1176
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?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\AlternativeLogin; | ||
|
|
||
| use OCP\Authentication\IAlternativeLogin; | ||
|
|
||
| class AlternativeLogin implements IAlternativeLogin { | ||
| public function __construct( | ||
| private readonly string $name, | ||
| private readonly string $href, | ||
| ) { | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function getLabel(): string { | ||
| return $this->name; | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function getLink(): string { | ||
| return $this->href; | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function getClass(): string { | ||
| return ''; | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function load(): void { | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?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\AlternativeLogin; | ||
|
|
||
| use OCA\User_SAML\Service\ProviderListingService; | ||
| use OCP\AppFramework\Services\IAppConfig; | ||
| use OCP\Authentication\IAlternativeLoginProvider; | ||
| use OCP\IRequest; | ||
| use OCP\IURLGenerator; | ||
|
|
||
| /** | ||
| * @psalm-suppress UndefinedClass IAlternativeLoginProvider is only defined in NC >= 34 | ||
| */ | ||
| class AlternativeLoginProvider implements IAlternativeLoginProvider { | ||
| public function __construct( | ||
| private readonly IRequest $request, | ||
| private readonly IUrlGenerator $urlGenerator, | ||
| private readonly ProviderListingService $providerListingService, | ||
| private readonly IAppConfig $appConfig, | ||
| ) { | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function getAlternativeLogins(): array { | ||
| $type = $this->appConfig->getAppValueString('type'); | ||
|
|
||
| if ($type !== 'saml') { | ||
| return []; | ||
| } | ||
|
|
||
| $redirectUrl = $this->request->getParam('redirect_url') ?? ''; | ||
| $absoluteRedirectUrl = $this->urlGenerator->getAbsoluteURL($redirectUrl); | ||
| return array_map(fn (array $idp): AlternativeLogin | ||
| => new AlternativeLogin($idp['display-name'], $idp['url']), $this->providerListingService->getIdps($absoluteRedirectUrl)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| <?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\Service; | ||
|
|
||
| use OC\Security\CSRF\CsrfTokenManager; | ||
| use OCA\User_SAML\SAMLSettings; | ||
| use OCP\IL10N; | ||
| use OCP\IURLGenerator; | ||
| use Psr\Container\ContainerExceptionInterface; | ||
| use Psr\Container\NotFoundExceptionInterface; | ||
|
|
||
| class ProviderListingService { | ||
| public function __construct( | ||
| private readonly IL10N $l10n, | ||
| private readonly IUrlGenerator $urlGenerator, | ||
| private readonly SAMLSettings $samlSettings, | ||
| private readonly CsrfTokenManager $csrfTokenManager, | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Return the display name of the SSO identity provider. | ||
| */ | ||
| protected function getSSODisplayName(?string $displayName): string { | ||
| if ($displayName === null || $displayName === '') { | ||
| $displayName = $this->l10n->t('SSO & SAML log in'); | ||
| } | ||
|
|
||
| return $displayName; | ||
| } | ||
|
|
||
| /** | ||
| * @throws ContainerExceptionInterface | ||
| * @throws NotFoundExceptionInterface | ||
| * @throws \OCP\DB\Exception | ||
| */ | ||
| private function getSSOUrl(string $redirectUrl, int $idp): string { | ||
| $originalUrl = ''; | ||
| if (!empty($redirectUrl)) { | ||
| $originalUrl = $this->urlGenerator->getAbsoluteURL($redirectUrl); | ||
| } | ||
|
|
||
| $csrfToken = $this->csrfTokenManager->getToken(); | ||
|
|
||
| $settings = $this->samlSettings->get($idp); | ||
| $method = $settings['general-is_saml_request_using_post'] ?? 'get'; | ||
|
|
||
| return $this->urlGenerator->linkToRouteAbsolute( | ||
| 'user_saml.SAML.login', | ||
| [ | ||
| 'requesttoken' => $csrfToken->getEncryptedValue(), | ||
| 'originalUrl' => $originalUrl, | ||
| 'idp' => (string)$idp, | ||
| 'method' => $method, | ||
| ] | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the IdPs showed at the login page | ||
| * | ||
| * @return list<array{url: string, display-name: string}> | ||
| */ | ||
| public function getIdps(string $redirectUrl): array { | ||
| $result = []; | ||
| $idps = $this->samlSettings->getListOfIdps(); | ||
| foreach ($idps as $idpId => $displayName) { | ||
| $result[] = [ | ||
| 'url' => $this->getSSOUrl($redirectUrl, $idpId), | ||
| 'display-name' => $this->getSSODisplayName($displayName), | ||
| ]; | ||
| } | ||
|
|
||
| return $result; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this code path, which leads to a later call to
getIdps()thengetSSOUrl()results in the absolute version of the redirecturl being duplicated. Symptoms reported on the community help forum: https://help.nextcloud.com/t/double-url-with-saml-app-azure-loginpage-wrong-redirect/247252There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in #1196