-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathApplication.php
More file actions
164 lines (143 loc) · 6.35 KB
/
Application.php
File metadata and controls
164 lines (143 loc) · 6.35 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\UserOIDC\AppInfo;
use Exception;
use OC_App;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\UserOIDC\AlternativeLogin\AlternativeLoginProvider;
use OCA\UserOIDC\Db\ProviderMapper;
use OCA\UserOIDC\Event\ExchangedTokenRequestedEvent;
use OCA\UserOIDC\Event\ExternalTokenRequestedEvent;
use OCA\UserOIDC\Event\InternalTokenRequestedEvent;
use OCA\UserOIDC\Listener\ExchangedTokenRequestedListener;
use OCA\UserOIDC\Listener\ExternalTokenRequestedListener;
use OCA\UserOIDC\Listener\InternalTokenRequestedListener;
use OCA\UserOIDC\Listener\TimezoneHandlingListener;
use OCA\UserOIDC\Listener\TokenInvalidatedListener;
use OCA\UserOIDC\Service\ID4MeService;
use OCA\UserOIDC\Service\SettingsService;
use OCA\UserOIDC\Service\TokenService;
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\IConfig;
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);
$config = $this->getContainer()->get(IConfig::class);
if (version_compare($config->getSystemValueString('version', '0.0.0'), '32.0.0', '>=')) {
// see https://docs.nextcloud.com/server/latest/developer_manual/app_publishing_maintenance/app_upgrade_guide/upgrade_to_32.html#id3
$userManager->registerBackend($this->backend);
} else {
\OC_User::useBackend($this->backend);
}
$context->registerEventListener(LoadAdditionalScriptsEvent::class, TimezoneHandlingListener::class);
$context->registerEventListener(ExchangedTokenRequestedEvent::class, ExchangedTokenRequestedListener::class);
$context->registerEventListener(ExternalTokenRequestedEvent::class, ExternalTokenRequestedListener::class);
$context->registerEventListener(InternalTokenRequestedEvent::class, InternalTokenRequestedListener::class);
if (class_exists(\OCP\Authentication\Events\TokenInvalidatedEvent::class)) {
$context->registerEventListener(\OCP\Authentication\Events\TokenInvalidatedEvent::class, TokenInvalidatedListener::class);
}
if (version_compare($config->getSystemValueString('version', '0.0.0'), '34.0.0', '>=')) {
/**
* @psalm-suppress UndefinedInterfaceMethod
* @psalm-suppress MissingDependency
*/
$context->registerAlternativeLoginProvider(AlternativeLoginProvider::class);
}
}
public function boot(IBootContext $context): void {
$context->injectFn(\Closure::fromCallable([$this->backend, 'injectSession']));
$context->injectFn(\Closure::fromCallable([$this, 'checkLoginToken']));
/** @var IUserSession $userSession */
$userSession = $this->getContainer()->get(IUserSession::class);
if ($userSession->isLoggedIn()) {
return;
}
try {
$context->injectFn(\Closure::fromCallable([$this, 'registerRedirect']));
if (version_compare($this->getContainer()->get(IConfig::class)->getSystemValueString('version', '0.0.0'), '34.0.0', '<')) {
$context->injectFn(\Closure::fromCallable([$this, 'registerLogin']));
}
} catch (Throwable $e) {
}
}
private function checkLoginToken(TokenService $tokenService): void {
$tokenService->checkLoginToken();
}
private function registerRedirect(IRequest $request, IURLGenerator $urlGenerator, SettingsService $settings, ProviderMapper $providerMapper): void {
$redirectUrl = $request->getParam('redirect_url');
$absoluteRedirectUrl = !empty($redirectUrl) ? $urlGenerator->getAbsoluteURL($redirectUrl) : $redirectUrl;
// 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()) {
$providers = $this->getCachedProviders($providerMapper);
if (count($providers) === 1) {
$targetUrl = $urlGenerator->linkToRoute(self::APP_ID . '.login.login', [
'providerId' => $providers[0]->getId(),
'redirectUrl' => $absoluteRedirectUrl
]);
header('Location: ' . $targetUrl);
exit();
}
}
}
private function registerLogin(
IRequest $request, IL10N $l10n, IURLGenerator $urlGenerator, IConfig $config, ProviderMapper $providerMapper,
): void {
$redirectUrl = $request->getParam('redirect_url');
$absoluteRedirectUrl = !empty($redirectUrl) ? $urlGenerator->getAbsoluteURL($redirectUrl) : $redirectUrl;
$providers = $this->getCachedProviders($providerMapper);
$customLoginLabel = $config->getSystemValue('user_oidc', [])['login_label'] ?? '';
foreach ($providers as $provider) {
// FIXME: Move to IAlternativeLogin but requires boot due to db connection
OC_App::registerLogIn([
'name' => $customLoginLabel
? preg_replace('/{name}/', $provider->getIdentifier(), $customLoginLabel)
: $l10n->t('Login with %1s', [$provider->getIdentifier()]),
'href' => $urlGenerator->linkToRoute(self::APP_ID . '.login.login', ['providerId' => $provider->getId(), 'redirectUrl' => $absoluteRedirectUrl]),
]);
}
/** @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;
}
}