Skip to content
Merged
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
4 changes: 3 additions & 1 deletion lib/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,9 @@ public function code(string $state = '', string $code = '', string $scope = '',
return $this->build403TemplateResponse($message, Http::STATUS_BAD_REQUEST, ['reason' => 'non-soft auto provision, user conflict'], false);
}
// use potential user from other backend, create it in our backend if it does not exist
$user = $this->provisioningService->provisionUser($userId, $providerId, $idTokenPayload, $userFromOtherBackend);
$provisioningResult = $this->provisioningService->provisionUser($userId, $providerId, $idTokenPayload, $userFromOtherBackend);
$user = $provisioningResult['user'];
$this->session->set('user_oidc.oidcUserData', $provisioningResult['userData']);
} else {
// when auto provision is disabled, we assume the user has been created by another user backend (or manually)
$user = $userFromOtherBackend;
Expand Down
26 changes: 19 additions & 7 deletions lib/Service/ProvisioningService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use OCA\UserOIDC\Db\UserMapper;
use OCA\UserOIDC\Event\AttributeMappedEvent;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\PropertyDoesNotExistException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
use OCP\DB\Exception;
Expand All @@ -26,6 +27,7 @@
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use OCP\PreConditionNotMetException;
use OCP\User\Events\UserChangedEvent;
use Psr\Log\LoggerInterface;
use Throwable;
Expand Down Expand Up @@ -104,15 +106,18 @@ public function getClaimValue(object|array $tokenPayload, string $claimPath, int

return null;
}

/**
* @param string $tokenUserId
* @param int $providerId
* @param object $idTokenPayload
* @param IUser|null $existingLocalUser
* @return IUser|null
* @return array{user: ?IUser, userData: array}
* @throws Exception
* @throws PropertyDoesNotExistException
* @throws PreConditionNotMetException
*/
public function provisionUser(string $tokenUserId, int $providerId, object $idTokenPayload, ?IUser $existingLocalUser = null): ?IUser {
public function provisionUser(string $tokenUserId, int $providerId, object $idTokenPayload, ?IUser $existingLocalUser = null): array {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a fan of using arrays that way, but can we annotate the type of the array so psalm can analyze it at least?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, done. I can't predict what's gonna be in userData so I keep a simple array type. All good?

Not a fan of using arrays that way

I agree it's not great. The alternative would be to pass the ISession object from the LoginController to provisioningService->provisionUser() to be able to change the session in there. Not elegant either.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can also create a ProvisioningResult class that contains the user and the raw user data. It's a bit cleaner. Wdyt?

// user data potentially later used by globalsiteselector if user_oidc is used with global scale
$oidcGssUserData = get_object_vars($idTokenPayload);

Expand Down Expand Up @@ -191,15 +196,21 @@ public function provisionUser(string $tokenUserId, int $providerId, object $idTo
$isUserCreationDisabled = isset($oidcSystemConfig['disable_account_creation'])
&& in_array($oidcSystemConfig['disable_account_creation'], [true, 'true', 1, '1'], true);
if ($isUserCreationDisabled) {
return null;
return [
'user' => null,
'userData' => $oidcGssUserData,
];
}

$backendUser = $this->userMapper->getOrCreate($providerId, $event->getValue() ?? '');
$this->logger->debug('User obtained from the OIDC user backend: ' . $backendUser->getUserId());

$user = $this->userManager->get($backendUser->getUserId());
if ($user === null) {
return null;
return [
'user' => null,
'userData' => $oidcGssUserData,
];
}
}

Expand Down Expand Up @@ -413,8 +424,6 @@ public function provisionUser(string $tokenUserId, int $providerId, object $idTo
$account->setProperty('gender', $event->getValue(), $fallbackScope, '1', '');
}

$this->session->set('user_oidc.oidcUserData', $oidcGssUserData);

while (true) {
try {
$this->accountManager->updateAccount($account);
Expand All @@ -432,7 +441,10 @@ public function provisionUser(string $tokenUserId, int $providerId, object $idTo
throw $e;
}
}
return $user;
return [
'user' => $user,
'userData' => $oidcGssUserData,
];
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/User/Provisioning/SelfEncodedTokenProvisioning.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function provisionUser(Provider $provider, string $tokenUserId, string $b
return null;
}

return $this->provisioningService->provisionUser($tokenUserId, $provider->getId(), $payload, $userFromOtherBackend);
$provisioningResult = $this->provisioningService->provisionUser($tokenUserId, $provider->getId(), $payload, $userFromOtherBackend);
return $provisioningResult['user'];
}
}
Loading