Skip to content

Commit 26cd37e

Browse files
committed
Merge #13 Central customization setup V31
2 parents f9761be + a140c36 commit 26cd37e

File tree

5 files changed

+147
-7
lines changed

5 files changed

+147
-7
lines changed

.github/README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# MagentaCLOUD user_oidc
2+
3+
Customisation of the Nextcloud delivered OpenID connect app for MagentaCLOUD.
4+
5+
The app extends the standard `user_oidc` Nextcloud app,
6+
see [upstream configuration hints for basic setup](https://github.com/nextcloud/user_oidc/blob/main/README.md)
7+
8+
9+
## Feature: Event-based provisioning (upstream contribution candidate)
10+
The mechanism allows to implement custom puser provisioning logic in a separate Nextcloud app by
11+
registering and handling a attribute change and provisioning event:
12+
13+
```
14+
use OCP\AppFramework\App;
15+
use OCP\AppFramework\Bootstrap\IBootContext;
16+
use OCP\AppFramework\Bootstrap\IBootstrap;
17+
use OCP\AppFramework\Bootstrap\IRegistrationContext;
18+
19+
class Application extends App implements IBootstrap {
20+
...
21+
public function register(IRegistrationContext $context): void {
22+
$context->registerEventListener(AttributeMappedEvent::class, MyUserAttributeListener::class);
23+
$context->registerEventListener(UserAccountChangeEvent::class, MyUserAccountChangeListener::class);
24+
}
25+
...
26+
}
27+
```
28+
The provisioning handler should return a `OCA\UserOIDC\Event\UserAccountChangeResult` object
29+
30+
## Feature: Telekom-specific bearer token
31+
32+
Due to historic reason, Telekom bearer tokens have a close to standard structure, but
33+
require special security implementation in detail. The customisation overrides te standard
34+
35+
36+
### Requiring web-token libraries
37+
The central configuration branch `nmc/2372-central-setup` automatic merge will frequently fail if composer
38+
upstream
39+
40+
The fast and easy way to bring it back to sync with upstream is:
41+
```
42+
git checkout nmc/2372-central-setup
43+
git rebase --onto main nmc/2372-central-setup
44+
# manually take over everything from upstream for composer.lock (TODO: automate that)
45+
46+
# ALWAYS update web-token dependencies in composer.lock
47+
# to avoid upstream conflicts. The lock file diff should only contain adds to upstream state!
48+
composer update "web-token/jwt-*"
49+
```
50+
51+
52+
### Configuring an additional Bearer preshared secret with provider
53+
TODO
54+
55+
### Testing Bearer secrets
56+
TODO

COPYING.DTAG

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Although this Nextcloud app code is free and available under the AGPL3 license, Deutsche Telekom
2+
(including T-Systems) fully reserves all rights to the Telekom brand. To prevent users from getting confused about
3+
the source of a digital product or experience, there are stringent restrictions on using the Telekom brand and design,
4+
even when built into code that we provide. For any customization other than explicitly for Telekom or T-Systems, you must
5+
replace the Deutsche Telekom and T-Systems brand elements contained in the provided sources.

appinfo/routes.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@
1717
['name' => 'login#singleLogoutService', 'url' => '/sls', 'verb' => 'GET'],
1818
['name' => 'login#backChannelLogout', 'url' => '/backchannel-logout/{providerIdentifier}', 'verb' => 'POST'],
1919

20-
['name' => 'api#createUser', 'url' => '/user', 'verb' => 'POST'],
21-
['name' => 'api#deleteUser', 'url' => '/user/{userId}', 'verb' => 'DELETE'],
20+
// compatibility with NMC V24 until reconfig on SAM
21+
['name' => 'login#telekomBackChannelLogout', 'url' => '/logout', 'verb' => 'POST'],
22+
23+
// this is a security problem combined with Telekom provisioning, so we have to disable the endpoint
24+
// ['name' => 'api#createUser', 'url' => '/user', 'verb' => 'POST'],
25+
// ['name' => 'api#deleteUser', 'url' => '/user/{userId}', 'verb' => 'DELETE'],
2226

2327
['name' => 'id4me#showLogin', 'url' => '/id4me', 'verb' => 'GET'],
2428
['name' => 'id4me#login', 'url' => '/id4me', 'verb' => 'POST'],

lib/AppInfo/Application.php

Lines changed: 76 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,27 @@
1919
use OCA\UserOIDC\Listener\ExternalTokenRequestedListener;
2020
use OCA\UserOIDC\Listener\InternalTokenRequestedListener;
2121
use OCA\UserOIDC\Listener\TimezoneHandlingListener;
22+
use OCA\UserOIDC\MagentaBearer\MBackend;
2223
use OCA\UserOIDC\Listener\TokenInvalidatedListener;
2324
use OCA\UserOIDC\Service\ID4MeService;
25+
use OCA\UserOIDC\Service\ProvisioningEventService;
26+
use OCA\UserOIDC\Service\ProvisioningService;
2427
use OCA\UserOIDC\Service\SettingsService;
25-
use OCA\UserOIDC\Service\TokenService;
26-
use OCA\UserOIDC\User\Backend;
2728
use OCP\AppFramework\App;
2829
use OCP\AppFramework\Bootstrap\IBootContext;
2930
use OCP\AppFramework\Bootstrap\IBootstrap;
3031
use OCP\AppFramework\Bootstrap\IRegistrationContext;
3132
use OCP\IConfig;
3233
use OCP\IL10N;
3334
use OCP\IRequest;
35+
use OCP\ISession;
3436
use OCP\IURLGenerator;
3537
use OCP\IUserManager;
3638
use OCP\IUserSession;
39+
use OCP\Security\ISecureRandom;
40+
41+
// this is needed only for the special, shortened client login flow
42+
use Psr\Container\ContainerInterface;
3743
use Throwable;
3844

3945
class Application extends App implements IBootstrap {
@@ -48,11 +54,19 @@ public function __construct(array $urlParams = []) {
4854
}
4955

5056
public function register(IRegistrationContext $context): void {
57+
// Register the composer autoloader required for the added jwt-token libs
58+
include_once __DIR__ . '/../../vendor/autoload.php';
59+
60+
// override registration of provisioning srevice to use event-based solution
61+
$this->getContainer()->registerService(ProvisioningService::class, function (ContainerInterface $c): ProvisioningService {
62+
return $c->get(ProvisioningEventService::class);
63+
});
64+
5165
/** @var IUserManager $userManager */
5266
$userManager = $this->getContainer()->get(IUserManager::class);
5367

5468
/* Register our own user backend */
55-
$this->backend = $this->getContainer()->get(Backend::class);
69+
$this->backend = $this->getContainer()->get(MBackend::class);
5670

5771
$config = $this->getContainer()->get(IConfig::class);
5872
if (version_compare($config->getSystemValueString('version', '0.0.0'), '32.0.0', '>=')) {
@@ -83,12 +97,69 @@ public function boot(IBootContext $context): void {
8397
try {
8498
$context->injectFn(\Closure::fromCallable([$this, 'registerRedirect']));
8599
$context->injectFn(\Closure::fromCallable([$this, 'registerLogin']));
100+
// this is the custom auto-redirect for MagentaCLOUD client access
101+
$context->injectFn(\Closure::fromCallable([$this, 'registerNmcClientFlow']));
86102
} catch (Throwable $e) {
87103
}
88104
}
89105

90-
private function checkLoginToken(TokenService $tokenService): void {
91-
$tokenService->checkLoginToken();
106+
/**
107+
* This is the automatic redirect exclusively for Nextcloud/Magentacloud clients completely skipping consent layer
108+
*/
109+
private function registerNmcClientFlow(IRequest $request,
110+
IURLGenerator $urlGenerator,
111+
ProviderMapper $providerMapper,
112+
ISession $session,
113+
ISecureRandom $random): void {
114+
$providers = $this->getCachedProviders($providerMapper);
115+
116+
// Handle immediate redirect on client first-time login
117+
$isClientLoginFlow = false;
118+
119+
try {
120+
$isClientLoginFlow = $request->getPathInfo() === '/login/flow';
121+
} catch (Exception $e) {
122+
// in case any errors happen when checking for the path do not apply redirect logic as it is only needed for the login
123+
}
124+
125+
if ($isClientLoginFlow) {
126+
// only redirect if Telekom provider registered
127+
$tproviders = array_values(array_filter($providers, function ($p) {
128+
return strtolower($p->getIdentifier()) === 'telekom';
129+
}));
130+
131+
if (count($tproviders) == 0) {
132+
// always show normal login flow as error fallback
133+
return;
134+
}
135+
136+
$stateToken = $random->generate(64, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
137+
$session->set('client.flow.state.token', $stateToken);
138+
139+
// call the service to get the params, but suppress the template
140+
// compute grant redirect Url to go directly to Telekom login
141+
$redirectUrl = $urlGenerator->linkToRoute('core.ClientFlowLogin.grantPage', [
142+
'stateToken' => $stateToken,
143+
// grantPage service operation is deriving oauth2 client name (again),
144+
// so we simply pass on clientIdentifier or empty string
145+
'clientIdentifier' => $request->getParam('clientIdentifier', ''),
146+
'direct' => $request->getParam('direct', '0')
147+
]);
148+
149+
if ($redirectUrl === null) {
150+
// always show normal login flow as error fallback
151+
return;
152+
}
153+
154+
// direct login, consent layer later
155+
$targetUrl = $urlGenerator->linkToRoute(self::APP_ID . '.login.login', [
156+
'providerId' => $tproviders[0]->getId(),
157+
'redirectUrl' => $redirectUrl
158+
]);
159+
160+
header('Location: ' . $targetUrl);
161+
exit();
162+
}
92163
}
93164

94165
private function registerRedirect(IRequest $request, IURLGenerator $urlGenerator, SettingsService $settings, ProviderMapper $providerMapper): void {

tests/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@
1818
require_once __DIR__ . '/../../../tests/autoload.php';
1919
require_once __DIR__ . '/../vendor/autoload.php';
2020

21+
\OC::$loader->addValidRoot(OC::$SERVERROOT . '/tests');
22+
\OC::$composerAutoloader->addPsr4('OCA\\UserOIDC\\BaseTest\\', dirname(__FILE__) . '/unit/MagentaCloud/', true);
2123
Server::get(IAppManager::class)->loadApp('user_oidc');
24+
25+
OC_Hook::clear();

0 commit comments

Comments
 (0)