2016-06-29 00:04:23 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
require_once __DIR__ . '/../3rdparty/vendor/autoload.php';
|
|
|
|
|
2016-11-14 11:54:03 +01:00
|
|
|
// If we run in CLI mode do not setup the app as it can fail the OCC execution
|
|
|
|
// since the URLGenerator isn't accessible.
|
2017-01-04 20:14:45 +01:00
|
|
|
$cli = false;
|
2021-12-10 09:28:16 +01:00
|
|
|
if (OC::$CLI) {
|
2017-01-04 20:14:45 +01:00
|
|
|
$cli = true;
|
2016-11-14 11:54:03 +01:00
|
|
|
}
|
2019-07-04 11:23:34 +02:00
|
|
|
try {
|
|
|
|
$urlGenerator = \OC::$server->getURLGenerator();
|
|
|
|
$l = \OC::$server->getL10N('user_saml');
|
|
|
|
$config = \OC::$server->getConfig();
|
|
|
|
$request = \OC::$server->getRequest();
|
|
|
|
$userSession = \OC::$server->getUserSession();
|
|
|
|
$session = \OC::$server->getSession();
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
\OC::$server->getLogger()->logException($e);
|
|
|
|
return;
|
|
|
|
}
|
2021-11-09 02:45:45 +01:00
|
|
|
|
|
|
|
$samlSettings = \OC::$server->query(\OCA\User_SAML\SAMLSettings::class);
|
2016-06-29 00:04:23 +02:00
|
|
|
|
2020-04-22 15:52:10 +02:00
|
|
|
$userData = new \OCA\User_SAML\UserData(
|
|
|
|
new \OCA\User_SAML\UserResolver(\OC::$server->getUserManager()),
|
|
|
|
$samlSettings,
|
|
|
|
$config
|
|
|
|
);
|
|
|
|
|
2016-06-29 00:04:23 +02:00
|
|
|
$userBackend = new \OCA\User_SAML\UserBackend(
|
2016-09-26 22:06:17 +02:00
|
|
|
$config,
|
2016-11-14 11:54:03 +01:00
|
|
|
$urlGenerator,
|
2016-06-29 18:50:02 +02:00
|
|
|
\OC::$server->getSession(),
|
2017-02-16 11:47:45 +01:00
|
|
|
\OC::$server->getDatabaseConnection(),
|
2017-03-10 16:27:45 +01:00
|
|
|
\OC::$server->getUserManager(),
|
2018-07-11 12:22:45 +02:00
|
|
|
\OC::$server->getGroupManager(),
|
2018-09-21 17:55:36 +02:00
|
|
|
$samlSettings,
|
2020-04-22 15:52:10 +02:00
|
|
|
\OC::$server->getLogger(),
|
2022-02-18 09:26:09 +01:00
|
|
|
$userData,
|
|
|
|
\OC::$server->query(\OCP\EventDispatcher\IEventDispatcher::class),
|
2016-06-29 00:04:23 +02:00
|
|
|
);
|
2016-06-29 20:34:10 +02:00
|
|
|
$userBackend->registerBackends(\OC::$server->getUserManager()->getBackends());
|
2016-06-29 00:04:23 +02:00
|
|
|
OC_User::useBackend($userBackend);
|
|
|
|
|
2019-05-02 14:35:36 +02:00
|
|
|
$params = [];
|
|
|
|
|
2016-06-29 18:50:02 +02:00
|
|
|
// Setting up the one login config may fail, if so, do not catch the requests later.
|
2016-11-14 11:54:03 +01:00
|
|
|
$returnScript = false;
|
|
|
|
$type = '';
|
2021-12-10 09:28:16 +01:00
|
|
|
switch ($config->getAppValue('user_saml', 'type')) {
|
2016-11-14 11:54:03 +01:00
|
|
|
case 'saml':
|
|
|
|
$type = 'saml';
|
|
|
|
break;
|
|
|
|
case 'environment-variable':
|
|
|
|
$type = 'environment-variable';
|
|
|
|
break;
|
2018-08-14 09:39:08 +02:00
|
|
|
default:
|
|
|
|
return;
|
2016-11-14 11:54:03 +01:00
|
|
|
}
|
|
|
|
|
2018-10-29 23:05:34 +01:00
|
|
|
if ($type === 'environment-variable') {
|
2020-07-24 15:07:04 +02:00
|
|
|
// We should ignore oauth2 token endpoint (oauth can send the credentials as basic auth which will fail with apache auth)
|
|
|
|
$uri = $request->getRequestUri();
|
|
|
|
if (substr($uri, -24) === '/apps/oauth/api/v1/token') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-29 23:05:34 +01:00
|
|
|
OC_User::handleApacheAuth();
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:28:16 +01:00
|
|
|
if ($returnScript === true) {
|
2016-06-29 18:50:02 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-04 14:46:58 +01:00
|
|
|
$app = \OC::$server->query(\OCA\User_SAML\AppInfo\Application::class);
|
2018-03-12 19:07:03 +01:00
|
|
|
$app->registerDavAuth();
|
2018-03-05 18:44:39 +01:00
|
|
|
|
2016-09-26 21:40:59 +02:00
|
|
|
$redirectSituation = false;
|
2018-03-13 17:29:52 +01:00
|
|
|
|
|
|
|
$user = $userSession->getUser();
|
|
|
|
if ($user !== null) {
|
|
|
|
$enabled = $user->isEnabled();
|
|
|
|
if ($enabled === false) {
|
|
|
|
$targetUrl = $urlGenerator->linkToRouteAbsolute(
|
|
|
|
'user_saml.SAML.genericError',
|
|
|
|
[
|
|
|
|
'message' => $l->t('This user account is disabled, please contact your administrator.')
|
|
|
|
]
|
|
|
|
);
|
|
|
|
header('Location: '.$targetUrl);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 21:40:59 +02:00
|
|
|
// All requests that are not authenticated and match against the "/login" route are
|
|
|
|
// redirected to the SAML login endpoint
|
2021-12-10 09:28:16 +01:00
|
|
|
if (!$cli &&
|
2017-01-04 20:14:45 +01:00
|
|
|
!$userSession->isLoggedIn() &&
|
2022-05-12 14:48:31 +02:00
|
|
|
\OC::$server->getRequest()->getPathInfo() === '/login' &&
|
2017-01-19 00:29:30 +01:00
|
|
|
$type !== '') {
|
2019-05-02 14:35:36 +02:00
|
|
|
try {
|
|
|
|
$params = $request->getParams();
|
|
|
|
} catch (\LogicException $e) {
|
|
|
|
// ignore exception when PUT is called since getParams cannot parse parameters in that case
|
|
|
|
}
|
2022-05-09 13:16:43 +02:00
|
|
|
if (isset($params['direct']) && ($params['direct'] === 1 || $params['direct'] === '1')) {
|
2018-03-16 16:51:29 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-09-26 21:40:59 +02:00
|
|
|
$redirectSituation = true;
|
2016-06-29 00:04:23 +02:00
|
|
|
}
|
|
|
|
|
2016-06-29 12:06:50 +02:00
|
|
|
// If a request to OCS or remote.php is sent by the official desktop clients it can
|
|
|
|
// be intercepted as it supports SAML. All other clients don't yet and thus we
|
|
|
|
// require the usage of application specific passwords there.
|
2016-09-26 22:06:17 +02:00
|
|
|
//
|
|
|
|
// However, it is an opt-in setting to use SAML for the desktop clients. For better
|
|
|
|
// UX (users don't have to reauthenticate) we default to disallow the access via
|
|
|
|
// SAML at the moment.
|
|
|
|
$useSamlForDesktopClients = $config->getAppValue('user_saml', 'general-use_saml_auth_for_desktop', '0');
|
2021-12-10 09:28:16 +01:00
|
|
|
if ($useSamlForDesktopClients === '1') {
|
2022-04-11 14:34:23 +02:00
|
|
|
$currentUrl = substr(explode('?', $request->getRequestUri(), 2)[0], strlen(\OC::$WEBROOT));
|
2021-12-10 09:28:16 +01:00
|
|
|
if (substr($currentUrl, 0, 12) === '/remote.php/' || substr($currentUrl, 0, 5) === '/ocs/') {
|
|
|
|
if (!$userSession->isLoggedIn() && $request->isUserAgent([\OCP\IRequest::USER_AGENT_CLIENT_DESKTOP])) {
|
2016-09-26 22:06:17 +02:00
|
|
|
$redirectSituation = true;
|
2019-02-01 20:31:21 +01:00
|
|
|
|
|
|
|
if (preg_match('/^.*\/(\d+\.\d+\.\d+).*$/', $request->getHeader('USER_AGENT'), $matches) === 1) {
|
|
|
|
$versionstring = $matches[1];
|
|
|
|
|
|
|
|
if (version_compare($versionstring, '2.5.0', '>=') === true) {
|
|
|
|
$redirectSituation = false;
|
|
|
|
}
|
|
|
|
}
|
2016-09-26 22:06:17 +02:00
|
|
|
}
|
2016-06-29 12:06:50 +02:00
|
|
|
}
|
|
|
|
}
|
2016-09-26 21:40:59 +02:00
|
|
|
|
2018-07-10 17:33:51 +02:00
|
|
|
$multipleUserBackEnds = $samlSettings->allowMultipleUserBackEnds();
|
|
|
|
$configuredIdps = $samlSettings->getListOfIdps();
|
2022-07-28 14:31:35 +02:00
|
|
|
$showLoginOptions = ($multipleUserBackEnds || count($configuredIdps) > 1) && $type === 'saml';
|
2018-03-16 16:51:29 +01:00
|
|
|
|
2018-07-10 17:33:51 +02:00
|
|
|
if ($redirectSituation === true && $showLoginOptions) {
|
2019-05-02 14:35:36 +02:00
|
|
|
try {
|
|
|
|
$params = $request->getParams();
|
|
|
|
} catch (\LogicException $e) {
|
|
|
|
// ignore exception when PUT is called since getParams cannot parse parameters in that case
|
|
|
|
}
|
2018-03-16 16:51:29 +01:00
|
|
|
$redirectUrl = '';
|
2021-12-10 09:28:16 +01:00
|
|
|
if (isset($params['redirect_url'])) {
|
2018-03-16 16:51:29 +01:00
|
|
|
$redirectUrl = $params['redirect_url'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$targetUrl = $urlGenerator->linkToRouteAbsolute(
|
|
|
|
'user_saml.SAML.selectUserBackEnd',
|
|
|
|
[
|
|
|
|
'redirectUrl' => $redirectUrl
|
|
|
|
]
|
|
|
|
);
|
|
|
|
header('Location: '.$targetUrl);
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
2021-12-10 09:28:16 +01:00
|
|
|
if ($redirectSituation === true) {
|
2019-05-02 14:35:36 +02:00
|
|
|
try {
|
|
|
|
$params = $request->getParams();
|
|
|
|
} catch (\LogicException $e) {
|
|
|
|
// ignore exception when PUT is called since getParams cannot parse parameters in that case
|
|
|
|
}
|
2017-08-18 13:58:03 +02:00
|
|
|
$originalUrl = '';
|
2021-12-10 09:28:16 +01:00
|
|
|
if (isset($params['redirect_url'])) {
|
2017-08-18 13:58:03 +02:00
|
|
|
$originalUrl = $urlGenerator->getAbsoluteURL($params['redirect_url']);
|
|
|
|
}
|
|
|
|
|
2016-09-26 21:40:59 +02:00
|
|
|
$csrfToken = \OC::$server->getCsrfTokenManager()->getToken();
|
2017-08-18 13:58:03 +02:00
|
|
|
$targetUrl = $urlGenerator->linkToRouteAbsolute(
|
|
|
|
'user_saml.SAML.login',
|
|
|
|
[
|
|
|
|
'requesttoken' => $csrfToken->getEncryptedValue(),
|
|
|
|
'originalUrl' => $originalUrl,
|
2018-08-06 10:49:52 +02:00
|
|
|
'idp' => 1,
|
2017-08-18 13:58:03 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
header('Location: '.$targetUrl);
|
2016-09-26 21:40:59 +02:00
|
|
|
exit();
|
|
|
|
}
|