1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-29 10:38:08 +01:00

Suppress uncaught Hybridauth exceptions in e_user_provider

And add a check for those exceptions in
`social_ui::generateSocialLoginSection()`

Fixes: #4192
This commit is contained in:
Nick Liu 2021-12-28 11:51:20 +01:00
parent b40288d665
commit 3f59b3bc14
No known key found for this signature in database
GPG Key ID: 1167C5F9C9897637
3 changed files with 64 additions and 8 deletions

View File

@ -1171,16 +1171,30 @@ class e_user_provider
}
$this->respawnHybridauth();
$this->setProvider($provider);
$providerId = $this->getProvider();
if ($providerId && $this->hybridauth->isConnectedWith($providerId))
try
{
$this->adapter = $this->hybridauth->getAdapter($providerId);
$this->respawnHybridauth();
$this->setProvider($provider);
$providerId = $this->getProvider();
if ($providerId && $this->hybridauth->isConnectedWith($providerId))
{
$this->adapter = $this->hybridauth->getAdapter($providerId);
}
}
catch (\Hybridauth\Exception\InvalidArgumentException $e)
{
if (!$suppress_exceptions) throw $e;
}
catch (\Hybridauth\Exception\UnexpectedValueException $e)
{
if (!$suppress_exceptions) throw $e;
}
}
/**
* @throws \Hybridauth\Exception\InvalidArgumentException
*/
private function respawnHybridauth()
{
$this->hybridauth = new Hybridauth\Hybridauth($this->_config);
@ -1237,9 +1251,10 @@ class e_user_provider
/**
* Get the social login providers for which we have adapters
*
* This function is slow! Please cache the output instead of calling it multiple times.
* Despite this being a static method, it memoizes (caches) the slow reflection code in the {@link e107} registry
* after the first run, so subsequent calls to this method are fast.
*
* @return array String list of supported providers. Empty if Hybridauth is broken.
* @return string[] String list of supported providers. Empty if Hybridauth is broken.
*/
public static function getSupportedProviders()
{

View File

@ -606,6 +606,19 @@ class social_ui extends e_admin_ui
foreach ($provider_names as $provider_name)
{
// Check if the current configuration for the provider is valid
try
{
new e_user_provider($provider_name, [], false);
}
catch (\Hybridauth\Exception\InvalidArgumentException $e)
{
e107::getMessage()->addError("[{$e->getCode()}] {$e->getMessage()}");
}
catch (\Hybridauth\Exception\UnexpectedValueException $ignored)
{
}
$text .= $this->generateSocialLoginRow($provider_name, $readonly);
}

View File

@ -115,4 +115,32 @@ class e_user_providerTest extends \Codeception\Test\Unit
$result = e_user_provider::getSupplementalFieldsOf("Vkontakte");
$this->assertTrue(array_key_exists('photo_size', $result));
}
public function testNewSuppressExceptions()
{
$this->assertInstanceOf(
e_user_provider::class,
new e_user_provider("Facebook", ["providers" => ["Facebook", ["enabled" => true]]])
);
}
public function testNewNoSuppressConfigurationException()
{
$this->expectException(\Hybridauth\Exception\InvalidArgumentException::class);
new e_user_provider(
"Facebook",
["providers" => ["Facebook" => ["enabled" => true]]],
false
);
}
public function testNewNoSuppressDisabledException()
{
$this->expectException(\Hybridauth\Exception\UnexpectedValueException::class);
new e_user_provider(
"Facebook",
["providers" => ["Facebook" => ["enabled" => false]]],
false
);
}
}