Skip to content

Commit e8c669b

Browse files
committed
feat: Post login Idp
Signed-off-by: Hoang Pham <hoangmaths96@gmail.com>
1 parent 1f0a05c commit e8c669b

File tree

6 files changed

+43
-12
lines changed

6 files changed

+43
-12
lines changed

js/admin.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,10 +355,12 @@ $(function() {
355355
} else {
356356
$(this).val("0");
357357
}
358+
358359
if(key === 'require_provisioned_account') {
359360
$('#user-saml-attribute-mapping').toggleClass('hidden');
360361
$('#user-saml-filtering').toggleClass('hidden');
361362
}
363+
362364
OCA.User_SAML.Admin.setSamlConfigValue('general', key, $(this).val(), true);
363365
});
364366
});

lib/Controller/SAMLController.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
use OneLogin\Saml2\Error;
3535
use OneLogin\Saml2\Settings;
3636
use OneLogin\Saml2\ValidationError;
37+
use Psr\Container\ContainerExceptionInterface;
38+
use Psr\Container\NotFoundExceptionInterface;
3739
use Psr\Log\LoggerInterface;
3840

3941
class SAMLController extends Controller {
@@ -180,7 +182,7 @@ protected function assertGroupMemberships(): void {
180182
*
181183
* @throws Exception
182184
*/
183-
public function login(int $idp = 1) {
185+
public function login(int $idp = 1): Http\RedirectResponse|Http\TemplateResponse {
184186
$originalUrl = (string)$this->request->getParam('originalUrl', '');
185187
if (!$this->trustedDomainHelper->isTrustedUrl($originalUrl)) {
186188
$originalUrl = '';
@@ -194,7 +196,8 @@ public function login(int $idp = 1) {
194196
$returnUrl = $originalUrl ?: $this->urlGenerator->linkToRouteAbsolute('user_saml.SAML.login');
195197
$ssoUrl = $auth->login($returnUrl, [], false, false, true);
196198

197-
$method = $this->request->getParam('method', 'get');
199+
$settings = $this->samlSettings->get($idp);
200+
$method = $settings['general-saml_request_method'] ?? 'get';
198201
if ($method === 'post') {
199202
$query = parse_url($ssoUrl, PHP_URL_QUERY);
200203
parse_str($query, $params);
@@ -630,6 +633,11 @@ private function getIdps(string $redirectUrl): array {
630633
return $result;
631634
}
632635

636+
/**
637+
* @throws ContainerExceptionInterface
638+
* @throws NotFoundExceptionInterface
639+
* @throws \OCP\DB\Exception
640+
*/
633641
private function getSSOUrl(string $redirectUrl, string $idp): string {
634642
$originalUrl = '';
635643
if (!empty($redirectUrl)) {
@@ -639,17 +647,19 @@ private function getSSOUrl(string $redirectUrl, string $idp): string {
639647
/** @var CsrfTokenManager $csrfTokenManager */
640648
$csrfTokenManager = Server::get(CsrfTokenManager::class);
641649
$csrfToken = $csrfTokenManager->getToken();
642-
$ssoUrl = $this->urlGenerator->linkToRouteAbsolute(
650+
651+
$settings = $this->samlSettings->get((int)$idp);
652+
$method = $settings['general-saml_request_method'] ?? 'get';
653+
654+
return $this->urlGenerator->linkToRouteAbsolute(
643655
'user_saml.SAML.login',
644656
[
645657
'requesttoken' => $csrfToken->getEncryptedValue(),
646658
'originalUrl' => $originalUrl,
647659
'idp' => $idp,
648-
'method' => 'post',
660+
'method' => $method,
649661
]
650662
);
651-
652-
return $ssoUrl;
653663
}
654664

655665
/**

lib/SAMLSettings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class SAMLSettings {
2626
public const IDP_CONFIG_KEYS = [
2727
'general-idp0_display_name',
2828
'general-uid_mapping',
29+
'general-saml_request_method',
2930
'idp-entityId',
3031
'idp-singleLogoutService.responseUrl',
3132
'idp-singleLogoutService.url',

lib/Settings/Admin.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function getForm() {
8585
'text' => $this->l10n->t('Only allow authentication if an account exists on some other backend (e.g. LDAP).'),
8686
'type' => 'checkbox',
8787
'global' => true,
88-
]
88+
],
8989
];
9090
$attributeMappingSettings = [
9191
'displayName_mapping' => [
@@ -199,6 +199,11 @@ public function getForm() {
199199
'type' => 'line',
200200
'required' => false,
201201
];
202+
$generalSettings['saml_request_method'] = [
203+
'text' => $this->l10n->t('Use POST method for SAML request (default: GET)'),
204+
'type' => 'checkbox',
205+
'required' => false,
206+
];
202207
$generalSettings['allow_multiple_user_back_ends'] = [
203208
'text' => $this->l10n->t('Allow the use of multiple user back-ends (e.g. LDAP)'),
204209
'type' => 'checkbox',

templates/login_post.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,26 @@
44
* SPDX-License-Identifier: AGPL-3.0-or-later
55
*/
66

7-
/** @var array $_ */
7+
/**
8+
* @var array $_
9+
* @var IL10N $l
10+
*
11+
*/
12+
13+
use OCP\IL10N;
14+
15+
p($l->t('Please wait while you are redirected to the SSO server.'));
816
?>
9-
Please wait while you are redirected to the SSO server.
1017

1118
<form action="<?= $_['ssoUrl'] ?>" method="post">
1219
<input type="hidden" name="SAMLRequest" value="<?= $_['samlRequest'] ?>" />
1320
<input type="hidden" name="RelayState" value="<?= $_['relayState'] ?>" />
1421
<input type="hidden" name="SigAlg" value="<?= $_['sigAlg'] ?>" />
1522
<input type="hidden" name="Signature" value="<?= $_['signature'] ?>" />
1623
<noscript>
17-
<p>JavaScript is disabled. Click the button below to continue.</p>
24+
<p>
25+
<?php p($l->t('Please wait while you are redirected to the SSO server.')); ?>
26+
</p>
1827
<input type="submit" value="Continue" />
1928
</noscript>
2029
</form>

tests/unit/Settings/AdminTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public function formDataProvider() {
8484
'type' => 'line',
8585
'required' => false,
8686
],
87+
'saml_request_method' => [
88+
'text' => $this->l10n->t('Use POST method for SAML request (default: GET)'),
89+
'type' => 'checkbox',
90+
'required' => false,
91+
],
8792
'uid_mapping' => [
8893
'text' => 'Attribute to map the UID to.',
8994
'type' => 'line',
@@ -226,8 +231,7 @@ public function testGetFormWithoutType() {
226231
->willReturn('');
227232

228233
$params = $this->formDataProvider();
229-
unset($params['general']['idp0_display_name']);
230-
unset($params['general']['allow_multiple_user_back_ends']);
234+
unset($params['general']['idp0_display_name'], $params['general']['saml_request_method'], $params['general']['allow_multiple_user_back_ends']);
231235
$params['type'] = '';
232236

233237
$expected = new TemplateResponse('user_saml', 'admin', $params);

0 commit comments

Comments
 (0)