Skip to content

Commit e8d0fff

Browse files
committed
Bugfix: Connecting with an authorization provider is rejected if the transmitted credentials contain an existing webtrees user name or email
1 parent 3050b42 commit e8d0fff

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

src/LoginWithAuthorizationProviderAction.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,23 @@ public function handle(ServerRequestInterface $request): ResponseInterface
265265
$user_to_connect = Session::get(OAuth2Client::activeModuleName() . OAuth2Client::SESSION_USER_TO_CONNECT, 0);
266266

267267
//Check if username/email already exists
268-
$existing_credentials = (($this->user_service->findByEmail($email) !== null) OR ($this->user_service->findByUserName($user_name) !== null));
268+
$existing_user = $this->user_service->findByEmail($email) ?? $this->user_service->findByUserName($user_name);
269+
270+
//Check if the authorizatiohn provider is already connected with an user
271+
$provider_id_is_connected = $this->findUserByAuthorizationProviderId($provider_name, $authorization_provider_id) !== null;
272+
273+
//Check if user has not signed in before (i.e. existing user, no provider name, no login timestamp)
274+
$existing_user_not_signed_in_yet = false;
275+
if ($existing_user !== null && ($existing_user->getPreference(OAuth2Client::USER_PREF_PROVIDER_NAME, '') === '') && ($existing_user->getPreference(UserInterface::PREF_TIMESTAMP_ACTIVE) === '0')) {
276+
$existing_user_not_signed_in_yet = true;
277+
}
269278

270279
//If we shall connect an existing user to a provider
271280
if($provider_to_connect === $provider_name && $user_to_connect !== 0) {
272281

273-
if ($existing_credentials OR $this->findUserByAuthorizationProviderId($provider_name, $authorization_provider_id) !== null) {
282+
//We do not connect an existing user who has not signed in yet, because it might have been registered based on an authorization provider (and not signed in yet)
283+
//We do not connect users with an authorization provider user ID, which is already connected to another user
284+
if ($existing_user_not_signed_in_yet OR $provider_id_is_connected) {
274285
$message = I18N::translate('The identity received by the authorization provider cannot be connected to the requested user, because it is already used to sign in by another webtrees user.');
275286
FlashMessages::addMessage($message, 'danger');
276287
CustomModuleLog::addDebugLog($log_module, $message);
@@ -291,7 +302,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
291302
}
292303

293304
//If user does not exist already, register based on the authorization provider user data
294-
if (!$existing_credentials && $this->findUserByAuthorizationProviderId($provider_name, $authorization_provider_id) === null) {
305+
if ($existing_user === null && !$provider_id_is_connected) {
295306

296307
//If user did not request to register (i.e. sign in and no account was found)
297308
if ($connect_action !== OAuth2Client::CONNECT_ACTION_REGISTER) {

0 commit comments

Comments
 (0)