Browse Source
- added social config vars in Aauth Config - added Social Class - added Social Controller - enhanced Login & Register with Social Features - updated tests for Social Module - updated Account Languagev3-dev
14 changed files with 822 additions and 17 deletions
@ -0,0 +1,170 @@
|
||||
<?php |
||||
/** |
||||
* CodeIgniter-Aauth |
||||
* |
||||
* Aauth is a User Authorization Library for CodeIgniter 4.x, which aims to make |
||||
* easy some essential jobs such as login, permissions and access operations. |
||||
* Despite ease of use, it has also very advanced features like grouping, |
||||
* access management, public access etc.. |
||||
* |
||||
* @package CodeIgniter-Aauth |
||||
* @author Emre Akay |
||||
* @author Raphael "REJack" Jackstadt |
||||
* @copyright 2014-2019 Emre Akay |
||||
* @license https://opensource.org/licenses/MIT MIT License |
||||
* @link https://github.com/emreakay/CodeIgniter-Aauth |
||||
*/ |
||||
|
||||
namespace App\Controllers\Account; |
||||
|
||||
use CodeIgniter\Controller; |
||||
use Config\Aauth as AauthConfig; |
||||
use App\Libraries\Aauth; |
||||
use Config\Services; |
||||
|
||||
/** |
||||
* Aauth Accont/Social Controller |
||||
* |
||||
* @package CodeIgniter-Aauth |
||||
*/ |
||||
class Social extends Controller |
||||
{ |
||||
/** |
||||
* Constructor |
||||
*/ |
||||
public function __construct() |
||||
{ |
||||
$this->config = new AauthConfig(); |
||||
$this->aauth = new Aauth(); |
||||
$this->request = Services::request(); |
||||
helper('form'); |
||||
} |
||||
|
||||
/** |
||||
* Index |
||||
* |
||||
* @param string $provider Provider Name |
||||
* |
||||
* @return redirect |
||||
*/ |
||||
public function connect(string $provider = null) |
||||
{ |
||||
if ($provider) |
||||
{ |
||||
session()->setFlashdata('social_provider', $provider); |
||||
} |
||||
else |
||||
{ |
||||
$provider = session('social_provider'); |
||||
} |
||||
|
||||
if ($userId = $this->aauth->getUserId()) |
||||
{ |
||||
if ($this->aauth->authenticateProvider($provider, 'account/social/connect/')) |
||||
{ |
||||
if ($userId = $this->aauth->getUserId()) |
||||
{ |
||||
helper('text'); |
||||
$userProfile = $this->aauth->getSocialDetails($provider); |
||||
$password = random_string('alnum', (config('Aauth')->passwordMin + 2)); |
||||
$username = preg_replace('/[^A-Za-z0-9]/', '', $userProfile->displayName); |
||||
|
||||
$this->aauth->linkSocial($userId, $provider); |
||||
} |
||||
} |
||||
|
||||
return redirect()->to(site_url('/account')); |
||||
} |
||||
|
||||
return redirect()->to('/'); |
||||
} |
||||
/** |
||||
* Index |
||||
* |
||||
* @param string $provider Provider Name |
||||
* |
||||
* @return redirect |
||||
*/ |
||||
public function disconnect(string $provider) |
||||
{ |
||||
if ($userId = $this->aauth->getUserId()) |
||||
{ |
||||
$this->aauth->unlinkSocial($userId, $provider); |
||||
|
||||
return redirect()->to(site_url('/account')); |
||||
} |
||||
|
||||
return redirect()->to('/'); |
||||
} |
||||
|
||||
/** |
||||
* Index |
||||
* |
||||
* @param string $provider Provider Name |
||||
* |
||||
* @return redirect |
||||
*/ |
||||
public function login(string $provider = null) |
||||
{ |
||||
if ($provider) |
||||
{ |
||||
session()->setFlashdata('social_provider', $provider); |
||||
} |
||||
else |
||||
{ |
||||
$provider = session('social_provider'); |
||||
} |
||||
|
||||
if ($this->aauth->authenticateProvider($provider, 'account/social/login/')) |
||||
{ |
||||
if ($this->aauth->loginSocial($provider)) |
||||
{ |
||||
return redirect()->to(site_url('/account')); |
||||
} |
||||
} |
||||
|
||||
return redirect()->to(site_url('/account/login'))->with('errors', lang('Aauth.notFoundUser')); |
||||
} |
||||
|
||||
/** |
||||
* Index |
||||
* |
||||
* @param string $provider Provider Name |
||||
* |
||||
* @return redirect |
||||
*/ |
||||
public function register(string $provider = null) |
||||
{ |
||||
if ($provider) |
||||
{ |
||||
session()->setFlashdata('social_provider', $provider); |
||||
} |
||||
else |
||||
{ |
||||
$provider = session('social_provider'); |
||||
} |
||||
|
||||
if ($this->aauth->authenticateProvider($provider, 'account/social/register/')) |
||||
{ |
||||
if (! $this->aauth->loginSocial($provider)) |
||||
{ |
||||
helper('text'); |
||||
$userProfile = $this->aauth->getSocialDetails($provider); |
||||
$password = random_string('alnum', (config('Aauth')->passwordMin + 2)); |
||||
$username = preg_replace('/[^A-Za-z0-9]/', '', $userProfile->displayName); |
||||
|
||||
if ($userId = $this->aauth->createUser($userProfile->email, $password, $username)) |
||||
{ |
||||
$this->aauth->linkSocial($userId, $provider); |
||||
$this->aauth->loginSocial($provider); |
||||
|
||||
return redirect()->to(site_url('/account')); |
||||
} |
||||
|
||||
return redirect()->to(site_url('/account/register'))->with('errors', $this->aauth->printErrors('<br />', true)); |
||||
} |
||||
|
||||
return redirect()->to(site_url('/account')); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,286 @@
|
||||
<?php |
||||
/** |
||||
* CodeIgniter-Aauth |
||||
* |
||||
* Aauth is a User Authorization Library for CodeIgniter 4.x, which aims to make |
||||
* easy some essential jobs such as login, permissions and access operations. |
||||
* Despite ease of use, it has also very advanced features like grouping, |
||||
* access management, public access etc.. |
||||
* |
||||
* @package CodeIgniter-Aauth |
||||
* @author Emre Akay |
||||
* @author Raphael "REJack" Jackstadt |
||||
* @copyright 2014-2019 Emre Akay |
||||
* @license https://opensource.org/licenses/MIT MIT License |
||||
* @link https://github.com/emreakay/CodeIgniter-Aauth |
||||
* @since 3.0.0 |
||||
*/ |
||||
|
||||
namespace App\Libraries\Aauth; |
||||
|
||||
/** |
||||
* Aauth Social |
||||
* |
||||
* Class for handling social logins |
||||
* |
||||
* @package CodeIgniter-Aauth |
||||
*/ |
||||
class Social extends \App\Libraries\Aauth |
||||
{ |
||||
|
||||
/** |
||||
* Variable to load HybridAuth config array |
||||
* |
||||
* @var array |
||||
*/ |
||||
protected $configHybridAuth = []; |
||||
|
||||
/** |
||||
* Variable to store HybridAuths storage name |
||||
* |
||||
* @var string |
||||
*/ |
||||
protected $storageHybridAuth = 'HYBRIDAUTH::STORAGE'; |
||||
|
||||
/** |
||||
* Constructor |
||||
* |
||||
* Prepares config & session variable. |
||||
* |
||||
* @param \Config\Aauth $config Config Object |
||||
* @param \CodeIgniter\Session\Session $session Session Class |
||||
* |
||||
* @return void |
||||
*/ |
||||
public function __construct(\Config\Aauth $config = null, \CodeIgniter\Session\Session $session = null) |
||||
{ |
||||
parent::__construct($config, $session); |
||||
|
||||
$this->configHybridAuth['providers'] = $config->socialProviders; |
||||
$this->configHybridAuth['callback'] = site_url(); |
||||
} |
||||
|
||||
/** |
||||
* Login Social |
||||
* |
||||
* @param string $provider Provider Name |
||||
* |
||||
* @return boolean|object |
||||
*/ |
||||
public function loginSocial(string $provider = null) |
||||
{ |
||||
$userProfile = $this->getSocialDetails($provider); |
||||
|
||||
if ($userId = $this->getSocialUserId($provider, $userProfile->identifier)) |
||||
{ |
||||
$session = service('session'); |
||||
$storage = $session->get($this->storageHybridAuth); |
||||
|
||||
$this->updateSocialProviderIdentifier($userId, 'storage', json_encode($storage)); |
||||
|
||||
if ($this->config->socialRemember) |
||||
{ |
||||
$expires = $this->config->socialRemember; |
||||
|
||||
if ($expires === true) |
||||
{ |
||||
$expires = $storage[strtolower($provider) . '.expires_at']; |
||||
} |
||||
|
||||
$this->generateRemember($userId, $expires); |
||||
} |
||||
return $this->loginFast($userId); |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* Link Social |
||||
* |
||||
* @param integer $userId User Id |
||||
* @param string $provider Provider Name |
||||
* |
||||
* @return boolean |
||||
*/ |
||||
public function linkSocial(int $userId, string $provider) |
||||
{ |
||||
$userProfile = $this->getSocialDetails($provider); |
||||
$this->updateSocialStorage($userId); |
||||
|
||||
return $this->updateSocialProviderIdentifier($userId, $provider, $userProfile->identifier); |
||||
} |
||||
|
||||
/** |
||||
* Unlink Social |
||||
* |
||||
* @param integer $userId User Id |
||||
* @param string $provider Provider Name |
||||
* |
||||
* @return boolean |
||||
*/ |
||||
public function unlinkSocial(int $userId, string $provider) |
||||
{ |
||||
$session = service('session'); |
||||
$session->remove($this->storageHybridAuth); |
||||
|
||||
$userVariableModel = $this->getModel('UserVariable'); |
||||
$userVariableModel->delete($userId, 'social_storage', true); |
||||
|
||||
return $userVariableModel->delete($userId, 'social_' . strtolower($provider), true); |
||||
} |
||||
|
||||
/** |
||||
* Rebuild Social Storage |
||||
* |
||||
* @param integer $userId User Id |
||||
* |
||||
* @return void |
||||
*/ |
||||
public function rebuildSocialStorage(int $userId) |
||||
{ |
||||
$userVariableModel = $this->getModel('UserVariable'); |
||||
$providers = $this->getProviders(); |
||||
|
||||
if ($storedData = $userVariableModel->find($userId, 'social_storage', true)) |
||||
{ |
||||
$storedData = json_decode($storedData, true); |
||||
|
||||
foreach ($providers as $provider) |
||||
{ |
||||
if ($storedData[strtolower($provider) . '.expires_at'] > time()) |
||||
{ |
||||
$session = service('session'); |
||||
$session->set($this->storageHybridAuth, $storedData); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Get Social User Id |
||||
* |
||||
* @param string $provider Provider Name |
||||
* @param string $identifier Identifier |
||||
* |
||||
* @return integer|boolean |
||||
*/ |
||||
public function getSocialUserId(string $provider, string $identifier) |
||||
{ |
||||
$userVariableModel = $this->getModel('UserVariable'); |
||||
$whereArray = [ |
||||
'data_key' => 'social_' . strtolower($provider), |
||||
'data_value' => $identifier, |
||||
'system' => 1, |
||||
]; |
||||
|
||||
if ($user = $userVariableModel->select('user_id')->where($whereArray)->first()) |
||||
{ |
||||
return (int) $user['user_id']; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* Get Social Identifier |
||||
* |
||||
* @param string $provider Provider Name |
||||
* @param integer $userId User Id |
||||
* |
||||
* @return integer|boolean |
||||
*/ |
||||
public function getSocialIdentifier(string $provider, int $userId) |
||||
{ |
||||
$userVariableModel = $this->getModel('UserVariable'); |
||||
$whereArray = [ |
||||
'data_key' => 'social_' . strtolower($provider), |
||||
'user_id' => $userId, |
||||
'system' => 1, |
||||
]; |
||||
|
||||
if ($user = $userVariableModel->select('data_value')->where($whereArray)->first()) |
||||
{ |
||||
return $user['data_value']; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* Get Social Details |
||||
* |
||||
* @param string $provider Provider Name |
||||
* |
||||
* @return \Hybridauth\User\Profile |
||||
*/ |
||||
public function getSocialDetails(string $provider) |
||||
{ |
||||
$hybridauth = new \Hybridauth\Hybridauth($this->configHybridAuth); |
||||
|
||||
$adapter = $hybridauth->getAdapter($provider); |
||||
|
||||
return $adapter->getUserProfile(); |
||||
} |
||||
|
||||
/** |
||||
* Get Providers |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function getProviders() |
||||
{ |
||||
$hybridauth = new \Hybridauth\Hybridauth($this->configHybridAuth); |
||||
|
||||
return $hybridauth->getProviders(); |
||||
} |
||||
|
||||
/** |
||||
* Authenticate Provider |
||||
* |
||||
* @param string $provider Provider Name |
||||
* @param string $callbackUrl Callback Link |
||||
* |
||||
* @return \Hybridauth\Adapter\AdapterInterface |
||||
*/ |
||||
public function authenticateProvider(string $provider, string $callbackUrl) |
||||
{ |
||||
$this->configHybridAuth['callback'] = site_url($callbackUrl); |
||||
|
||||
$hybridauth = new \Hybridauth\Hybridauth($this->configHybridAuth); |
||||
$adapter = $hybridauth->authenticate($provider); |
||||
|
||||
return $adapter; |
||||
} |
||||
|
||||
/** |
||||
* Update Social Storage |
||||
* |
||||
* @param integer $userId User Id |
||||
* |
||||
* @return void |
||||
*/ |
||||
private function updateSocialStorage(int $userId) |
||||
{ |
||||
$session = service('session'); |
||||
$storage = $session->get($this->storageHybridAuth); |
||||
|
||||
$this->updateSocialProviderIdentifier($userId, 'storage', json_encode($storage)); |
||||
} |
||||
|
||||
/** |
||||
* Update Social Provider Identifier |
||||
* |
||||
* @param integer $userId User Id |
||||
* @param string $provider Provider Name |
||||
* @param string $identifier Identifier |
||||
* |
||||
* @return boolean |
||||
*/ |
||||
private function updateSocialProviderIdentifier(int $userId, string $provider, string $identifier) |
||||
{ |
||||
$userVariableModel = $this->getModel('UserVariable'); |
||||
|
||||
return $userVariableModel->save($userId, 'social_' . strtolower($provider), $identifier, true); |
||||
} |
||||
} |
@ -0,0 +1,207 @@
|
||||
<?php namespace Tests\Aauth\Libraries\Aauth; |
||||
|
||||
use Config\Aauth as AauthConfig; |
||||
use Config\Logger; |
||||
use Config\Services; |
||||
use Tests\Support\Log\TestLogger; |
||||
use Tests\Support\Session\MockSession; |
||||
use CodeIgniter\Session\Handlers\FileHandler; |
||||
use CodeIgniter\Test\CIDatabaseTestCase; |
||||
use App\Libraries\Aauth; |
||||
use App\Models\Aauth\UserModel; |
||||
use App\Models\Aauth\UserVariableModel; |
||||
use OTPHP\TOTP; |
||||
|
||||
/** |
||||
* @runTestsInSeparateProcesses |
||||
* @preserveGlobalState disabled |
||||
*/ |
||||
class SocialTest extends CIDatabaseTestCase |
||||
{ |
||||
protected $refresh = true; |
||||
|
||||
protected $basePath = FCPATH . '../app/Database/Migrations'; |
||||
|
||||
protected $namespace = 'App'; |
||||
|
||||
public function setUp() |
||||
{ |
||||
parent::setUp(); |
||||
|
||||
$this->config = new AauthConfig(); |
||||
|
||||
$this->config->socialEnabled = true; |
||||
|
||||
$this->library = new Aauth($this->config, null); |
||||
$_COOKIE = []; |
||||
$_SESSION = []; |
||||
} |
||||
|
||||
public function tearDown() |
||||
{ |
||||
} |
||||
|
||||
protected function getInstance($options = []) |
||||
{ |
||||
$defaults = [ |
||||
'sessionDriver' => 'CodeIgniter\Session\Handlers\FileHandler', |
||||
'sessionCookieName' => 'ci_session', |
||||
'sessionExpiration' => 7200, |
||||
'sessionSavePath' => 'null', |
||||
'sessionMatchIP' => false, |
||||
'sessionTimeToUpdate' => 300, |
||||
'sessionRegenerateDestroy' => false, |
||||
'cookieDomain' => '', |
||||
'cookiePrefix' => '', |
||||
'cookiePath' => '/', |
||||
'cookieSecure' => false, |
||||
]; |
||||
|
||||
$config = (object)$defaults; |
||||
|
||||
$session = new MockSession(new FileHandler($config, Services::request()->getIPAddress()), $config); |
||||
$session->setLogger(new TestLogger(new Logger())); |
||||
$session->start(); |
||||
|
||||
return $session; |
||||
} |
||||
|
||||
//-------------------------------------------------------------------- |
||||
|
||||
public function testUnlinkSocial() |
||||
{ |
||||
$testArray = ['facebook.expires_at' => strtotime('+1 hour')]; |
||||
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||
'user_id' => 1, |
||||
'data_key' => 'social_storage', |
||||
'data_value' => json_encode($testArray), |
||||
'system' => true, |
||||
]); |
||||
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||
'user_id' => 1, |
||||
'data_key' => 'social_facebook', |
||||
'data_value' => 'testing00testing00testing', |
||||
'system' => true, |
||||
]); |
||||
|
||||
$config = new AauthConfig(); |
||||
$session = $this->getInstance(); |
||||
|
||||
$config->socialEnabled = true; |
||||
$config->socialProviders = [ |
||||
'Facebook' => [ |
||||
'enabled' => true, |
||||
'keys' => [ |
||||
'id' => 'testing', |
||||
'secret' => 'testing00testing00testing', |
||||
], |
||||
], |
||||
]; |
||||
|
||||
$this->library = new Aauth($config, $session); |
||||
$this->library->unlinkSocial(1, 'Facebook'); |
||||
|
||||
$this->dontSeeInDatabase($this->config->dbTableUserVariables, [ |
||||
'user_id' => 1, |
||||
'data_key' => 'social_storage', |
||||
'data_value' => json_encode($testArray), |
||||
'system' => true, |
||||
]); |
||||
$this->dontSeeInDatabase($this->config->dbTableUserVariables, [ |
||||
'user_id' => 1, |
||||
'data_key' => 'social_facebook', |
||||
'data_value' => 'testing00testing00testing', |
||||
'system' => true, |
||||
]); |
||||
|
||||
$this->assertFalse(isset($_SESSION['HYBRIDAUTH::STORAGE'])); |
||||
} |
||||
|
||||
public function testGetSocialUserId() |
||||
{ |
||||
$config = new AauthConfig(); |
||||
$session = $this->getInstance(); |
||||
|
||||
$config->socialEnabled = true; |
||||
$this->library = new Aauth($config, $session); |
||||
|
||||
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||
'user_id' => 1, |
||||
'data_key' => 'social_testing', |
||||
'data_value' => 'testingidentifier', |
||||
'system' => true, |
||||
]); |
||||
$this->assertEquals(1, $this->library->getSocialUserId('testing', 'testingidentifier')); |
||||
$this->assertFalse($this->library->getSocialUserId('testing', 'none')); |
||||
} |
||||
|
||||
public function testGetSocialIdentifier() |
||||
{ |
||||
$config = new AauthConfig(); |
||||
$session = $this->getInstance(); |
||||
|
||||
$config->socialEnabled = true; |
||||
$this->library = new Aauth($config, $session); |
||||
|
||||
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||
'user_id' => 1, |
||||
'data_key' => 'social_testing', |
||||
'data_value' => 'testingidentifier', |
||||
'system' => true, |
||||
]); |
||||
$this->assertEquals('testingidentifier', $this->library->getSocialIdentifier('testing', 1)); |
||||
$this->assertFalse($this->library->getSocialIdentifier('testing99', 1)); |
||||
} |
||||
|
||||
public function testGetProviders() |
||||
{ |
||||
$config = new AauthConfig(); |
||||
$session = $this->getInstance(); |
||||
|
||||
$config->socialProviders = [ |
||||
'Facebook' => [ |
||||
'enabled' => true, |
||||
'keys' => [ |
||||
'id' => 'testing', |
||||
'secret' => 'testing00testing00testing', |
||||
], |
||||
], |
||||
]; |
||||
|
||||
$config->socialEnabled = true; |
||||
$this->library = new Aauth($config, $session); |
||||
|
||||
$this->assertEquals(['Facebook'], $this->library->getProviders()); |
||||
} |
||||
|
||||
public function testRebuildSocialStorage() |
||||
{ |
||||
$testArray = ['facebook.expires_at' => strtotime('+1 hour')]; |
||||
$this->hasInDatabase($this->config->dbTableUserVariables, [ |
||||
'user_id' => 1, |
||||
'data_key' => 'social_storage', |
||||
'data_value' => json_encode($testArray), |
||||
'system' => true, |
||||
]); |
||||
|
||||
$config = new AauthConfig(); |
||||
$session = $this->getInstance(); |
||||
|
||||
$config->socialEnabled = true; |
||||
$config->socialProviders = [ |
||||
'Facebook' => [ |
||||
'enabled' => true, |
||||
'keys' => [ |
||||
'id' => 'testing', |
||||
'secret' => 'testing00testing00testing', |
||||
], |
||||
], |
||||
]; |
||||
|
||||
$this->library = new Aauth($config, $session); |
||||
$this->library->rebuildSocialStorage(1); |
||||
|
||||
$this->assertTrue(isset($_SESSION['HYBRIDAUTH::STORAGE'])); |
||||
$this->assertEquals($testArray, $_SESSION['HYBRIDAUTH::STORAGE']); |
||||
} |
||||
} |
Loading…
Reference in new issue