Skip to content

Commit cdf8015

Browse files
committed
feat(idps): Allow to filter idps based on trusted domains
1 parent 8d43257 commit cdf8015

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

appinfo/app.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
}
152152

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

157157
if ($redirectSituation === true && $showLoginOptions) {
@@ -192,7 +192,7 @@
192192
[
193193
'requesttoken' => $csrfToken->getEncryptedValue(),
194194
'originalUrl' => $originalUrl,
195-
'idp' => array_keys($configuredIdps)[0] ?? '',
195+
'idp' => array_key_first($configuredIdps) ?? '',
196196
]
197197
);
198198
header('Location: '.$targetUrl);

lib/Controller/SAMLController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ public function selectUserBackEnd(string $redirectUrl): Http\TemplateResponse {
608608
*/
609609
private function getIdps(string $redirectUrl): array {
610610
$result = [];
611-
$idps = $this->samlSettings->getListOfIdps();
611+
$idps = $this->samlSettings->getListOfIdps($this->request);
612612
foreach ($idps as $idpId => $displayName) {
613613
$result[] = [
614614
'url' => $this->getSSOUrl($redirectUrl, (string)$idpId),

lib/SAMLSettings.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class SAMLSettings {
4040
// IdP-specific keys
4141
public const IDP_CONFIG_KEYS = [
4242
'general-idp0_display_name',
43+
'general-custom_hosts',
4344
'general-uid_mapping',
4445
'idp-entityId',
4546
'idp-singleLogoutService.responseUrl',
@@ -110,11 +111,20 @@ public function __construct(
110111
* @return array<int, string>
111112
* @throws Exception
112113
*/
113-
public function getListOfIdps(): array {
114+
public function getListOfIdps(?\OCP\IRequest $request = null): array {
115+
$serverHost = !is_null($request) ? $request->getServerHost() : null;
116+
114117
$this->ensureConfigurationsLoaded();
115118

116119
$result = [];
117120
foreach ($this->configurations as $configID => $config) {
121+
// Filter configs which don't match the trusted host in the request
122+
if (!empty($serverHost)) {
123+
$customHosts = key_exists('general-custom_hosts', $config) ? array_map('trim', explode(',', $config['general-custom_hosts'])) : [];
124+
if (!in_array($serverHost, $customHosts)) {
125+
continue;
126+
}
127+
}
118128
// no fancy array_* method, because there might be thousands
119129
$result[$configID] = $config['general-idp0_display_name'] ?? '';
120130
}

lib/Settings/Admin.php

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ public function getForm() {
9898
'type' => 'line',
9999
'required' => true,
100100
],
101+
'custom_hosts' => [
102+
'text' => $this->l10n->t('Hostnames associated with this provider (e.g. nextcloud.a.com, nextcloud.b.com).'),
103+
'type' => 'line',
104+
],
101105
'require_provisioned_account' => [
102106
'text' => $this->l10n->t('Only allow authentication if an account exists on some other backend (e.g. LDAP).'),
103107
'type' => 'checkbox',

0 commit comments

Comments
 (0)