1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-13 01:54:12 +02:00

Core modifications to support Hybridauth 3

- MOD: Replaced e107::getPref('social_login') with
       SocialLoginConfigManager::getValidConfiguredProviderConfigs()
- FIX: signup_shortcodes updated with new social login providers
- MOD: e107::filter_request() code de-duplication: HTTP 400 exits
- MOD: Deprecated e107::getHybridAuth() to discourage direct access to
       third-party dependency Hybridauth
- FIX: Updated e_user_provider for Hybridauth 3
- FIX: e_user::tryProviderSession() and Hybridauth 3
- NEW: Dynamic auth provider support in social_adminarea
- NEW: Database migration for social plugin's social_login pref
This commit is contained in:
Nick Liu
2020-02-17 10:36:03 +01:00
parent 46c75ae4d0
commit 91bfc1df23
12 changed files with 1228 additions and 509 deletions

View File

@@ -0,0 +1,60 @@
<?php
/**
* e107 website system
*
* Copyright (C) 2008-2020 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
*/
class e_user_providerTest extends \Codeception\Test\Unit
{
/** @var e_user_provider */
protected $e_user_provider;
protected function _before()
{
try
{
include_once(e_HANDLER . 'user_handler.php');
$this->e_user_provider = $this->make('e_user_provider');
}
catch (Exception $e)
{
$this->fail("Couldn't load e_user_provider object: {$e}");
}
}
public function testGetSupportedProviders()
{
$result = e_user_provider::getSupportedProviders();
$this->assertIsArray($result);
$this->assertContains("Facebook", $result);
$this->assertContains("Twitter", $result);
$this->assertCount(42, $result,
"The number of Hybridauth providers has changed! If this is intentional, note the change " .
"in Hybridauth providers in the release changelog and update the count in this test."
);
}
public function testGetProviderType()
{
$result = e_user_provider::getTypeOf("NotARealProvider");
$this->assertFalse($result);
$result = e_user_provider::getTypeOf("Steam");
$this->assertEquals("OpenID", $result);
$result = e_user_provider::getTypeOf("StackExchangeOpenID");
$this->assertEquals("OpenID", $result);
$result = e_user_provider::getTypeOf("Twitter");
$this->assertEquals("OAuth1", $result);
$result = e_user_provider::getTypeOf("WordPress");
$this->assertEquals("OAuth2", $result);
}
}

View File

@@ -0,0 +1,215 @@
<?php
use Codeception\Stub;
/**
* e107 website system
*
* Copyright (C) 2008-2020 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
*/
class SocialLoginConfigManagerTest extends \Codeception\Test\Unit
{
/**
* @var e_core_pref
*/
private $pref;
/**
* @var SocialLoginConfigManager
*/
private $manager;
/**
* @throws Exception
*/
public function _before()
{
include_once(e_PLUGIN . "social/SocialLoginConfigManager.php");
$this->pref = $this->make('e_pref');
$this->pref->set('social_login', [
'Twitter-OAuth1' => [
'enabled' => true,
'keys' => [
'id' => 'ID',
'secret' => 'SECRET',
],
],
'StackExchange-OpenID' => [
'enabled' => false,
],
'GitHub-OAuth2' => [
'enabled' => true,
'keys' => [
'id' => 'ID',
'secret' => 'SECRET',
],
'scope' => 'identity',
],
'OpenID' => [
'enabled' => true,
],
]);
$this->manager = new SocialLoginConfigManager($this->pref);
}
public function testIsProviderEnabled()
{
$this->assertTrue($this->manager->isProviderEnabled('Twitter'));
$this->assertFalse($this->manager->isProviderEnabled('StackExchangeOpenID'));
$this->assertTrue($this->manager->isProviderEnabled('GitHub'));
$this->assertTrue($this->manager->isProviderEnabled('OpenID'));
}
public function testForgetProvider()
{
$this->manager->forgetProvider('OpenID');
$result = $this->manager->getConfiguredProviders();
$this->assertCount(3, $result);
$this->manager->forgetProvider('StackExchangeOpenID');
$result = $this->manager->getConfiguredProviders();
$this->assertCount(2, $result);
$this->manager->forgetProvider('FakeProvider');
$result = $this->manager->getConfiguredProviders();
$this->assertCount(2, $result);
}
public function testSetProviderConfig()
{
$this->manager->setProviderConfig('MyEnabledProvider', ['enabled' => true]);
$result = $this->manager->getConfiguredProviders();
$this->assertContains('MyEnabledProvider', $result);
$this->assertTrue($this->manager->isProviderEnabled('MyEnabledProvider'));
$this->manager->setProviderConfig('MyDisabledProvider', ['garbage' => 'nonsense']);
$result = $this->manager->getConfiguredProviders();
$this->assertContains('MyDisabledProvider', $result);
$this->assertFalse($this->manager->isProviderEnabled('MyDisabledProvider'));
}
public function testSetProviderConfigForgetsProviderIfEmpty()
{
$this->manager->setProviderConfig('EmptyProvider', [
'enabled' => null,
'keys' => [
'id' => '',
'secret' => 0,
],
'scope' => false,
]);
$result = $this->manager->getConfiguredProviders();
$this->assertNotContains('EmptyProvider', $result);
}
public function testSetProviderConfigDiscardsEmptyOptions()
{
$this->manager->setProviderConfig('MiscProvider', [
'enabled' => true,
'openid_identifier' => '',
'keys' => [
'id' => null,
'secret' => 0,
],
'scope' => false,
]);
$result = $this->manager->getProviderConfig('MiscProvider');
$this->assertEquals(['enabled' => true], $result);
}
public function testGetProviderConfig()
{
$result = $this->manager->getProviderConfig('Twitter');
$this->assertTrue($result['enabled']);
$this->assertArrayHasKey('keys', $result);
$this->assertArrayNotHasKey('scope', $result);
$result = $this->manager->getProviderConfig('Twitter', 'keys/id');
$this->assertEquals('ID', $result);
$result = $this->manager->getProviderConfig('Twitter', '/keys/secret');
$this->assertEquals('SECRET', $result);
$result = $this->manager->getProviderConfig('Twitter', '/fake');
$this->assertNull($result);
$result = $this->manager->getProviderConfig('StackExchangeOpenID');
$this->assertFalse($result['enabled']);
$this->assertArrayNotHasKey('keys', $result);
$this->assertArrayNotHasKey('scope', $result);
$result = $this->manager->getProviderConfig('GitHub');
$this->assertEquals('identity', $result['scope']);
}
public function testGetConfiguredProviders()
{
$result = $this->manager->getConfiguredProviders();
$this->assertCount(4, $result);
$this->assertContains('Twitter', $result);
$this->assertContains('StackExchangeOpenID', $result);
$this->assertContains('GitHub', $result);
$this->assertContains('OpenID', $result);
}
public function testNormalizeProviderNameFixesCapitalization()
{
$output = $this->manager->normalizeProviderName("Github");
$this->assertEquals("GitHub-OAuth2", $output);
}
public function testNormalizeProviderNamePassesThroughUnknownName()
{
$output = $this->manager->normalizeProviderName("iPhone");
$this->assertEquals("iPhone", $output);
}
public function testNormalizeProviderNameRemovesTypeFromName()
{
$output = $this->manager->normalizeProviderName("StackExchangeOpenID");
$this->assertEquals("StackExchange-OpenID", $output);
$output = $this->manager->normalizeProviderName("aolOPENid");
$this->assertEquals("AOL-OpenID", $output);
}
public function testNormalizeProviderNameFindsCorrectType()
{
$output = $this->manager->normalizeProviderName("StackExchange");
$this->assertEquals("StackExchange-OAuth2", $output);
$output = $this->manager->normalizeProviderName("Telegram");
$this->assertEquals("Telegram", $output);
}
public function testNormalizeProviderNameGeneric()
{
$output = $this->manager->normalizeProviderName("openid");
$this->assertEquals("OpenID", $output);
}
public function testNormalizeProviderNameFakeGeneric()
{
$output = $this->manager->normalizeProviderName("OAuth2");
$this->assertEquals("OAuth2", $output);
}
public function testDenormalizeProviderName()
{
$output = $this->manager->denormalizeProviderName("OpenID");
$this->assertEquals("OpenID", $output);
$output = $this->manager->denormalizeProviderName("StackExchange-OAuth1");
$this->assertEquals("StackExchangeOAuth1", $output);
$output = $this->manager->denormalizeProviderName("StackExchange-OAuth2");
$this->assertEquals("StackExchange", $output);
$output = $this->manager->denormalizeProviderName("StackExchange-OpenID");
$this->assertEquals("StackExchangeOpenID", $output);
}
}

