Skip to content

Commit

Permalink
feat(idps): Allow to filter idps based on trusted domains
Browse files Browse the repository at this point in the history
Signed-off-by: Jarkko Lehtoranta <[email protected]>
  • Loading branch information
jlehtoranta authored and juliusknorr committed Aug 16, 2024
1 parent 8c21eb3 commit 7a5ef2f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
}

$multipleUserBackEnds = $samlSettings->allowMultipleUserBackEnds();
$configuredIdps = $samlSettings->getListOfIdps();
$configuredIdps = $samlSettings->getListOfIdps($request);
$showLoginOptions = ($multipleUserBackEnds || count($configuredIdps) > 1) && $type === 'saml';

if ($redirectSituation === true && $showLoginOptions) {
Expand Down Expand Up @@ -152,7 +152,7 @@
[
'requesttoken' => $csrfToken->getEncryptedValue(),
'originalUrl' => $originalUrl,
'idp' => array_keys($configuredIdps)[0] ?? '',
'idp' => array_key_first($configuredIdps) ?? '',
]
);
header('Location: '.$targetUrl);
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/SAMLController.php
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ public function selectUserBackEnd(string $redirectUrl): Http\TemplateResponse {
*/
private function getIdps(string $redirectUrl): array {
$result = [];
$idps = $this->samlSettings->getListOfIdps();
$idps = $this->samlSettings->getListOfIdps($this->request);
foreach ($idps as $idpId => $displayName) {
$result[] = [
'url' => $this->getSSOUrl($redirectUrl, (string)$idpId),
Expand Down
12 changes: 11 additions & 1 deletion lib/SAMLSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class SAMLSettings {
// IdP-specific keys
public const IDP_CONFIG_KEYS = [
'general-idp0_display_name',
'general-custom_hosts',
'general-uid_mapping',
'idp-entityId',
'idp-singleLogoutService.responseUrl',
Expand Down Expand Up @@ -95,11 +96,20 @@ public function __construct(
* @return array<int, string>
* @throws Exception
*/
public function getListOfIdps(): array {
public function getListOfIdps(?\OCP\IRequest $request = null): array {
$serverHost = !is_null($request) ? $request->getServerHost() : null;

$this->ensureConfigurationsLoaded();

$result = [];
foreach ($this->configurations as $configID => $config) {
// Filter configs which don't match the trusted host in the request
if (!empty($serverHost)) {
$customHosts = key_exists('general-custom_hosts', $config) ? array_map('trim', explode(',', $config['general-custom_hosts'])) : [];
if (!in_array($serverHost, $customHosts)) {
continue;
}
}
// no fancy array_* method, because there might be thousands
$result[$configID] = $config['general-idp0_display_name'] ?? '';
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function getForm() {
'type' => 'line',
'required' => true,
],
'custom_hosts' => [
'text' => $this->l10n->t('Hostnames associated with this provider (e.g. nextcloud.a.com, nextcloud.b.com).'),
'type' => 'line',
],
'require_provisioned_account' => [
'text' => $this->l10n->t('Only allow authentication if an account exists on some other backend (e.g. LDAP).'),
'type' => 'checkbox',
Expand Down

0 comments on commit 7a5ef2f

Please sign in to comment.