Skip to content

Commit 7a5ef2f

Browse files
jlehtorantajuliusknorr
authored andcommitted
feat(idps): Allow to filter idps based on trusted domains
Signed-off-by: Jarkko Lehtoranta <[email protected]>
1 parent 8c21eb3 commit 7a5ef2f

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
@@ -111,7 +111,7 @@
111111
}
112112

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

117117
if ($redirectSituation === true && $showLoginOptions) {
@@ -152,7 +152,7 @@
152152
[
153153
'requesttoken' => $csrfToken->getEncryptedValue(),
154154
'originalUrl' => $originalUrl,
155-
'idp' => array_keys($configuredIdps)[0] ?? '',
155+
'idp' => array_key_first($configuredIdps) ?? '',
156156
]
157157
);
158158
header('Location: '.$targetUrl);

lib/Controller/SAMLController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ public function selectUserBackEnd(string $redirectUrl): Http\TemplateResponse {
592592
*/
593593
private function getIdps(string $redirectUrl): array {
594594
$result = [];
595-
$idps = $this->samlSettings->getListOfIdps();
595+
$idps = $this->samlSettings->getListOfIdps($this->request);
596596
foreach ($idps as $idpId => $displayName) {
597597
$result[] = [
598598
'url' => $this->getSSOUrl($redirectUrl, (string)$idpId),

lib/SAMLSettings.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class SAMLSettings {
2525
// IdP-specific keys
2626
public const IDP_CONFIG_KEYS = [
2727
'general-idp0_display_name',
28+
'general-custom_hosts',
2829
'general-uid_mapping',
2930
'idp-entityId',
3031
'idp-singleLogoutService.responseUrl',
@@ -95,11 +96,20 @@ public function __construct(
9596
* @return array<int, string>
9697
* @throws Exception
9798
*/
98-
public function getListOfIdps(): array {
99+
public function getListOfIdps(?\OCP\IRequest $request = null): array {
100+
$serverHost = !is_null($request) ? $request->getServerHost() : null;
101+
99102
$this->ensureConfigurationsLoaded();
100103

101104
$result = [];
102105
foreach ($this->configurations as $configID => $config) {
106+
// Filter configs which don't match the trusted host in the request
107+
if (!empty($serverHost)) {
108+
$customHosts = key_exists('general-custom_hosts', $config) ? array_map('trim', explode(',', $config['general-custom_hosts'])) : [];
109+
if (!in_array($serverHost, $customHosts)) {
110+
continue;
111+
}
112+
}
103113
// no fancy array_* method, because there might be thousands
104114
$result[$configID] = $config['general-idp0_display_name'] ?? '';
105115
}

lib/Settings/Admin.php

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public function getForm() {
8181
'type' => 'line',
8282
'required' => true,
8383
],
84+
'custom_hosts' => [
85+
'text' => $this->l10n->t('Hostnames associated with this provider (e.g. nextcloud.a.com, nextcloud.b.com).'),
86+
'type' => 'line',
87+
],
8488
'require_provisioned_account' => [
8589
'text' => $this->l10n->t('Only allow authentication if an account exists on some other backend (e.g. LDAP).'),
8690
'type' => 'checkbox',

0 commit comments

Comments
 (0)