-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multi Auth integration #206 #226
Changes from 44 commits
b3066c9
df5545c
3652a36
ab80d90
765927b
0ddedd8
3e259b6
fc2566a
6b18359
49d9069
38056ee
74e47a7
3fa4666
eb565be
c211394
5f5f397
1632ce6
5dec51a
d5c248d
2fd9b28
ebf4a1f
b81e5c0
46c9078
fe83121
719964b
dedae84
69124b7
deebdbd
96dc8dd
ae3b702
36bc319
c984180
67e220b
0a44eaa
4d8fdeb
c303055
6f0b893
05983a4
aaf0e5c
dbdaada
49daa81
d68cd71
e0ad5ce
98fec4c
0c8465f
d0b34db
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ nbproject | |
composer.lock | ||
composer.phar | ||
/vendor/* | ||
phpunit.xml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: yuklia | ||
* Date: 05.05.15 | ||
* Time: 14:04 | ||
*/ | ||
|
||
/** | ||
* @link http://hybridauth.sourceforge.net/userguide/Configuration.html | ||
*/ | ||
return array( | ||
//"base_url" the url that point to HybridAuth Endpoint (where index.php and config.php are found) | ||
"base_url" => "[http|https][your.domain]/auth/endpoint", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. то что в |
||
|
||
"providers" => array( | ||
"Google" => array( // 'id' is your google client id | ||
"enabled" => true, | ||
"wrapper" => array( "path" => "Providers/Google.php", "class" => "Hybrid_Providers_Google" ), | ||
"keys" => array("id" => "%%client_id%%", | ||
"secret" => "%%client_secret%%"), | ||
"scope" => "https://www.googleapis.com/auth/userinfo.profile ". // optional | ||
"https://www.googleapis.com/auth/userinfo.email" , // optional | ||
"access_type" => "offline", // optional | ||
"approval_prompt" => "force", // optional | ||
), | ||
|
||
"Facebook" => array( // 'id' is your facebook application id | ||
"enabled" => true, | ||
"wrapper" => array( "path" => "Providers/Facebook.php", "class" => "Hybrid_Providers_Facebook" ), | ||
"keys" => array("id" => "%%appId%%", "secret" => "%%secret%%"), | ||
"scope" => "email, user_about_me, user_birthday, user_hometown, publish_actions", // optional | ||
), | ||
|
||
"Twitter" => array( // 'key' is your twitter application consumer key | ||
"enabled" => true, | ||
"wrapper" => array( "path" => "Providers/Twitter.php", "class" => "Hybrid_Providers_Twitter" ), | ||
"keys" => array("key" => "%%consumerKey%%", "secret" => "%%consumerSecret%%") | ||
) | ||
), | ||
|
||
"debug_mode" => false, | ||
|
||
// to enable logging, set 'debug_mode' to true, then provide here a path of a writable file | ||
"debug_file" =>'' | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: yuklia | ||
* Date: 06.05.15 | ||
* Time: 18:41 | ||
*/ | ||
namespace Application\Auth; | ||
|
||
interface AuthInterface | ||
{ | ||
/** | ||
* @param array $data | ||
* @param \Application\Users\Row $user | ||
* @return void | ||
*/ | ||
public function registration($data, $user); | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function authProcess(); | ||
|
||
/** | ||
* @return array | ||
* @throws \Application\Exception | ||
*/ | ||
public function getOptions(); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Придираюсь, но перевод строки лишний :) |
||
|
||
/** | ||
* @param \Application\Auth\Row $auth | ||
* @return mixed | ||
*/ | ||
public function alreadyRegisteredLogic($auth); | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getProfile(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
<?php | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. А тут нет заголовка файла, класса и т.д |
||
namespace Application\Auth; | ||
|
||
use Bluz\Proxy\Config; | ||
use Bluz\Proxy\Messages; | ||
use Application\Auth; | ||
use Application\Users; | ||
|
||
class AuthProvider implements AuthInterface | ||
{ | ||
/** @var \Bluz\Http\Response */ | ||
protected $response; | ||
|
||
/** @var \Application\Users\Row $identity */ | ||
protected $identity; | ||
|
||
/** @var \Hybrid_Auth $hybridauth */ | ||
protected $hybridauth; | ||
|
||
/** @var \Hybrid_Provider_Adapter $authAdapter */ | ||
protected $authAdapter; | ||
|
||
/** | ||
* the same name as was mentioned in hybridauth config section providers | ||
* @var string | ||
*/ | ||
protected $providerName; | ||
|
||
public function __construct($providerName) | ||
{ | ||
if (!in_array(ucfirst($providerName), $this->getAvailableProviders())) { | ||
throw new \Exception(sprintf('Provider % is not defined in configuration file', ucfirst($providerName))); | ||
} | ||
$this->providerName = ucfirst($providerName); | ||
} | ||
|
||
|
||
/** | ||
* @return \Hybrid_Auth | ||
*/ | ||
public function getHybridauth() | ||
{ | ||
if (!$this->hybridauth) { | ||
$this->hybridauth = new \Hybrid_Auth($this->getOptions()); | ||
} | ||
|
||
return $this->hybridauth; | ||
} | ||
|
||
public function setHybridauth($hybridauth) | ||
{ | ||
$this->hybridauth = $hybridauth; | ||
} | ||
|
||
|
||
/** | ||
* @param \Bluz\Http\Response $response | ||
*/ | ||
public function setResponse($response) | ||
{ | ||
$this->response = $response; | ||
} | ||
|
||
/** | ||
* @return \Bluz\Http\Response | ||
*/ | ||
public function getResponse() | ||
{ | ||
return $this->response; | ||
} | ||
|
||
/** | ||
* @param \Application\Users\Row $identity | ||
*/ | ||
public function setIdentity($identity) | ||
{ | ||
$this->identity = $identity; | ||
} | ||
|
||
/** | ||
* @return \Application\Users\Row $user | ||
*/ | ||
public function getIdentity() | ||
{ | ||
return $this->identity; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getProviderName() | ||
{ | ||
return $this->providerName; | ||
} | ||
|
||
/** | ||
* @param string $providerName | ||
*/ | ||
public function setProviderName($providerName) | ||
{ | ||
$this->providerName = $providerName; | ||
} | ||
|
||
/** | ||
* @return \Hybrid_Provider_Adapter | ||
* @throws \Exception | ||
*/ | ||
public function getAuthAdapter() | ||
{ | ||
if (!$this->authAdapter) { | ||
/** @var \Hybrid_Provider_Adapter $authProvider */ | ||
$this->authAdapter = $this->getHybridauth()->authenticate($this->providerName); | ||
|
||
if (!$this->authAdapter->isUserConnected()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему используется корневой |
||
throw new \Exception('Cannot connect to current provider !'); | ||
} | ||
} | ||
|
||
return $this->authAdapter; | ||
} | ||
|
||
/** | ||
* @param \Hybrid_Provider_Adapter $authAdapter | ||
*/ | ||
public function setAuthAdapter($authAdapter) | ||
{ | ||
$this->authAdapter = $authAdapter; | ||
} | ||
|
||
|
||
/** | ||
* @param \Hybrid_User_Profile $data | ||
* @param \Application\Users\Row $user | ||
* @return void | ||
*/ | ||
public function registration($data, $user) | ||
{ | ||
$row = new Auth\Row(); | ||
$row->userId = $user->id; | ||
$row->provider = strtolower($this->providerName); | ||
|
||
$row->foreignKey = $data->identifier; | ||
$row->token = $this->authAdapter->getAccessToken()['access_token']; | ||
$row->tokenSecret = ($this->authAdapter->getAccessToken()['access_token_secret']) ? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Сократить же можно до |
||
$this->authAdapter->getAccessToken()['access_token_secret'] : ''; | ||
$row->tokenType = Auth\Table::TYPE_ACCESS; | ||
$row->save(); | ||
|
||
Messages::addNotice(sprintf('Your account was linked to %s successfully !', $this->providerName)); | ||
$this->response->redirectTo('users', 'profile', ['id' => $user->id]); | ||
} | ||
|
||
|
||
/** | ||
* @return void | ||
*/ | ||
public function authProcess() | ||
{ | ||
|
||
$this->authAdapter = $this->getAuthAdapter(); | ||
$profile = $this->getProfile(); | ||
|
||
/** | ||
* @var Auth\Table $authTable | ||
*/ | ||
$authTable = Auth\Table::getInstance(); | ||
$auth = $authTable->getAuthRow(strtolower($this->providerName), $profile->identifier); | ||
|
||
|
||
if ($this->identity) { | ||
if ($auth) { | ||
Messages::addNotice(sprintf('You have already linked to %s', $this->providerName)); | ||
$this->response->redirectTo('users', 'profile', ['id' => $this->identity->id]); | ||
} else { | ||
$user = Users\Table::findRow($this->identity->id); | ||
$this->registration($profile, $user); | ||
} | ||
} | ||
|
||
if ($auth) { | ||
$this->alreadyRegisteredLogic($auth); | ||
} else { | ||
Messages::addError(sprintf('First you need to be linked to %s', $this->providerName)); | ||
$this->response->redirectTo('users', 'signin'); | ||
} | ||
} | ||
|
||
/** | ||
* @return array | ||
* @throws \Application\Exception | ||
*/ | ||
public function getOptions() | ||
{ | ||
return Config::getData('hybridauth'); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function getAvailableProviders() | ||
{ | ||
return array_keys(Config::getData('hybridauth')['providers']); | ||
} | ||
|
||
/** | ||
* @param $auth | ||
* @return mixed | ||
*/ | ||
public function alreadyRegisteredLogic($auth) | ||
{ | ||
$user = Users\Table::findRow($auth->userId); | ||
|
||
if ($user->status != Users\Table::STATUS_ACTIVE) { | ||
Messages::addError('User is not active'); | ||
} | ||
|
||
$user->login(); | ||
$this->response->redirectTo('index', 'index'); | ||
} | ||
|
||
/** | ||
* @return \Hybrid_User_Profile | ||
*/ | ||
public function getProfile() | ||
{ | ||
return $this->authAdapter->getUserProfile(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Пора бы на что-нить более стандартное поменять