Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed

- Fix: Default OIDC provider name not set while updating to Nextcloud Hub setup [#1126](https://github.com/nextcloud/integration_openproject/pull/1126)
- Fix: User shown connected in webUI after converting existing OAuth to SSO via setup endpoint [#1132](https://github.com/nextcloud/integration_openproject/pull/1132)

### Changed

Expand Down
21 changes: 8 additions & 13 deletions lib/Controller/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,9 @@ private function setIntegrationConfig(array $values): array {
}
// since we can now switch between both authorization method we need to know what we are resetting (either "oauth2" or "oidc" method)
// determines if we are switching from "oauth2" to "oidc" auth method
$runningOauth2Reset = (
key_exists('openproject_client_id', $values) &&
!$values['openproject_client_id'] &&
key_exists('openproject_client_secret', $values) &&
!$values['openproject_client_secret'] &&
$oldOpenProjectOauthUrl &&
$oldClientId &&
$oldClientSecret
$switchingOAuthToOIDC = (
$oldAuthMethod === Application::AUTH_METHOD_OAUTH && key_exists('authorization_method', $values) &&
$values['authorization_method'] === Application::AUTH_METHOD_OIDC
);

// determines if we are switching from "oidc" to "oauth2" auth method
Expand Down Expand Up @@ -309,13 +304,14 @@ private function setIntegrationConfig(array $values): array {
}

$this->config->deleteAppValue(Application::APP_ID, 'oPOAuthTokenRevokeStatus');
$resetOpenProjectClient = ((key_exists('openproject_client_id', $values) && $values['openproject_client_id'] !== $oldClientId) ||
(key_exists('openproject_client_secret', $values) && $values['openproject_client_secret'] !== $oldClientSecret));
if (
// when the OP client information has changed
(!$runningFullResetWithOIDCAuth && ((key_exists('openproject_client_id', $values) && $values['openproject_client_id'] !== $oldClientId) ||
(key_exists('openproject_client_secret', $values) && $values['openproject_client_secret'] !== $oldClientSecret))) ||
(!$runningFullResetWithOIDCAuth && $resetOpenProjectClient) ||
// when the OP client information is reset
$runningFullResetWithOAuth2Auth ||
$runningOauth2Reset
$switchingOAuthToOIDC
) {
$this->userManager->callForAllUsers(function (IUser $user) use (
$oldOpenProjectOauthUrl, $oldClientId, $oldClientSecret
Expand Down Expand Up @@ -378,8 +374,7 @@ private function setIntegrationConfig(array $values): array {
}

// when switching from "oauth2" to "oidc" authorization method
if (key_exists('authorization_method', $values) &&
$values['authorization_method'] === Application::AUTH_METHOD_OIDC && $runningOauth2Reset) {
if ($switchingOAuthToOIDC) {
$this->resetOauth2Configs();
}

Expand Down
77 changes: 70 additions & 7 deletions tests/lib/Controller/ConfigControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ public function updateIntegrationSuccessProvider(): array {
'status',
],
],
"oauth to oidc" => [
"oauth to oidc: no existing oauth client" => [
"authMethod" => Application::AUTH_METHOD_OAUTH,
"oauthClientId" => 0,
'settings' => [
Expand All @@ -2017,6 +2017,16 @@ public function updateIntegrationSuccessProvider(): array {
'status',
],
],
"oauth to oidc: existing oauth client" => [
"authMethod" => Application::AUTH_METHOD_OAUTH,
"oauthClientId" => 1,
'settings' => [
'authorization_method' => Application::AUTH_METHOD_OIDC,
],
'responseProps' => [
'status',
],
],
"oidc to oauth" => [
"authMethod" => Application::AUTH_METHOD_OIDC,
"oauthClientId" => 0,
Expand All @@ -2043,22 +2053,59 @@ public function updateIntegrationSuccessProvider(): array {
* @dataProvider updateIntegrationSuccessProvider
*/
public function testUpdateIntegrationSuccess(string $authMethod, int $oauthClientId, array $settings, array $responseProps): void {
$user = $this->createMock(IUser::class);
$user->method('getUID')->willReturn('testUser');

$userManagerMock = $this->createMock(IUserManager::class);
$userManagerMock->method('userExists')->willReturn(true);
$userManagerMock->method('callForAllUsers')
->willReturnCallback(function (callable $callback) use ($user) {
$callback($user);
});

$oauthMock = $this->createMock(OauthService::class);

$oldAuthMethod = $authMethod;
// change in authorization method
if (isset($settings['authorization_method'])) {
$authMethod = $settings['authorization_method'];
}

$configState = [
'authorization_method' => $oldAuthMethod,
'nc_oauth_client_id' => $oauthClientId,
];

$setAppValueCalls = [];
$deletedKeys = [];

$configMock = $this->createMock(IConfig::class);
$configMock
->method('getAppValue')
->willReturnMap([
[Application::APP_ID, 'authorization_method', '', $authMethod],
[Application::APP_ID, 'nc_oauth_client_id', '', $oauthClientId],
]);

$configMock->method('getAppValue')
->willReturnCallback(function ($app, $key, $default = '') use (&$configState) {
return $configState[$key] ?? $default;
});

$configMock->method('setAppValue')
->willReturnCallback(function ($app, $key, $value) use (&$configState, &$setAppValueCalls) {
$setAppValueCalls[] = [$app, $key, $value];
$configState[$key] = $value;
return true;
});

$configMock->method('deleteAppValue')
->willReturnCallback(function ($app, $key) use (&$configState, &$deletedKeys) {
$deletedKeys[] = $key;
unset($configState[$key]);
return true;
});

$deletedUserValues = [];
$configMock->method('deleteUserValue')
->willReturnCallback(function ($uid, $app, $key) use (&$deletedUserValues) {
$deletedUserValues[] = [$uid, $app, $key];
return true;
});

if ($authMethod === Application::AUTH_METHOD_OAUTH) {
if ($oauthClientId) {
Expand Down Expand Up @@ -2122,6 +2169,22 @@ public function testUpdateIntegrationSuccess(string $authMethod, int $oauthClien
$this->assertEquals(Http::STATUS_OK, $response->getStatus());
$this->assertArrayHasKey('status', $data);

if ($oldAuthMethod === Application::AUTH_METHOD_OAUTH
&& isset($settings['authorization_method'])
&& $settings['authorization_method'] === Application::AUTH_METHOD_OIDC) {
$this->assertContains([Application::APP_ID, 'openproject_client_id', ''], $setAppValueCalls);
$this->assertContains([Application::APP_ID, 'openproject_client_secret', ''], $setAppValueCalls);
$this->assertContains('nc_oauth_client_id', $deletedKeys);

$expectedDeletedKeys = ['token', 'login', 'user_id', 'user_name', 'refresh_token', 'token_expires_at'];
foreach ($expectedDeletedKeys as $key) {
$this->assertContains(
['testUser', Application::APP_ID, $key],
$deletedUserValues
);
}
}

foreach ($responseProps as $prop) {
$this->assertArrayHasKey($prop, $data);
}
Expand Down
Loading