-
Notifications
You must be signed in to change notification settings - Fork 35
/
Application.php
135 lines (118 loc) · 4.74 KB
/
Application.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
declare(strict_types=1);
/**
* @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
*
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @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/>.
*
*/
namespace OCA\UserOIDC\AppInfo;
use Exception;
use OC_App;
use OC_User;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\UserOIDC\Db\ProviderMapper;
use OCA\UserOIDC\Listener\TimezoneHandlingListener;
use OCA\UserOIDC\Service\ID4MeService;
use OCA\UserOIDC\Service\SettingsService;
use OCA\UserOIDC\User\Backend;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\IL10N;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\IUserSession;
use Throwable;
class Application extends App implements IBootstrap {
public const APP_ID = 'user_oidc';
public const OIDC_API_REQ_HEADER = 'Authorization';
private $backend;
private $cachedProviders;
public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);
}
public function register(IRegistrationContext $context): void {
/** @var IUserManager $userManager */
$userManager = $this->getContainer()->get(IUserManager::class);
/* Register our own user backend */
$this->backend = $this->getContainer()->get(Backend::class);
$userManager->registerBackend($this->backend);
OC_User::useBackend($this->backend);
$context->registerEventListener(LoadAdditionalScriptsEvent::class, TimezoneHandlingListener::class);
}
public function boot(IBootContext $context): void {
$context->injectFn(\Closure::fromCallable([$this->backend, 'injectSession']));
/** @var IUserSession $userSession */
$userSession = $this->getContainer()->get(IUserSession::class);
if ($userSession->isLoggedIn()) {
return;
}
try {
$context->injectFn(\Closure::fromCallable([$this, 'registerRedirect']));
$context->injectFn(\Closure::fromCallable([$this, 'registerLogin']));
} catch (Throwable $e) {
}
}
private function registerRedirect(IRequest $request, IURLGenerator $urlGenerator, SettingsService $settings, ProviderMapper $providerMapper): void {
$providers = $this->getCachedProviders($providerMapper);
$redirectUrl = $request->getParam('redirect_url');
// Handle immediate redirect to the oidc provider if just one is configured and no other backends are allowed
$isDefaultLogin = false;
try {
$isDefaultLogin = $request->getPathInfo() === '/login' && $request->getParam('direct') !== '1';
} catch (Exception $e) {
// in case any errors happen when checking for the path do not apply redirect logic as it is only needed for the login
}
if ($isDefaultLogin && !$settings->getAllowMultipleUserBackEnds() && count($providers) === 1) {
$targetUrl = $urlGenerator->linkToRoute(self::APP_ID . '.login.login', [
'providerId' => $providers[0]->getId(),
'redirectUrl' => $redirectUrl
]);
header('Location: ' . $targetUrl);
exit();
}
}
private function registerLogin(IRequest $request, IL10N $l10n, IURLGenerator $urlGenerator, ProviderMapper $providerMapper): void {
$redirectUrl = $request->getParam('redirect_url');
$providers = $this->getCachedProviders($providerMapper);
foreach ($providers as $provider) {
// FIXME: Move to IAlternativeLogin but requires boot due to db connection
OC_App::registerLogIn([
'name' => $l10n->t('Login with %1s', [$provider->getIdentifier()]),
'href' => $urlGenerator->linkToRoute(self::APP_ID . '.login.login', ['providerId' => $provider->getId(), 'redirectUrl' => $redirectUrl]),
]);
}
/** @var ID4MeService $id4meService */
$id4meService = $this->getContainer()->get(ID4MeService::class);
if ($id4meService->getID4ME()) {
OC_App::registerLogIn([
'name' => 'ID4ME',
'href' => $urlGenerator->linkToRoute(self::APP_ID . '.id4me.login'),
]);
}
}
private function getCachedProviders(ProviderMapper $providerMapper): array {
if (!isset($this->cachedProviders)) {
$this->cachedProviders = $providerMapper->getProviders();
}
return $this->cachedProviders;
}
}