Skip to content

Commit 3b191a2

Browse files
committed
Check received provider and ID against existing user
1 parent 5606d23 commit 3b191a2

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/LoginWithAuthorizationProviderAction.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
268268
//Check if username/email already exists
269269
$existing_user = $this->user_service->findByEmail($email) ?? $this->user_service->findByUserName($user_name);
270270

271-
//Check if the authorizatiohn provider is already connected with an user
271+
//Check if the authorizatiohn provider ID is already connected with an user
272272
$provider_id_is_connected = $this->findUserByAuthorizationProviderId($provider_name, $authorization_provider_id) !== null;
273273

274274
//Check if user has not signed in before (i.e. existing user, no provider name, no login timestamp)
@@ -302,10 +302,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
302302
self::deleteSessionValuesForProviderConnection();
303303
}
304304

305-
//If user does not exist already, register based on the authorization provider user data
305+
//If user does not exist already and user is not connected already, register based on the authorization provider user data
306306
if ($existing_user === null && !$provider_id_is_connected) {
307307

308-
//If user did not request to register (i.e. sign in and no account was found)
308+
//If user did not request to register (i.e. signed in and no account was found)
309309
if ($connect_action !== OAuth2Client::CONNECT_ACTION_REGISTER) {
310310
FlashMessages::addMessage(I18N::translate('Currently, no webtrees user account is related to the user data received from the authorization provider.'));
311311
}
@@ -436,6 +436,14 @@ private function doLogin(string $email, string $provider_name, string $authoriza
436436
I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider_name));
437437
}
438438
}
439+
//If user has authorization provider, but provider/ID does not match
440+
elseif ( ($user->getPreference(OAuth2Client::USER_PREF_PROVIDER_NAME, '') !== $provider_name)
441+
OR ($user->getPreference(OAuth2Client::USER_PREF_ID_AT_PROVIDER, '') !== $authorization_provider_id)) {
442+
443+
Log::addAuthenticationLog($oauth_log_prefix . ': ' . 'Login denied. The email address or username already exists: ' . $provider_name . ' ' . $authorization_provider_id);
444+
throw new Exception(I18N::translate('Login denied. The email address or username already exists.') . ' ' .
445+
I18N::translate('To connect an existing user with %s, sign in and select: My pages / My account / Connect with', $provider_name));
446+
}
439447

440448
Auth::login($user);
441449
Log::addAuthenticationLog('Login: ' . Auth::user()->userName() . '/' . Auth::user()->realName());

0 commit comments

Comments
 (0)