Skip to content

Commit

Permalink
Suppress uncaught Hybridauth exceptions in e_user_provider
Browse files Browse the repository at this point in the history
And add a check for those exceptions in
`social_ui::generateSocialLoginSection()`

Fixes: #4192
  • Loading branch information
Deltik committed Dec 28, 2021
1 parent b40288d commit 3f59b3b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
29 changes: 22 additions & 7 deletions e107_handlers/user_handler.php
Expand Up @@ -1171,16 +1171,30 @@ public function __construct($provider = null, $config = array(), $suppress_excep

}

$this->respawnHybridauth();
$this->setProvider($provider);
try
{
$this->respawnHybridauth();
$this->setProvider($provider);

$providerId = $this->getProvider();
if ($providerId && $this->hybridauth->isConnectedWith($providerId))
$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)
{
$this->adapter = $this->hybridauth->getAdapter($providerId);
if (!$suppress_exceptions) throw $e;
}
}

/**
* @throws \Hybridauth\Exception\InvalidArgumentException
*/
private function respawnHybridauth()
{
$this->hybridauth = new Hybridauth\Hybridauth($this->_config);
Expand Down Expand Up @@ -1237,9 +1251,10 @@ public function userId()
/**
* 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()
{
Expand Down
13 changes: 13 additions & 0 deletions e107_plugins/social/admin_config.php
Expand Up @@ -606,6 +606,19 @@ private function generateSocialLoginSection($provider_names, $readonly=false)

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);
}

Expand Down
28 changes: 28 additions & 0 deletions e107_tests/tests/unit/e_user_providerTest.php
Expand Up @@ -115,4 +115,32 @@ public function testGetSupplementalFieldsOf()
$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
);
}
}

0 comments on commit 3f59b3b

Please sign in to comment.