Skip to content

Commit 3d04f94

Browse files
authored
Merge pull request #1262 from nextcloud/enh/noid/lazy-loading
Lazy config loading
2 parents f440dea + dcdeb63 commit 3d04f94

File tree

11 files changed

+120
-51
lines changed

11 files changed

+120
-51
lines changed

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<name>OpenID Connect user backend</name>
99
<summary>Use an OpenID Connect backend to login to your Nextcloud</summary>
1010
<description>Allows flexible configuration of an OIDC server as Nextcloud login user backend.</description>
11-
<version>8.1.0</version>
11+
<version>8.1.1</version>
1212
<licence>agpl</licence>
1313
<author>Roeland Jago Douma</author>
1414
<author>Julius Härtl</author>

lib/Controller/LoginController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ public function code(string $state = '', string $code = '', string $scope = '',
601601
$this->eventDispatcher->dispatchTyped(new UserLoggedInEvent($user, $user->getUID(), null, false));
602602
}
603603

604-
$storeLoginTokenEnabled = $this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0') === '1';
604+
$storeLoginTokenEnabled = $this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0', lazy: true) === '1';
605605
if ($storeLoginTokenEnabled) {
606606
// store all token information for potential token exchange requests
607607
$tokenData = array_merge(

lib/Controller/SettingsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function setID4ME(bool $enabled): JSONResponse {
180180
public function setAdminConfig(array $values): JSONResponse {
181181
foreach ($values as $key => $value) {
182182
if ($key === 'store_login_token' && is_bool($value)) {
183-
$this->appConfig->setValueString(Application::APP_ID, 'store_login_token', $value ? '1' : '0');
183+
$this->appConfig->setValueString(Application::APP_ID, 'store_login_token', $value ? '1' : '0', lazy: true);
184184
}
185185
}
186186
return new JSONResponse([]);

lib/Listener/ExternalTokenRequestedListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function handle(Event $event): void {
4242

4343
$this->logger->debug('[ExternalTokenRequestedListener] received request');
4444

45-
$storeLoginTokenEnabled = $this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0') === '1';
45+
$storeLoginTokenEnabled = $this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0', lazy: true) === '1';
4646
if (!$storeLoginTokenEnabled) {
4747
throw new GetExternalTokenFailedException('Failed to get external token, login token is not stored', 0);
4848
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\UserOIDC\Migration;
11+
12+
use Closure;
13+
use OCA\UserOIDC\AppInfo\Application;
14+
use OCA\UserOIDC\Db\ProviderMapper;
15+
use OCA\UserOIDC\Service\ProviderService;
16+
use OCP\IAppConfig;
17+
use OCP\Migration\IOutput;
18+
use OCP\Migration\SimpleMigrationStep;
19+
20+
class Version080101Date20251201121749 extends SimpleMigrationStep {
21+
22+
public function __construct(
23+
private IAppConfig $appConfig,
24+
private ProviderService $providerService,
25+
private ProviderMapper $providerMapper,
26+
) {
27+
}
28+
29+
/**
30+
* @param IOutput $output
31+
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
32+
* @param array $options
33+
*/
34+
public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
35+
// make admin settings lazy
36+
$keys = [
37+
'store_login_token',
38+
'id4me_enabled',
39+
'allow_multiple_user_backends',
40+
];
41+
foreach ($keys as $key) {
42+
try {
43+
if ($this->appConfig->hasKey(Application::APP_ID, $key)) {
44+
$value = $this->appConfig->getValueString(Application::APP_ID, $key);
45+
$this->appConfig->setValueString(Application::APP_ID, $key, $value, lazy: true);
46+
}
47+
} catch (\Exception) {
48+
}
49+
}
50+
51+
// make all provider settings lazy
52+
$providers = $this->providerMapper->getProviders();
53+
$supportedSettingKeys = $this->providerService->getSupportedSettings();
54+
$supportedSettingKeys[] = ProviderService::SETTING_JWKS_CACHE;
55+
$supportedSettingKeys[] = ProviderService::SETTING_JWKS_CACHE_TIMESTAMP;
56+
foreach ($supportedSettingKeys as $key) {
57+
foreach ($providers as $provider) {
58+
$realKey = $this->providerService->getSettingsKey($provider->getId(), $key);
59+
if ($this->appConfig->hasKey(Application::APP_ID, $realKey)) {
60+
try {
61+
$value = $this->appConfig->getValueString(Application::APP_ID, $realKey);
62+
$this->appConfig->setValueString(Application::APP_ID, $realKey, $value, lazy: true);
63+
} catch (\Exception) {
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}

lib/Service/ID4MeService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public function __construct(
1919
}
2020

2121
public function setID4ME(bool $enabled): void {
22-
$this->appConfig->setValueString(Application::APP_ID, 'id4me_enabled', $enabled ? '1' : '0');
22+
$this->appConfig->setValueString(Application::APP_ID, 'id4me_enabled', $enabled ? '1' : '0', lazy: true);
2323
}
2424

2525
public function getID4ME(): bool {
26-
return $this->appConfig->getValueString(Application::APP_ID, 'id4me_enabled', '0') === '1';
26+
return $this->appConfig->getValueString(Application::APP_ID, 'id4me_enabled', '0', lazy: true) === '1';
2727
}
2828
}

lib/Service/ProviderService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,22 @@ public function deleteSettings(int $providerId): void {
125125
}
126126

127127
public function setSetting(int $providerId, string $key, string $value): void {
128-
$this->appConfig->setValueString(Application::APP_ID, $this->getSettingsKey($providerId, $key), $value);
128+
$this->appConfig->setValueString(Application::APP_ID, $this->getSettingsKey($providerId, $key), $value, lazy: true);
129129
}
130130

131131
public function getSetting(int $providerId, string $key, string $default = ''): string {
132-
$value = $this->appConfig->getValueString(Application::APP_ID, $this->getSettingsKey($providerId, $key), '');
132+
$value = $this->appConfig->getValueString(Application::APP_ID, $this->getSettingsKey($providerId, $key), '', lazy: true);
133133
if ($value === '') {
134134
return $default;
135135
}
136136
return $value;
137137
}
138138

139-
private function getSettingsKey(int $providerId, string $key): string {
139+
public function getSettingsKey(int $providerId, string $key): string {
140140
return 'provider-' . strval($providerId) . '-' . $key;
141141
}
142142

143-
private function getSupportedSettings(): array {
143+
public function getSupportedSettings(): array {
144144
return [
145145
self::SETTING_MAPPING_DISPLAYNAME,
146146
self::SETTING_MAPPING_EMAIL,

lib/Service/SettingsService.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(
2525

2626
public function getAllowMultipleUserBackEnds(): bool {
2727
try {
28-
return $this->appConfig->getValueString(Application::APP_ID, 'allow_multiple_user_backends', '1') === '1';
28+
return $this->appConfig->getValueString(Application::APP_ID, 'allow_multiple_user_backends', '1', lazy: true) === '1';
2929
} catch (AppConfigTypeConflictException $e) {
3030
$this->logger->warning('Incorrect app config type when getting "allow_multiple_user_backends"', ['exception' => $e]);
3131
return true;
@@ -34,11 +34,11 @@ public function getAllowMultipleUserBackEnds(): bool {
3434

3535
public function setAllowMultipleUserBackEnds(bool $value): void {
3636
try {
37-
$this->appConfig->setValueString(Application::APP_ID, 'allow_multiple_user_backends', $value ? '1' : '0');
37+
$this->appConfig->setValueString(Application::APP_ID, 'allow_multiple_user_backends', $value ? '1' : '0', lazy: true);
3838
} catch (AppConfigTypeConflictException $e) {
3939
$this->logger->warning('Incorrect app config type when setting "allow_multiple_user_backends"', ['exception' => $e]);
4040
$this->appConfig->deleteKey(Application::APP_ID, 'allow_multiple_user_backends');
41-
$this->appConfig->setValueString(Application::APP_ID, 'allow_multiple_user_backends', $value ? '1' : '0');
41+
$this->appConfig->setValueString(Application::APP_ID, 'allow_multiple_user_backends', $value ? '1' : '0', lazy: true);
4242
}
4343
}
4444
}

lib/Service/TokenService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ public function getToken(bool $refreshIfExpired = true): ?Token {
116116
* @throws PreConditionNotMetException
117117
*/
118118
public function checkLoginToken(): void {
119-
$storeLoginTokenEnabled = $this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0') === '1';
119+
$storeLoginTokenEnabled = $this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0', lazy: true) === '1';
120120
if (!$storeLoginTokenEnabled) {
121121
return;
122122
}
@@ -255,7 +255,7 @@ public function decodeIdToken(Token $token): array {
255255
* @throws \JsonException
256256
*/
257257
public function getExchangedToken(string $targetAudience, array $extraScopes = []): Token {
258-
$storeLoginTokenEnabled = $this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0') === '1';
258+
$storeLoginTokenEnabled = $this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0', lazy: true) === '1';
259259
if (!$storeLoginTokenEnabled) {
260260
throw new TokenExchangeFailedException(
261261
'Failed to exchange token, storing the login token is disabled. It can be enabled in config.php',

lib/Settings/AdminSettings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getForm() {
3737
);
3838
$this->initialStateService->provideInitialState(
3939
'storeLoginTokenState',
40-
$this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0') === '1'
40+
$this->appConfig->getValueString(Application::APP_ID, 'store_login_token', '0', lazy: true) === '1'
4141
);
4242
$this->initialStateService->provideInitialState(
4343
'providers',

0 commit comments

Comments
 (0)