View File

@@ -0,0 +1,127 @@
<?php
/**
* e107 website system
*
* Copyright (C) 2008-2020 e107 Inc (e107.org)
* Released under the terms and conditions of the
* GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
*
*/
class social_setupTest extends \Codeception\Test\Unit
{
public function testUpgrade()
{
include_once(e_PLUGIN . "social/SocialLoginConfigManager.php");
include_once(e_PLUGIN . "social/social_setup.php");
e107::getConfig()->set(SocialLoginConfigManager::SOCIAL_LOGIN_PREF, SOCIAL_LOGIN_LEGACY_DATA);
$social_setup = new social_setup();
$this->assertTrue($social_setup->upgrade_required());
$this->assertIsArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/AOL"));
$this->assertIsNotArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/AOL-OpenID"));
$social_setup->upgrade_pre();
$this->assertFalse($social_setup->upgrade_required());
$this->assertIsNotArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/AOL"));
$this->assertIsArray(e107::getConfig()->getPref(SocialLoginConfigManager::SOCIAL_LOGIN_PREF . "/AOL-OpenID"));
}
}
const SOCIAL_LOGIN_LEGACY_DATA =
array(
'FakeProviderNeverExisted' =>
array(
'enabled' => '1',
),
'AOL' =>
array(
'enabled' => '1',
),
'Facebook' =>
array(
'keys' =>
array(
'id' => 'a',
'secret' => 'b',
),
'scope' => 'c',
'enabled' => '1',
),
'Foursquare' =>
array(
'keys' =>
array(
'id' => 'a',
'secret' => 'b',
),
'enabled' => '1',
),
'Github' =>
array(
'keys' =>
array(
'id' => 'a',
'secret' => 'b',
),
'scope' => 'c',
'enabled' => '1',
),
'Google' =>
array(
'keys' =>
array(
'id' => 'a',
'secret' => 'b',
),
'scope' => 'c',
'enabled' => '1',
),
'LinkedIn' =>
array(
'keys' =>
array(
'id' => 'a',
'secret' => 'b',
),
'enabled' => '1',
),
'Live' =>
array(
'keys' =>
array(
'id' => 'a',
'secret' => 'b',
),
'enabled' => '1',
),
'OpenID' =>
array(
'enabled' => '1',
),
'Steam' =>
array(
'keys' =>
array(
'key' => 'a',
),
'enabled' => '1',
),
'Twitter' =>
array(
'keys' =>
array(
'key' => 'a',
'secret' => 'b',
),
'enabled' => '1',
),
'Yahoo' =>
array(
'keys' =>
array(
'id' => 'a',
'secret' => 'b',
),
'enabled' => '1',
),
);