From b3066c9cc93dc9a80ec136794cefd4f68318096c Mon Sep 17 00:00:00 2001 From: yuklia Date: Thu, 26 Feb 2015 18:58:31 +0200 Subject: [PATCH 01/46] mailer --- application/configs/default/mailer.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/application/configs/default/mailer.php b/application/configs/default/mailer.php index 669dc52a..abea52e2 100644 --- a/application/configs/default/mailer.php +++ b/application/configs/default/mailer.php @@ -13,6 +13,16 @@ "name" => "Bluz" ], "settings" => [ - "CharSet" => "UTF-8" + "CharSet" => "UTF-8", + "Mailer" => "smtp", // mail, sendmail, smtp, qmail + "Host" => "10.10.0.114", + "Port" => "2525" + ], + + // Custom Headers + "headers" => [ + "PROJECT" => "Bluz", + 'EMAILS' => 'y.kostrikova@nixsolutions.com, baziak@nixsolutions.com', + 'EXTERNAL' => false ], ); From df5545c8c0fc31dd315b220853a21e2ea3a2dbfc Mon Sep 17 00:00:00 2001 From: yuklia Date: Tue, 5 May 2015 17:55:35 +0300 Subject: [PATCH 02/46] hybrid auth --- application/configs/default/hybridauth.php | 38 ++++++++ application/configs/default/opauth.php | 47 ++++++++++ application/modules/auth/controllers/auth.php | 22 +++++ .../modules/index/controllers/index.php | 18 ++++ composer.json | 94 ++++++++++--------- 5 files changed, 174 insertions(+), 45 deletions(-) create mode 100644 application/configs/default/hybridauth.php create mode 100644 application/configs/default/opauth.php create mode 100644 application/modules/auth/controllers/auth.php diff --git a/application/configs/default/hybridauth.php b/application/configs/default/hybridauth.php new file mode 100644 index 00000000..b5fa9e09 --- /dev/null +++ b/application/configs/default/hybridauth.php @@ -0,0 +1,38 @@ + "http://sk.com/auth/auth", + + "providers" => array( + // google + /* "Google" => array( // 'id' is your google client id + "enabled" => true, + "keys" => array("id" => "", "secret" => "") + ),*/ + + // facebook + /*"Facebook" => array( // 'id' is your facebook application id + "enabled" => true, + "keys" => array("id" => "1413483462306154", "secret" => "0911925061bdb04a3d8c41129f672065"), + "scope" => "email, user_about_me, user_birthday, user_hometown", // optional + ),*/ + + // twitter + "Twitter" => array( // 'key' is your twitter application consumer key + "enabled" => true, + "keys" => array("key" => "eOMvWh3ODqk4A1MHEeolm0Cfv", "secret" => "aJSrRzFlJxZp60IhTPnsOetwYVZ6XBZmcd4wQbPwyc7hgIYg0M") + ) + ), + + "debug_mode" => false, + + // to enable logging, set 'debug_mode' to true, then provide here a path of a writable file + "debug_file" => "", + ); \ No newline at end of file diff --git a/application/configs/default/opauth.php b/application/configs/default/opauth.php new file mode 100644 index 00000000..3442dd30 --- /dev/null +++ b/application/configs/default/opauth.php @@ -0,0 +1,47 @@ + '/', + + /** + * Callback URL: redirected to after authentication, successful or otherwise + */ + 'callback_url' => '{path}callback.php', + + /** + * A random string used for signing of $auth response. + */ + 'security_salt' => 'LDFmiilYf8Fyw5W10rx4W1KsVrieQCnpBzzpTBWA5vJidQKDx8pMJbmw28R1C4m', + + /** + * Strategy + * Refer to individual strategy's documentation on configuration requirements. + * + * eg. + * 'Strategy' => array( + * + * 'Facebook' => array( + * 'app_id' => 'APP ID', + * 'app_secret' => 'APP_SECRET' + * ), + * + * ) + * + */ + 'Strategy' => array( + // Define strategies and their respective configs here + + ), +); \ No newline at end of file diff --git a/application/modules/auth/controllers/auth.php b/application/modules/auth/controllers/auth.php new file mode 100644 index 00000000..4d13db92 --- /dev/null +++ b/application/modules/auth/controllers/auth.php @@ -0,0 +1,22 @@ +authenticate( "Twitter" ); + + if ($twitter->isUserConnected()) { + $user_profile = $twitter->getUserProfile(); + echo "Hi there! " . $user_profile->displayName; + } + + } + catch( Exception $e ){ + echo "Ooophs, we got an error: " . $e->getMessage(); + } }; diff --git a/composer.json b/composer.json index ec2a6a38..e60834e1 100644 --- a/composer.json +++ b/composer.json @@ -1,49 +1,53 @@ { - "name": "bluzphp/skeleton", - "description": "Skeleton application for Bluz, a lightweight PHP framework", - "minimum-stability": "dev", - "type": "library", - "require": { - "php": ">=5.4", - "bluzphp/framework": "~0.9", - "phpmailer/phpmailer": "~5.2", - "guzzle/guzzle": "~3.8", - "zircote/swagger-php": "~0.9" + "name": "bluzphp/skeleton", + "description": "Skeleton application for Bluz, a lightweight PHP framework", + "minimum-stability": "dev", + "type": "library", + "require": { + "php": ">=5.4", + "bluzphp/framework": "~0.9", + "phpmailer/phpmailer": "~5.2", + "guzzle/guzzle": "~3.8", + "zircote/swagger-php": "~0.9" + }, + "require-dev": { + "phpunit/phpunit": "~4.4", + "satooshi/php-coveralls": "~0.6", + "squizlabs/php_codesniffer": "~2.0", + "zendframework/zend-dom": "~2.3", + "hybridauth/hybridauth": "dev-master", + "opauth/opauth": "*", + "opauth/facebook": "*", + "opauth/twitter": "*" + }, + "config": { + "vendor-dir": "vendor" + }, + "autoload": { + "psr-0": { + "": "application/library" }, - "require-dev": { - "phpunit/phpunit": "~4.4", - "satooshi/php-coveralls": "~0.6", - "squizlabs/php_codesniffer": "~2.0", - "zendframework/zend-dom": "~2.3" + "psr-4": { + "Application\\": "application/models", + "Application\\Tests\\": "tests/library" }, - "config": { - "vendor-dir": "vendor" - }, - "autoload": { - "psr-0": { - "": "application/library" - }, - "psr-4": { - "Application\\": "application/models", - "Application\\Tests\\": "tests/library" - }, - "files": [ - "application/_functions.php", - "application/_loader.php" - ] - }, - "scripts": { - "post-install-cmd": "php bin/install.php" - }, - "authors": [ - { - "name": "Bluz Skeleton Contributors", - "homepage": "https://github.com/bluzphp/skeleton/graphs/contributors" - } - ], - "support": { - "issues": "https://github.com/bluzphp/skeleton/issues", - "wiki": "https://github.com/bluzphp/skeleton/wiki" - }, - "license": "MIT" + "files": [ + "application/_functions.php", + "application/_loader.php" + ] + }, + "scripts": { + "post-install-cmd": "php bin/install.php" + }, + "authors": [ + { + "name": "Bluz Skeleton Contributors", + "homepage": "https://github.com/bluzphp/skeleton/graphs/contributors" + } + ], + "support": { + "issues": "https://github.com/bluzphp/skeleton/issues", + "wiki": "https://github.com/bluzphp/skeleton/wiki" + }, + "license": "MIT" } From 3652a36db659c8ff7b47fedaa598802525f7a8a5 Mon Sep 17 00:00:00 2001 From: yuklia Date: Tue, 5 May 2015 18:15:00 +0300 Subject: [PATCH 03/46] hotfix --- application/configs/default/hybridauth.php | 8 ++++---- application/modules/index/controllers/index.php | 4 +++- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/application/configs/default/hybridauth.php b/application/configs/default/hybridauth.php index b5fa9e09..889f5cda 100644 --- a/application/configs/default/hybridauth.php +++ b/application/configs/default/hybridauth.php @@ -12,17 +12,17 @@ "providers" => array( // google - /* "Google" => array( // 'id' is your google client id + "Google" => array( // 'id' is your google client id "enabled" => true, "keys" => array("id" => "", "secret" => "") - ),*/ + ), // facebook - /*"Facebook" => array( // 'id' is your facebook application id + "Facebook" => array( // 'id' is your facebook application id "enabled" => true, "keys" => array("id" => "1413483462306154", "secret" => "0911925061bdb04a3d8c41129f672065"), "scope" => "email, user_about_me, user_birthday, user_hometown", // optional - ),*/ + ), // twitter "Twitter" => array( // 'key' is your twitter application consumer key diff --git a/application/modules/index/controllers/index.php b/application/modules/index/controllers/index.php index 7b992693..a695bfe6 100644 --- a/application/modules/index/controllers/index.php +++ b/application/modules/index/controllers/index.php @@ -26,13 +26,15 @@ function () { try{ $hybridauth = new \Hybrid_Auth( $config ); - $twitter = $hybridauth->authenticate( "Twitter" ); + $twitter = $hybridauth->authenticate( "Facebook" ); if ($twitter->isUserConnected()) { $user_profile = $twitter->getUserProfile(); echo "Hi there! " . $user_profile->displayName; + $twitter->logout(); } + } catch( Exception $e ){ echo "Ooophs, we got an error: " . $e->getMessage(); From ab80d90b831218e30952076fb7c79e7a24db1621 Mon Sep 17 00:00:00 2001 From: yuklia Date: Tue, 5 May 2015 18:41:15 +0300 Subject: [PATCH 04/46] test --- application/configs/default/auth.php | 4 +- application/configs/default/hybridauth.php | 2 +- application/modules/auth/controllers/auth.php | 37 ++++++++++++++++++- .../modules/auth/controllers/endpoint.php | 22 +++++++++++ .../modules/index/controllers/index.php | 18 --------- 5 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 application/modules/auth/controllers/endpoint.php diff --git a/application/configs/default/auth.php b/application/configs/default/auth.php index eecc4782..8286aca9 100644 --- a/application/configs/default/auth.php +++ b/application/configs/default/auth.php @@ -12,8 +12,8 @@ } ), "facebook" => array( - "appId" => "%%appId%%", - "secret" => "%%secret%%", + "appId" => "1413483462306154", + "secret" => "0911925061bdb04a3d8c41129f672065", ), "twitter" => array( "consumer_key" => "%%consumerKey%%", diff --git a/application/configs/default/hybridauth.php b/application/configs/default/hybridauth.php index 889f5cda..ecb1f93f 100644 --- a/application/configs/default/hybridauth.php +++ b/application/configs/default/hybridauth.php @@ -8,7 +8,7 @@ return array( //"base_url" the url that point to HybridAuth Endpoint (where index.php and config.php are found) - "base_url" => "http://sk.com/auth/auth", + "base_url" => "http://sk.com/auth/endpoint", "providers" => array( // google diff --git a/application/modules/auth/controllers/auth.php b/application/modules/auth/controllers/auth.php index 4d13db92..1166a1f6 100644 --- a/application/modules/auth/controllers/auth.php +++ b/application/modules/auth/controllers/auth.php @@ -9,6 +9,8 @@ namespace Application; use Application\Users; +use Bluz\Proxy\Messages; +use Bluz\Proxy\Request; return /** @@ -18,5 +20,38 @@ */ function () { - include PATH_VENDOR."/"."hybridauth/hybridauth/hybridauth/index.php"; + try{ + $provider = Request::getParam('provider'); + $auth = new AuthFactory(); + $auth->setProvider($provider); + $auth->setResponse($this); + $auth->setService($this->getService()); + $auth->setIdentity($this->user()); + $auth->authProcess(); + }catch (Exception $e) { + Messages::addError($e->getMessage()); + } + return + function () { + return false; + }; + + /** + * $config = Config::getData('hybridauth'); + + try{ + $hybridauth = new \Hybrid_Auth( $config ); + + $twitter = $hybridauth->authenticate( "Facebook" ); + + if ($twitter->isUserConnected()) { + $user_profile = $twitter->getUserProfile(); + echo "Hi there! " . $user_profile->displayName; + $twitter->logout(); + } + } + catch( Exception $e ){ + echo "Ooophs, we got an error: " . $e->getMessage(); + } + */ }; diff --git a/application/modules/auth/controllers/endpoint.php b/application/modules/auth/controllers/endpoint.php new file mode 100644 index 00000000..4d13db92 --- /dev/null +++ b/application/modules/auth/controllers/endpoint.php @@ -0,0 +1,22 @@ +authenticate( "Facebook" ); - - if ($twitter->isUserConnected()) { - $user_profile = $twitter->getUserProfile(); - echo "Hi there! " . $user_profile->displayName; - $twitter->logout(); - } - - - } - catch( Exception $e ){ - echo "Ooophs, we got an error: " . $e->getMessage(); - } }; From 765927baa42a89cd2dadce4a71f46899fe07dfe3 Mon Sep 17 00:00:00 2001 From: yuklia Date: Tue, 5 May 2015 18:54:51 +0300 Subject: [PATCH 05/46] hotfix --- application/modules/auth/controllers/auth.php | 7 +------ application/modules/users/views/signin.phtml | 6 +++--- public/js/bluz.auth.js | 17 +++++++++++++++++ 3 files changed, 21 insertions(+), 9 deletions(-) create mode 100644 public/js/bluz.auth.js diff --git a/application/modules/auth/controllers/auth.php b/application/modules/auth/controllers/auth.php index 1166a1f6..f16fd069 100644 --- a/application/modules/auth/controllers/auth.php +++ b/application/modules/auth/controllers/auth.php @@ -22,12 +22,7 @@ function () { try{ $provider = Request::getParam('provider'); - $auth = new AuthFactory(); - $auth->setProvider($provider); - $auth->setResponse($this); - $auth->setService($this->getService()); - $auth->setIdentity($this->user()); - $auth->authProcess(); + }catch (Exception $e) { Messages::addError($e->getMessage()); } diff --git a/application/modules/users/views/signin.phtml b/application/modules/users/views/signin.phtml index 65e2dfd4..08363085 100644 --- a/application/modules/users/views/signin.phtml +++ b/application/modules/users/views/signin.phtml @@ -34,13 +34,13 @@ diff --git a/public/js/bluz.auth.js b/public/js/bluz.auth.js new file mode 100644 index 00000000..79b45a6b --- /dev/null +++ b/public/js/bluz.auth.js @@ -0,0 +1,17 @@ +/** + * Created by yuklia on 04.03.15. + */ +define(['jquery'], function ($) { + "use strict"; + $(function(){ + $('.provider').on('click', function(event){ + event.preventDefault(); + var provider = $(this).data('provider'); + $.ajax({ + type: "POST", + url: "auth/auth", + data: { provider: provider } + }); + }) + }) +}); From 0ddedd81dbf5f56da36e37502150ecc562358830 Mon Sep 17 00:00:00 2001 From: yuklia Date: Wed, 6 May 2015 08:18:44 +0300 Subject: [PATCH 06/46] test --- application/models/Auth/AbstractAuth.php | 250 ++++++++++++++++++ application/models/Auth/AuthFactory.php | 68 +++++ application/models/Auth/AuthInterface.php | 52 ++++ application/models/Auth/Facebook.php | 89 +++++++ application/models/Auth/Google.php | 147 ++++++++++ application/modules/auth/controllers/auth.php | 16 +- 6 files changed, 618 insertions(+), 4 deletions(-) create mode 100644 application/models/Auth/AbstractAuth.php create mode 100644 application/models/Auth/AuthFactory.php create mode 100644 application/models/Auth/AuthInterface.php create mode 100644 application/models/Auth/Facebook.php create mode 100644 application/models/Auth/Google.php diff --git a/application/models/Auth/AbstractAuth.php b/application/models/Auth/AbstractAuth.php new file mode 100644 index 00000000..c6f23600 --- /dev/null +++ b/application/models/Auth/AbstractAuth.php @@ -0,0 +1,250 @@ +hybridauth = new \Hybrid_Auth($this->getOptions()); + } + + /** + * @param mixed $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\AbstractService $service + */ + public function setService($service) + { + $this->service = $service; + } + + /** + * @return \Application\AbstractService + */ + public function getService() + { + return $this->service; + } + + /** + * @param \Application\Users\Entity\User $identity + */ + public function setIdentity($identity) + { + $this->identity = $identity; + } + + /** + * @return \Application\Users\Entity\User + */ + public function getIdentity() + { + return $this->identity; + } + + /** + * @param \Application\Auth\AuthService $authService + */ + public function setAuthService($authService) + { + $this->authService = $authService; + } + + /** + * @return \Application\Auth\AuthService + */ + public function getAuthService() + { + return $this->authService; + } + + /** + * @param \Application\Users\UserService $userService + */ + public function setUserService($userService) + { + $this->userService = $userService; + } + + /** + * @return \Application\Users\UserService + */ + public function getUserService() + { + return $this->userService; + } + + + /** + * @param array $data + * @param \Application\Users\Entity\User $user + * @return void + */ + public function registration($data, $user) + { + // TODO: Implement registration() method. + } + + /** + * @return void + */ + public function authProcess() + { + $elements = explode('\\', get_class($this)); + $providerName = end($elements); + $profile = $this->getProfile(); + + if (!$profile) { + /** + * If user doesn't allow application yet, redirect him to fb page for this. + * After this operation we will returned to this file. + * Is user declined app, we get param 'error' => 'access_denied' + */ + + // if user declined + if ('access_denied' == Request::getParam('error', null)) { + $this->response->redirectTo('users', 'signin'); + } + } + + /* if ( \Bluz\Proxy\Auth::getIdentity()) { + $this->response->redirectTo('index', 'index'); + }*/ + + /** + * @var Auth\Table $authTable + */ + $authTable = Auth\Table::getInstance(); + $auth = $authTable->getAuthRow(strtolower($providerName), $profile['id']); + + if ($auth) { + // if user has been registered + $user = Users\Table::findRow($auth->userId); + + $user->login(); + + if ($user->status != Users\Table::STATUS_ACTIVE) { + Messages::addError('User is not active'); + } + + $this->response->redirectTo('index', 'index'); + } else { + // sign up user + + // continue with registration + /** + * Write facebook response to session + */ + Session::set('facebook', $profile); + Messages::addNotice('To finish your registration fill the form'); + $this->redirectTo('users', 'signup'); + + + } + /* if ($this->identity) { + if ($auth) { + Messages::addNotice(sprintf('You have already linked to %s', $providerName)); + $this->response->redirectTo('users', 'profile', ['id' => $this->identity->getId()]); + } else { + $user = $this->userService->readOne($this->identity->getId()); + $this->registration($profile, $user); + } + } + + if ($auth) { + $this->alreadyRegisteredLogic($auth); //need to be not a proxy object + } else { + Messages::addError('You need to sign up via Ldap first'); + $this->response->redirectTo('users', 'signin'); + }*/ + + } + + /** + * @return array + * @throws \Application\Exception + */ + private function getOptions() + { + + } + + /** + * @return void + */ + public function redirectLogic() + { + // TODO: Implement redirectLogic() method. + } + + /** + * @param Auth $auth + * @return mixed + */ + public function alreadyRegisteredLogic(Auth $auth) + { + // TODO: Implement alreadyRegisteredLogic() method. + } + + /** + * @return array + */ + public function getProfile() + { + // TODO: Implement getProfile() method. + } + + public function setProvider($provider) + { + // TODO: Implement setProvider() method. + } + + /** + * @return mixed + */ + public function getProvider() + { + // TODO: Implement getProvider() method. + } +} \ No newline at end of file diff --git a/application/models/Auth/AuthFactory.php b/application/models/Auth/AuthFactory.php new file mode 100644 index 00000000..be03ec3a --- /dev/null +++ b/application/models/Auth/AuthFactory.php @@ -0,0 +1,68 @@ +authType = new $className(); + } + + + /** + * @param \Bluz\Http\Response $response + */ + public function setResponse($response) + { + $this->authType->setResponse($response); + } + + + /** + * @param mixed $identity + */ + public function setIdentity($identity) + { + $this->authType->setIdentity($identity); + } + + /** + * delegate to type + */ + public function authProcess(){ + + $this->authType->authProcess(); + } + + public function getProvider(){ + + return $this->authType; + } +} \ No newline at end of file diff --git a/application/models/Auth/AuthInterface.php b/application/models/Auth/AuthInterface.php new file mode 100644 index 00000000..40de5409 --- /dev/null +++ b/application/models/Auth/AuthInterface.php @@ -0,0 +1,52 @@ +setForeignKey($profile['id']); + $auth->setProvider(Auth::PROVIDER_FACEBOOK); + $auth->setToken($this->facebook->getAccessToken()); + $auth->setTokenType(AuthTable::TYPE_ACCESS); + $auth->setUser($user); + $auth->setTokenSecret(0); + $this->authService->saveObject($auth); + Messages::addNotice('Your account was linked to Facebook successfully !'); + $this->response->redirectTo('users', 'profile', ['id' => $user->getId()]); + + } + + /** + * @return array + */ + public function getProfile() + { + /** @var \Hybrid_Auth $hybridauth */ + $hybridauth = $this->getHybridauth(); + + $facebook = $hybridauth->authenticate( "Facebook" ); + + return $facebook->getUserProfile(); + } + + /** + * void + */ + public function redirectLogic() + { + $facebookConf = Config::getData('auth', 'facebook'); + //todo::need to be wrapped in plugin + $scheme = Request::getScheme() . '://'; + $host = Request::getHttpHost(); + $url = $facebookConf['redirect-uri']; + + // if user declined + if ('access_denied' == Request::getParam('error', null)) { + $this->response->redirectTo('users', 'profile', ['id' => $this->identity->getId()]); + } + $login_url = $this->facebook->getLoginUrl(array( + 'scope' => 'email', + 'redirect_uri' => $scheme . $host . '/' . $url)); + $this->response->redirect($login_url); + } + + /** + * @param Auth $auth + * @throws \Bluz\Auth\AuthException + * @throws \Exception + */ + public function alreadyRegisteredLogic(Auth $auth) + { + $auth->setToken($this->facebook->getAccessToken()); + $this->authService->updateObject($auth); + $user = $auth->getUser(); + + if ($user->getStatus() != User::STATUS_ACTIVE) { + Messages::addError('User is not active'); + } + + $this->userService->login($user); + $this->response->redirectTo('index', 'index'); + } + +} \ No newline at end of file diff --git a/application/models/Auth/Google.php b/application/models/Auth/Google.php new file mode 100644 index 00000000..69060aa9 --- /dev/null +++ b/application/models/Auth/Google.php @@ -0,0 +1,147 @@ +response->redirect($googleAuth->getAuthUrl($redirectUri)); + } + + $this->code = $code; + } + + /** + * @return mixed + */ + public function getCode() + { + return $this->code; + } + + /** + * @param $profile + * @param $user + */ + public function registration($profile, $user) + { + $auth = new \Application\Auth\Entity\Auth(); + $auth->setForeignKey($profile['id']); + $auth->setProvider(\Application\Auth\Entity\Auth::PROVIDER_GOOGLE); + $auth->setToken($this->google->accessToken); + if($this->google->refreshToken){ + $auth->setRefreshToken($this->google->refreshToken); + } + $auth->setTokenType('access'); + $auth->setUser($user); + $auth->setTokenSecret(0); + $this->authService->saveObject($auth); + Messages::addNotice('Your account was linked to Google successfully !'); + $this->response->redirectTo('users', 'profile', ['id' => $user->getId()]); + + } + + + /** + * @return array|mixed + * @throws \Exception + */ + public function getOptions() + { + $options = Config::getData('auth', 'google'); + if (!$options || !isset($options['client_id'], $options['client_secret']) + || empty($options['client_id']) || empty($options['client_secret']) + ) { + throw new \Exception('Google authorization is not configured'); + } + return $options; + } + + /** + * @return void + */ + public function redirectLogic() + { + $login_url = $this->google->getAuthUrl(Router::getFullUrl('google', 'redirect_uri')); + $this->response->redirect($login_url); + } + + /** + * @param Auth $auth + * @throws \Bluz\Auth\AuthException + * @throws \Exception + */ + public function alreadyRegisteredLogic(Auth $auth) + { + $user = $auth->getUser(); + + if ($user->getStatus() != User::STATUS_ACTIVE) { + Messages::addError('User is not active'); + } + + $this->userService->login($user); + $this->response->redirectTo('index', 'index'); + + } + + /** + * @return array + */ + public function getProfile() + { + //extend access_token live or get new one + $options = $this->getOptions(); + $this->google = new Client($options); + $config = Config::getData('auth', 'google'); + //todo::need to be wrapped in plugin + $scheme = Request::getScheme() . '://'; + $host = Request::getHttpHost(); + $url = $config['redirect-uri']; + $redirectUri = $scheme . $host . '/' . $url; + $this->google->getOauthAccessToken($this->code, $redirectUri); //getting temporary token + $userGoogle = $this->google->getUserInfo(); + if ($userGoogle) { + return $userGoogle; + } + /** + * If user doesn't allow application yet, redirect him to fb page for this. + * After this operation we will returned to this file. + * Is user declined app, we get param 'error' => 'access_denied' + */ + $this->redirectLogic(); + + } + + +} \ No newline at end of file diff --git a/application/modules/auth/controllers/auth.php b/application/modules/auth/controllers/auth.php index f16fd069..db7a7258 100644 --- a/application/modules/auth/controllers/auth.php +++ b/application/modules/auth/controllers/auth.php @@ -8,6 +8,7 @@ namespace Application; +use Application\Auth\AuthFactory; use Application\Users; use Bluz\Proxy\Messages; use Bluz\Proxy\Request; @@ -20,9 +21,16 @@ */ function () { + /** + * @var Bootstrap $this + */ try{ $provider = Request::getParam('provider'); - + $auth = new AuthFactory(); + $auth->setProvider($provider); + $auth->setResponse($this); + $auth->setIdentity($this->user()); + $auth->authProcess(); }catch (Exception $e) { Messages::addError($e->getMessage()); } @@ -31,8 +39,8 @@ function () { return false; }; - /** - * $config = Config::getData('hybridauth'); + + // $config = Config::getData('hybridauth'); try{ $hybridauth = new \Hybrid_Auth( $config ); @@ -48,5 +56,5 @@ function () { catch( Exception $e ){ echo "Ooophs, we got an error: " . $e->getMessage(); } - */ + }; From 3e259b603e2280c13225ce48c57f47fb1f77d008 Mon Sep 17 00:00:00 2001 From: yuklia Date: Wed, 6 May 2015 13:28:01 +0300 Subject: [PATCH 07/46] sign in via twitter --- application/configs/default/hybridauth.php | 10 +- application/models/Auth/AbstractAuth.php | 181 +++++------------- application/models/Auth/AuthFactory.php | 8 +- application/models/Auth/AuthInterface.php | 8 +- application/models/Auth/Twitter.php | 54 ++++++ application/modules/auth/controllers/auth.php | 6 +- application/modules/users/views/profile.phtml | 54 ++++-- application/modules/users/views/signin.phtml | 5 + 8 files changed, 167 insertions(+), 159 deletions(-) create mode 100644 application/models/Auth/Twitter.php diff --git a/application/configs/default/hybridauth.php b/application/configs/default/hybridauth.php index ecb1f93f..a4e151bf 100644 --- a/application/configs/default/hybridauth.php +++ b/application/configs/default/hybridauth.php @@ -6,6 +6,9 @@ * Time: 14:04 */ +/** + * You must define provider class inside providers scope + */ return array( //"base_url" the url that point to HybridAuth Endpoint (where index.php and config.php are found) "base_url" => "http://sk.com/auth/endpoint", @@ -14,7 +17,8 @@ // google "Google" => array( // 'id' is your google client id "enabled" => true, - "keys" => array("id" => "", "secret" => "") + "keys" => array("id" => "", "secret" => ""), + "provider" => 'Application\Auth\Google' ), // facebook @@ -22,12 +26,14 @@ "enabled" => true, "keys" => array("id" => "1413483462306154", "secret" => "0911925061bdb04a3d8c41129f672065"), "scope" => "email, user_about_me, user_birthday, user_hometown", // optional + "provider" => 'Application\Auth\Facebook' ), // twitter "Twitter" => array( // 'key' is your twitter application consumer key "enabled" => true, - "keys" => array("key" => "eOMvWh3ODqk4A1MHEeolm0Cfv", "secret" => "aJSrRzFlJxZp60IhTPnsOetwYVZ6XBZmcd4wQbPwyc7hgIYg0M") + "keys" => array("key" => "eOMvWh3ODqk4A1MHEeolm0Cfv", "secret" => "aJSrRzFlJxZp60IhTPnsOetwYVZ6XBZmcd4wQbPwyc7hgIYg0M"), + "provider" => 'Application\Auth\Twitter' ) ), diff --git a/application/models/Auth/AbstractAuth.php b/application/models/Auth/AbstractAuth.php index c6f23600..4648d2cf 100644 --- a/application/models/Auth/AbstractAuth.php +++ b/application/models/Auth/AbstractAuth.php @@ -2,38 +2,25 @@ namespace Application\Auth; +use Bluz\Proxy\Config; use Bluz\Proxy\Messages; use Application\Auth; use Application\Users; -use Bluz\Proxy\Request; -use Bluz\Proxy\Session; -class AbstractAuth implements AuthInterface +abstract class AbstractAuth implements AuthInterface { - /** @var \Bluz\Http\Response */ protected $response; - /** @var \Application\Users\Entity\User $identity */ + /** @var \Application\Users\Row $identity */ protected $identity; - private $hybridauth; + /** @var \Hybrid_Auth $hybridauth */ + protected $hybridauth; - /** - * @return \Hybrid_Auth - */ - public function getHybridauth() - { - return $this->hybridauth = new \Hybrid_Auth($this->getOptions()); - } + /** @var \Hybrid_Provider_Adapter $authAdapter*/ + protected $authAdapter; - /** - * @param mixed $hybridauth - */ - public function setHybridauth($hybridauth) - { - $this->hybridauth = $hybridauth; - } /** * @param \Bluz\Http\Response $response @@ -52,23 +39,7 @@ public function getResponse() } /** - * @param \Application\AbstractService $service - */ - public function setService($service) - { - $this->service = $service; - } - - /** - * @return \Application\AbstractService - */ - public function getService() - { - return $this->service; - } - - /** - * @param \Application\Users\Entity\User $identity + * @param \\Application\Users\Row $user $identity */ public function setIdentity($identity) { @@ -76,7 +47,7 @@ public function setIdentity($identity) } /** - * @return \Application\Users\Entity\User + * @return \Application\Users\Row $user */ public function getIdentity() { @@ -84,41 +55,8 @@ public function getIdentity() } /** - * @param \Application\Auth\AuthService $authService - */ - public function setAuthService($authService) - { - $this->authService = $authService; - } - - /** - * @return \Application\Auth\AuthService - */ - public function getAuthService() - { - return $this->authService; - } - - /** - * @param \Application\Users\UserService $userService - */ - public function setUserService($userService) - { - $this->userService = $userService; - } - - /** - * @return \Application\Users\UserService - */ - public function getUserService() - { - return $this->userService; - } - - - /** - * @param array $data - * @param \Application\Users\Entity\User $user + * @param \Hybrid_User_Profile $data + * @param \Application\Users\Row $user $user * @return void */ public function registration($data, $user) @@ -131,83 +69,49 @@ public function registration($data, $user) */ public function authProcess() { - $elements = explode('\\', get_class($this)); - $providerName = end($elements); - $profile = $this->getProfile(); - - if (!$profile) { - /** - * If user doesn't allow application yet, redirect him to fb page for this. - * After this operation we will returned to this file. - * Is user declined app, we get param 'error' => 'access_denied' - */ - - // if user declined - if ('access_denied' == Request::getParam('error', null)) { - $this->response->redirectTo('users', 'signin'); - } - } - - /* if ( \Bluz\Proxy\Auth::getIdentity()) { - $this->response->redirectTo('index', 'index'); - }*/ + $providerName = $this->getProviderName(); + $profile = $this->getProfile(); //? /** * @var Auth\Table $authTable */ $authTable = Auth\Table::getInstance(); - $auth = $authTable->getAuthRow(strtolower($providerName), $profile['id']); - - if ($auth) { - // if user has been registered - $user = Users\Table::findRow($auth->userId); - - $user->login(); - - if ($user->status != Users\Table::STATUS_ACTIVE) { - Messages::addError('User is not active'); + $auth = $authTable->getAuthRow(strtolower($providerName), $profile->identifier); + + if ($this->identity) { + if ($auth) { + Messages::addNotice(sprintf('You have already linked to %s', $providerName)); + $this->response->redirectTo('users', 'profile', ['id' => $this->identity->id]); + } else { + $user = Users\Table::findRow($this->identity->id); + $this->registration($profile, $user); } + } - $this->response->redirectTo('index', 'index'); + if ($auth) { + $this->alreadyRegisteredLogic($auth); } else { - // sign up user - - // continue with registration - /** - * Write facebook response to session - */ - Session::set('facebook', $profile); - Messages::addNotice('To finish your registration fill the form'); - $this->redirectTo('users', 'signup'); - - + Messages::addError('You need to sign in first'); + $this->response->redirectTo('users', 'signin'); } - /* if ($this->identity) { - if ($auth) { - Messages::addNotice(sprintf('You have already linked to %s', $providerName)); - $this->response->redirectTo('users', 'profile', ['id' => $this->identity->getId()]); - } else { - $user = $this->userService->readOne($this->identity->getId()); - $this->registration($profile, $user); - } - } - - if ($auth) { - $this->alreadyRegisteredLogic($auth); //need to be not a proxy object - } else { - Messages::addError('You need to sign up via Ldap first'); - $this->response->redirectTo('users', 'signin'); - }*/ + } + /** + * @return string + */ + private function getProviderName(){ + + $elements = explode('\\', get_class($this)); + return end($elements); } /** * @return array * @throws \Application\Exception */ - private function getOptions() + public function getOptions() { - + return Config::getData('hybridauth'); } /** @@ -222,17 +126,22 @@ public function redirectLogic() * @param Auth $auth * @return mixed */ - public function alreadyRegisteredLogic(Auth $auth) + public function alreadyRegisteredLogic($auth) { // TODO: Implement alreadyRegisteredLogic() method. } /** - * @return array + * @return \Hybrid_User_Profile */ public function getProfile() { - // TODO: Implement getProfile() method. + $this->hybridauth = new \Hybrid_Auth($this->getOptions()); + + /** @var \Hybrid_Provider_Adapter $authProvider */ + $this->authAdapter= $this->hybridauth->authenticate($this->getProviderName()); + + return $this->authAdapter->getUserProfile(); } public function setProvider($provider) diff --git a/application/models/Auth/AuthFactory.php b/application/models/Auth/AuthFactory.php index be03ec3a..ab10f98d 100644 --- a/application/models/Auth/AuthFactory.php +++ b/application/models/Auth/AuthFactory.php @@ -24,11 +24,13 @@ class AuthFactory extends AbstractAuth */ public function setProvider($provider){ - $options = Config::getData('hybridauth'); - if(empty($options['provider'])){ + $options = Config::getData('hybridauth','providers'); + $provider = $options[ucfirst($provider)]['provider']; + + if(empty($provider)){ throw new Exception('No provider was found !'); } - $className = $options['provider']; + $className = $provider; if (!class_exists($className)) { throw new Exception(sprintf('Class with name %s not found !', $className)); } diff --git a/application/models/Auth/AuthInterface.php b/application/models/Auth/AuthInterface.php index 40de5409..1a1b59a4 100644 --- a/application/models/Auth/AuthInterface.php +++ b/application/models/Auth/AuthInterface.php @@ -5,8 +5,8 @@ interface AuthInterface{ /** - * @param array $data - * @param \Application\Users\Entity\User $user + * @param \Hybrid_User_Profile $data + * @param \Application\Users\Row $user * @return void */ public function registration($data, $user); @@ -28,10 +28,10 @@ public function getOptions(); public function redirectLogic(); /** - * @param Auth $auth + * @param \Application\Auth\Row $auth * @return mixed */ - public function alreadyRegisteredLogic(Auth $auth); + public function alreadyRegisteredLogic($auth); /** * @return array diff --git a/application/models/Auth/Twitter.php b/application/models/Auth/Twitter.php new file mode 100644 index 00000000..84ad5fd6 --- /dev/null +++ b/application/models/Auth/Twitter.php @@ -0,0 +1,54 @@ +userId = $user->id; + $twitterRow->provider = Auth\Table::PROVIDER_TWITTER; + + $twitterRow->foreignKey = $profile->identifier; + $twitterRow->token = $this->authAdapter->getAccessToken()['access_token']; + $twitterRow->tokenSecret = $this->authAdapter->getAccessToken()['access_token_secret']; + $twitterRow->tokenType = Auth\Table::TYPE_ACCESS; + $twitterRow->save(); + + Messages::addNotice('Your account was linked to Twitter successfully !'); + $this->response->redirectTo('users', 'profile', ['id' => $user->id]); + } + + /** + * @param \Application\Auth\Row $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'); + } + +} diff --git a/application/modules/auth/controllers/auth.php b/application/modules/auth/controllers/auth.php index db7a7258..9cb2ac78 100644 --- a/application/modules/auth/controllers/auth.php +++ b/application/modules/auth/controllers/auth.php @@ -37,10 +37,10 @@ function () { return function () { return false; - }; + }; - // $config = Config::getData('hybridauth'); + /* $config = Config::getData('hybridauth'); try{ $hybridauth = new \Hybrid_Auth( $config ); @@ -55,6 +55,6 @@ function () { } catch( Exception $e ){ echo "Ooophs, we got an error: " . $e->getMessage(); - } + }*/ }; diff --git a/application/modules/users/views/profile.phtml b/application/modules/users/views/profile.phtml index 68ad32e6..6700fb7b 100644 --- a/application/modules/users/views/profile.phtml +++ b/application/modules/users/views/profile.phtml @@ -4,19 +4,51 @@ * @var \Bluz\View\View $this */ ?> + + + -
-
-
login?>
-
-
email?>
-
-
status?>
-
+ +
+
+
+ + avatar + +
+
+ + + + + + + + + + + + + + + +
email ?>
status ?>
+ + + +
+
+
+
diff --git a/application/modules/users/views/signin.phtml b/application/modules/users/views/signin.phtml index 08363085..d3b8fc7c 100644 --- a/application/modules/users/views/signin.phtml +++ b/application/modules/users/views/signin.phtml @@ -4,6 +4,11 @@ * @var \Bluz\View\View $this */ ?> + + +

From fc2566a4c18dd6599df9d3f9bd2aa24cf912139b Mon Sep 17 00:00:00 2001 From: yuklia Date: Wed, 6 May 2015 13:37:24 +0300 Subject: [PATCH 08/46] hotfix --- application/modules/auth/controllers/auth.php | 18 ------------------ composer.json | 1 - 2 files changed, 19 deletions(-) diff --git a/application/modules/auth/controllers/auth.php b/application/modules/auth/controllers/auth.php index 9cb2ac78..6b03dce6 100644 --- a/application/modules/auth/controllers/auth.php +++ b/application/modules/auth/controllers/auth.php @@ -39,22 +39,4 @@ function () { return false; }; - - /* $config = Config::getData('hybridauth'); - - try{ - $hybridauth = new \Hybrid_Auth( $config ); - - $twitter = $hybridauth->authenticate( "Facebook" ); - - if ($twitter->isUserConnected()) { - $user_profile = $twitter->getUserProfile(); - echo "Hi there! " . $user_profile->displayName; - $twitter->logout(); - } - } - catch( Exception $e ){ - echo "Ooophs, we got an error: " . $e->getMessage(); - }*/ - }; diff --git a/composer.json b/composer.json index e60834e1..1a7bc495 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,6 @@ "squizlabs/php_codesniffer": "~2.0", "zendframework/zend-dom": "~2.3", "hybridauth/hybridauth": "dev-master", - "opauth/opauth": "*", "opauth/facebook": "*", "opauth/twitter": "*" }, From 6b18359f807ee12991c12839b0d0d851d9c0f2b3 Mon Sep 17 00:00:00 2001 From: yuklia Date: Wed, 6 May 2015 15:48:16 +0300 Subject: [PATCH 09/46] facebook/google --- application/configs/default/hybridauth.php | 6 +- application/models/Auth/AbstractAuth.php | 15 +- application/models/Auth/AuthInterface.php | 5 - application/models/Auth/Facebook.php | 89 ++++------- application/models/Auth/Google.php | 143 +++--------------- .../modules/auth/controllers/endpoint.php | 2 - .../modules/index/controllers/index.php | 18 +++ public/js/bluz.auth.js | 1 + 8 files changed, 81 insertions(+), 198 deletions(-) diff --git a/application/configs/default/hybridauth.php b/application/configs/default/hybridauth.php index a4e151bf..7445043b 100644 --- a/application/configs/default/hybridauth.php +++ b/application/configs/default/hybridauth.php @@ -7,6 +7,7 @@ */ /** + * @link http://hybridauth.sourceforge.net/userguide/Configuration.html * You must define provider class inside providers scope */ return array( @@ -17,7 +18,8 @@ // google "Google" => array( // 'id' is your google client id "enabled" => true, - "keys" => array("id" => "", "secret" => ""), + "keys" => array("id" => "422236904670-r9jmuh7q4kc2vqpkscqtv9i1898mer4u.apps.googleusercontent.com", + "secret" => "3krFP90YB6F6xIADFG-Nzr1Q"), "provider" => 'Application\Auth\Google' ), @@ -25,7 +27,7 @@ "Facebook" => array( // 'id' is your facebook application id "enabled" => true, "keys" => array("id" => "1413483462306154", "secret" => "0911925061bdb04a3d8c41129f672065"), - "scope" => "email, user_about_me, user_birthday, user_hometown", // optional + "scope" => "email, user_about_me, user_birthday, user_hometown, publish_actions", // optional "provider" => 'Application\Auth\Facebook' ), diff --git a/application/models/Auth/AbstractAuth.php b/application/models/Auth/AbstractAuth.php index 4648d2cf..98a64a7a 100644 --- a/application/models/Auth/AbstractAuth.php +++ b/application/models/Auth/AbstractAuth.php @@ -64,11 +64,16 @@ public function registration($data, $user) // TODO: Implement registration() method. } + /** * @return void */ public function authProcess() { + if($this->hybridauth){ + $this->hybridauth->logoutAllProviders(); + } + $providerName = $this->getProviderName(); $profile = $this->getProfile(); //? @@ -114,14 +119,6 @@ public function getOptions() return Config::getData('hybridauth'); } - /** - * @return void - */ - public function redirectLogic() - { - // TODO: Implement redirectLogic() method. - } - /** * @param Auth $auth * @return mixed @@ -141,7 +138,7 @@ public function getProfile() /** @var \Hybrid_Provider_Adapter $authProvider */ $this->authAdapter= $this->hybridauth->authenticate($this->getProviderName()); - return $this->authAdapter->getUserProfile(); + return $this->authAdapter->getUserProfile(); } public function setProvider($provider) diff --git a/application/models/Auth/AuthInterface.php b/application/models/Auth/AuthInterface.php index 1a1b59a4..5e26ba89 100644 --- a/application/models/Auth/AuthInterface.php +++ b/application/models/Auth/AuthInterface.php @@ -22,11 +22,6 @@ public function authProcess(); */ public function getOptions(); - /** - * @return void - */ - public function redirectLogic(); - /** * @param \Application\Auth\Row $auth * @return mixed diff --git a/application/models/Auth/Facebook.php b/application/models/Auth/Facebook.php index 22c1cd81..9c10220f 100644 --- a/application/models/Auth/Facebook.php +++ b/application/models/Auth/Facebook.php @@ -1,89 +1,54 @@ setForeignKey($profile['id']); - $auth->setProvider(Auth::PROVIDER_FACEBOOK); - $auth->setToken($this->facebook->getAccessToken()); - $auth->setTokenType(AuthTable::TYPE_ACCESS); - $auth->setUser($user); - $auth->setTokenSecret(0); - $this->authService->saveObject($auth); - Messages::addNotice('Your account was linked to Facebook successfully !'); - $this->response->redirectTo('users', 'profile', ['id' => $user->getId()]); - - } - - /** - * @return array - */ - public function getProfile() - { - /** @var \Hybrid_Auth $hybridauth */ - $hybridauth = $this->getHybridauth(); + $twitterRow = new Auth\Row(); + $twitterRow->userId = $user->id; + $twitterRow->provider = Auth\Table::PROVIDER_FACEBOOK; - $facebook = $hybridauth->authenticate( "Facebook" ); + $twitterRow->foreignKey = $profile->identifier; + $twitterRow->token = $this->authAdapter->getAccessToken()['access_token']; + $twitterRow->tokenSecret = ($this->authAdapter->getAccessToken()['access_token_secret'])? $this->authAdapter->getAccessToken()['access_token_secret']: '' ; + $twitterRow->tokenType = Auth\Table::TYPE_ACCESS; + $twitterRow->save(); - return $facebook->getUserProfile(); + Messages::addNotice('Your account was linked to Facebook successfully !'); + $this->response->redirectTo('users', 'profile', ['id' => $user->id]); } /** - * void + * @param \Application\Auth\Row $auth + * @return mixed */ - public function redirectLogic() - { - $facebookConf = Config::getData('auth', 'facebook'); - //todo::need to be wrapped in plugin - $scheme = Request::getScheme() . '://'; - $host = Request::getHttpHost(); - $url = $facebookConf['redirect-uri']; - - // if user declined - if ('access_denied' == Request::getParam('error', null)) { - $this->response->redirectTo('users', 'profile', ['id' => $this->identity->getId()]); - } - $login_url = $this->facebook->getLoginUrl(array( - 'scope' => 'email', - 'redirect_uri' => $scheme . $host . '/' . $url)); - $this->response->redirect($login_url); - } + public function alreadyRegisteredLogic($auth){ - /** - * @param Auth $auth - * @throws \Bluz\Auth\AuthException - * @throws \Exception - */ - public function alreadyRegisteredLogic(Auth $auth) - { - $auth->setToken($this->facebook->getAccessToken()); - $this->authService->updateObject($auth); - $user = $auth->getUser(); + $user = Users\Table::findRow($auth->userId); - if ($user->getStatus() != User::STATUS_ACTIVE) { + if ($user->status != Users\Table::STATUS_ACTIVE) { Messages::addError('User is not active'); } - $this->userService->login($user); + $user->login(); $this->response->redirectTo('index', 'index'); } -} \ No newline at end of file +} diff --git a/application/models/Auth/Google.php b/application/models/Auth/Google.php index 69060aa9..d84b6392 100644 --- a/application/models/Auth/Google.php +++ b/application/models/Auth/Google.php @@ -2,146 +2,53 @@ /** * Created by PhpStorm. * User: yuklia - * Date: 2/17/15 - * Time: 11:06 AM + * Date: 06.05.15 + * Time: 11:02 */ namespace Application\Auth; -use Bluz\Proxy\Config; +use Application\Auth; +use Application\Users; use Bluz\Proxy\Messages; -use Bluz\Proxy\Request; -use Bluz\Proxy\Router; -use Google\Client; -class Google extends AbstractAuth{ - - /** @var Client/Google */ - private $google; - - /** @var */ - private $code; - - - /** - * @param mixed $code - */ - public function setCode($code) - { - if (!$code) { - $config = Config::getData('auth', 'google'); - $googleAuth = new Client($config); - //todo::need to be wrapped in plugin - $scheme = Request::getScheme() . '://'; - $host = Request::getHttpHost(); - $url = $config['redirect-uri']; - $redirectUri = $scheme . $host . '/' . $url; - $this->response->redirect($googleAuth->getAuthUrl($redirectUri)); - } - - $this->code = $code; - } +class Google extends AbstractAuth +{ /** - * @return mixed - */ - public function getCode() - { - return $this->code; - } - - /** - * @param $profile - * @param $user + * @param \Hybrid_User_Profile $profile + * @param \Application\Users\Row $user */ public function registration($profile, $user) { - $auth = new \Application\Auth\Entity\Auth(); - $auth->setForeignKey($profile['id']); - $auth->setProvider(\Application\Auth\Entity\Auth::PROVIDER_GOOGLE); - $auth->setToken($this->google->accessToken); - if($this->google->refreshToken){ - $auth->setRefreshToken($this->google->refreshToken); - } - $auth->setTokenType('access'); - $auth->setUser($user); - $auth->setTokenSecret(0); - $this->authService->saveObject($auth); - Messages::addNotice('Your account was linked to Google successfully !'); - $this->response->redirectTo('users', 'profile', ['id' => $user->getId()]); - - } + $twitterRow = new Auth\Row(); + $twitterRow->userId = $user->id; + $twitterRow->provider = Auth\Table::PROVIDER_GOOGLE; + $twitterRow->foreignKey = $profile->identifier; + $twitterRow->token = $this->authAdapter->getAccessToken()['access_token']; + $twitterRow->tokenSecret = ($this->authAdapter->getAccessToken()['access_token_secret'])? $this->authAdapter->getAccessToken()['access_token_secret']: '' ; + $twitterRow->tokenType = Auth\Table::TYPE_ACCESS; + $twitterRow->save(); - /** - * @return array|mixed - * @throws \Exception - */ - public function getOptions() - { - $options = Config::getData('auth', 'google'); - if (!$options || !isset($options['client_id'], $options['client_secret']) - || empty($options['client_id']) || empty($options['client_secret']) - ) { - throw new \Exception('Google authorization is not configured'); - } - return $options; + Messages::addNotice('Your account was linked to Google successfully !'); + $this->response->redirectTo('users', 'profile', ['id' => $user->id]); } /** - * @return void + * @param \Application\Auth\Row $auth + * @return mixed */ - public function redirectLogic() - { - $login_url = $this->google->getAuthUrl(Router::getFullUrl('google', 'redirect_uri')); - $this->response->redirect($login_url); - } + public function alreadyRegisteredLogic($auth){ - /** - * @param Auth $auth - * @throws \Bluz\Auth\AuthException - * @throws \Exception - */ - public function alreadyRegisteredLogic(Auth $auth) - { - $user = $auth->getUser(); + $user = Users\Table::findRow($auth->userId); - if ($user->getStatus() != User::STATUS_ACTIVE) { + if ($user->status != Users\Table::STATUS_ACTIVE) { Messages::addError('User is not active'); } - $this->userService->login($user); + $user->login(); $this->response->redirectTo('index', 'index'); - - } - - /** - * @return array - */ - public function getProfile() - { - //extend access_token live or get new one - $options = $this->getOptions(); - $this->google = new Client($options); - $config = Config::getData('auth', 'google'); - //todo::need to be wrapped in plugin - $scheme = Request::getScheme() . '://'; - $host = Request::getHttpHost(); - $url = $config['redirect-uri']; - $redirectUri = $scheme . $host . '/' . $url; - $this->google->getOauthAccessToken($this->code, $redirectUri); //getting temporary token - $userGoogle = $this->google->getUserInfo(); - if ($userGoogle) { - return $userGoogle; - } - /** - * If user doesn't allow application yet, redirect him to fb page for this. - * After this operation we will returned to this file. - * Is user declined app, we get param 'error' => 'access_denied' - */ - $this->redirectLogic(); - } - -} \ No newline at end of file +} diff --git a/application/modules/auth/controllers/endpoint.php b/application/modules/auth/controllers/endpoint.php index 4d13db92..76abb158 100644 --- a/application/modules/auth/controllers/endpoint.php +++ b/application/modules/auth/controllers/endpoint.php @@ -12,8 +12,6 @@ return /** - * @param int $id User UID - * @param string $code * @return \closure */ function () { diff --git a/application/modules/index/controllers/index.php b/application/modules/index/controllers/index.php index 611edf8d..129ad090 100644 --- a/application/modules/index/controllers/index.php +++ b/application/modules/index/controllers/index.php @@ -21,4 +21,22 @@ */ function () { + /* try{ + $hybridauth = new \Hybrid_Auth( Config::getData('hybridauth') ); + + $twitter = $hybridauth->authenticate( "Facebook" ); + + $user_profile = $twitter->getUserProfile(); + + echo "Hi there! " . $user_profile->displayName; + + $twitter->setUserStatus( "Hello world!" ); + + $user_contacts = $twitter->getUserContacts(); + $hybridauth->logoutAllProviders(); + } + catch( Exception $e ){ + echo "Ooophs, we got an error: " . $e->getMessage(); + }*/ + }; diff --git a/public/js/bluz.auth.js b/public/js/bluz.auth.js index 79b45a6b..381e5b31 100644 --- a/public/js/bluz.auth.js +++ b/public/js/bluz.auth.js @@ -2,6 +2,7 @@ * Created by yuklia on 04.03.15. */ define(['jquery'], function ($) { + console.log('test'); "use strict"; $(function(){ $('.provider').on('click', function(event){ From 49d9069cee0db3d7e5df4b0b121b5a49fcd943f9 Mon Sep 17 00:00:00 2001 From: yuklia Date: Wed, 6 May 2015 16:03:23 +0300 Subject: [PATCH 10/46] config modified --- application/configs/default/hybridauth.php | 3 +++ application/models/Auth/AbstractAuth.php | 4 ---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/application/configs/default/hybridauth.php b/application/configs/default/hybridauth.php index 7445043b..743acdef 100644 --- a/application/configs/default/hybridauth.php +++ b/application/configs/default/hybridauth.php @@ -18,6 +18,7 @@ // google "Google" => array( // 'id' is your google client id "enabled" => true, + "wrapper" => array( "path" => "Providers/Google.php", "class" => "Hybrid_Providers_Google" ), "keys" => array("id" => "422236904670-r9jmuh7q4kc2vqpkscqtv9i1898mer4u.apps.googleusercontent.com", "secret" => "3krFP90YB6F6xIADFG-Nzr1Q"), "provider" => 'Application\Auth\Google' @@ -26,6 +27,7 @@ // facebook "Facebook" => array( // 'id' is your facebook application id "enabled" => true, + "wrapper" => array( "path" => "Providers/Facebook.php", "class" => "Hybrid_Providers_Facebook" ), "keys" => array("id" => "1413483462306154", "secret" => "0911925061bdb04a3d8c41129f672065"), "scope" => "email, user_about_me, user_birthday, user_hometown, publish_actions", // optional "provider" => 'Application\Auth\Facebook' @@ -34,6 +36,7 @@ // twitter "Twitter" => array( // 'key' is your twitter application consumer key "enabled" => true, + "wrapper" => array( "path" => "Providers/Twitter.php", "class" => "Hybrid_Providers_Twitter" ), "keys" => array("key" => "eOMvWh3ODqk4A1MHEeolm0Cfv", "secret" => "aJSrRzFlJxZp60IhTPnsOetwYVZ6XBZmcd4wQbPwyc7hgIYg0M"), "provider" => 'Application\Auth\Twitter' ) diff --git a/application/models/Auth/AbstractAuth.php b/application/models/Auth/AbstractAuth.php index 98a64a7a..fd574a20 100644 --- a/application/models/Auth/AbstractAuth.php +++ b/application/models/Auth/AbstractAuth.php @@ -70,10 +70,6 @@ public function registration($data, $user) */ public function authProcess() { - if($this->hybridauth){ - $this->hybridauth->logoutAllProviders(); - } - $providerName = $this->getProviderName(); $profile = $this->getProfile(); //? From 38056ee527b4c2c5bb478cf9ce972fe8228b6001 Mon Sep 17 00:00:00 2001 From: yuklia Date: Wed, 6 May 2015 16:18:00 +0300 Subject: [PATCH 11/46] change prov --- application/models/Auth/AuthFactory.php | 4 +- .../{AbstractAuth.php => AuthProvider.php} | 43 ++++++++++++++----- application/modules/auth/controllers/auth.php | 3 +- 3 files changed, 37 insertions(+), 13 deletions(-) rename application/models/Auth/{AbstractAuth.php => AuthProvider.php} (68%) diff --git a/application/models/Auth/AuthFactory.php b/application/models/Auth/AuthFactory.php index ab10f98d..f7bf1eda 100644 --- a/application/models/Auth/AuthFactory.php +++ b/application/models/Auth/AuthFactory.php @@ -24,7 +24,7 @@ class AuthFactory extends AbstractAuth */ public function setProvider($provider){ - $options = Config::getData('hybridauth','providers'); + /* $options = Config::getData('hybridauth','providers'); $provider = $options[ucfirst($provider)]['provider']; if(empty($provider)){ @@ -34,7 +34,7 @@ public function setProvider($provider){ if (!class_exists($className)) { throw new Exception(sprintf('Class with name %s not found !', $className)); } - $this->authType = new $className(); + $this->authType = new $className();*/ } diff --git a/application/models/Auth/AbstractAuth.php b/application/models/Auth/AuthProvider.php similarity index 68% rename from application/models/Auth/AbstractAuth.php rename to application/models/Auth/AuthProvider.php index fd574a20..75a49dc3 100644 --- a/application/models/Auth/AbstractAuth.php +++ b/application/models/Auth/AuthProvider.php @@ -7,7 +7,7 @@ use Application\Auth; use Application\Users; -abstract class AbstractAuth implements AuthInterface +class AuthProvider implements AuthInterface { /** @var \Bluz\Http\Response */ protected $response; @@ -21,7 +21,12 @@ abstract class AbstractAuth implements AuthInterface /** @var \Hybrid_Provider_Adapter $authAdapter*/ protected $authAdapter; + protected $providerName; + public function __construct($providerName){ + + $this->providerName = $providerName; + } /** * @param \Bluz\Http\Response $response */ @@ -61,7 +66,18 @@ public function getIdentity() */ public function registration($data, $user) { - // TODO: Implement registration() method. + $twitterRow = new Auth\Row(); + $twitterRow->userId = $user->id; + $twitterRow->provider = strtolower($this->providerName); + + $twitterRow->foreignKey = $data->identifier; + $twitterRow->token = $this->authAdapter->getAccessToken()['access_token']; + $twitterRow->tokenSecret = ($this->authAdapter->getAccessToken()['access_token_secret'])? $this->authAdapter->getAccessToken()['access_token_secret']: '' ; + $twitterRow->tokenType = Auth\Table::TYPE_ACCESS; + $twitterRow->save(); + + Messages::addNotice('Your account was linked to Facebook successfully !'); + $this->response->redirectTo('users', 'profile', ['id' => $user->id]); } @@ -70,18 +86,18 @@ public function registration($data, $user) */ public function authProcess() { - $providerName = $this->getProviderName(); + //$providerName = $this->getProviderName(); $profile = $this->getProfile(); //? /** * @var Auth\Table $authTable */ $authTable = Auth\Table::getInstance(); - $auth = $authTable->getAuthRow(strtolower($providerName), $profile->identifier); + $auth = $authTable->getAuthRow(strtolower($this->providerName), $profile->identifier); if ($this->identity) { if ($auth) { - Messages::addNotice(sprintf('You have already linked to %s', $providerName)); + Messages::addNotice(sprintf('You have already linked to %s', ucfirst($this->providerName))); $this->response->redirectTo('users', 'profile', ['id' => $this->identity->id]); } else { $user = Users\Table::findRow($this->identity->id); @@ -100,11 +116,11 @@ public function authProcess() /** * @return string */ - private function getProviderName(){ + /* private function getProviderName(){ $elements = explode('\\', get_class($this)); return end($elements); - } + }*/ /** * @return array @@ -116,12 +132,19 @@ public function getOptions() } /** - * @param Auth $auth + * @param $auth * @return mixed */ public function alreadyRegisteredLogic($auth) { - // TODO: Implement alreadyRegisteredLogic() method. + $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'); } /** @@ -132,7 +155,7 @@ public function getProfile() $this->hybridauth = new \Hybrid_Auth($this->getOptions()); /** @var \Hybrid_Provider_Adapter $authProvider */ - $this->authAdapter= $this->hybridauth->authenticate($this->getProviderName()); + $this->authAdapter= $this->hybridauth->authenticate($this->providerName); return $this->authAdapter->getUserProfile(); } diff --git a/application/modules/auth/controllers/auth.php b/application/modules/auth/controllers/auth.php index 6b03dce6..8fb29897 100644 --- a/application/modules/auth/controllers/auth.php +++ b/application/modules/auth/controllers/auth.php @@ -9,6 +9,7 @@ namespace Application; use Application\Auth\AuthFactory; +use Application\Auth\AuthProvider; use Application\Users; use Bluz\Proxy\Messages; use Bluz\Proxy\Request; @@ -26,7 +27,7 @@ function () { */ try{ $provider = Request::getParam('provider'); - $auth = new AuthFactory(); + $auth = new AuthProvider($provider); $auth->setProvider($provider); $auth->setResponse($this); $auth->setIdentity($this->user()); From 74e47a79b7a2156b0c3cb90d01efd27f2c04de37 Mon Sep 17 00:00:00 2001 From: yuklia Date: Wed, 6 May 2015 17:24:46 +0300 Subject: [PATCH 12/46] auth providers --- application/models/Auth/AuthInterface.php | 47 ----------------------- 1 file changed, 47 deletions(-) delete mode 100644 application/models/Auth/AuthInterface.php diff --git a/application/models/Auth/AuthInterface.php b/application/models/Auth/AuthInterface.php deleted file mode 100644 index 5e26ba89..00000000 --- a/application/models/Auth/AuthInterface.php +++ /dev/null @@ -1,47 +0,0 @@ - Date: Wed, 6 May 2015 17:25:08 +0300 Subject: [PATCH 13/46] auth providers --- application/configs/default/hybridauth.php | 4 +-- application/models/Auth/AuthProvider.php | 36 +++++-------------- application/modules/auth/controllers/auth.php | 2 -- .../modules/index/controllers/index.php | 17 --------- .../modules/users/controllers/profile.php | 1 + application/modules/users/views/profile.phtml | 8 ++--- application/modules/users/views/signin.phtml | 11 ++---- 7 files changed, 19 insertions(+), 60 deletions(-) diff --git a/application/configs/default/hybridauth.php b/application/configs/default/hybridauth.php index 743acdef..21dc3859 100644 --- a/application/configs/default/hybridauth.php +++ b/application/configs/default/hybridauth.php @@ -42,8 +42,8 @@ ) ), - "debug_mode" => false, + "debug_mode" => true, // to enable logging, set 'debug_mode' to true, then provide here a path of a writable file - "debug_file" => "", + "debug_file" => PATH_ROOT.'/data/logs/hybrid.log' ); \ No newline at end of file diff --git a/application/models/Auth/AuthProvider.php b/application/models/Auth/AuthProvider.php index 75a49dc3..8d143db2 100644 --- a/application/models/Auth/AuthProvider.php +++ b/application/models/Auth/AuthProvider.php @@ -7,7 +7,7 @@ use Application\Auth; use Application\Users; -class AuthProvider implements AuthInterface +class AuthProvider { /** @var \Bluz\Http\Response */ protected $response; @@ -21,6 +21,9 @@ class AuthProvider implements AuthInterface /** @var \Hybrid_Provider_Adapter $authAdapter*/ protected $authAdapter; + /** + * @var string + */ protected $providerName; public function __construct($providerName){ @@ -72,11 +75,12 @@ public function registration($data, $user) $twitterRow->foreignKey = $data->identifier; $twitterRow->token = $this->authAdapter->getAccessToken()['access_token']; - $twitterRow->tokenSecret = ($this->authAdapter->getAccessToken()['access_token_secret'])? $this->authAdapter->getAccessToken()['access_token_secret']: '' ; + $twitterRow->tokenSecret = ($this->authAdapter->getAccessToken()['access_token_secret'])? + $this->authAdapter->getAccessToken()['access_token_secret']: '' ; $twitterRow->tokenType = Auth\Table::TYPE_ACCESS; $twitterRow->save(); - Messages::addNotice('Your account was linked to Facebook successfully !'); + Messages::addNotice(sprintf('Your account was linked to %s successfully !', ucfirst($this->providerName))); $this->response->redirectTo('users', 'profile', ['id' => $user->id]); } @@ -86,8 +90,7 @@ public function registration($data, $user) */ public function authProcess() { - //$providerName = $this->getProviderName(); - $profile = $this->getProfile(); //? + $profile = $this->getProfile(); /** * @var Auth\Table $authTable @@ -113,15 +116,6 @@ public function authProcess() } } - /** - * @return string - */ - /* private function getProviderName(){ - - $elements = explode('\\', get_class($this)); - return end($elements); - }*/ - /** * @return array * @throws \Application\Exception @@ -155,21 +149,9 @@ public function getProfile() $this->hybridauth = new \Hybrid_Auth($this->getOptions()); /** @var \Hybrid_Provider_Adapter $authProvider */ - $this->authAdapter= $this->hybridauth->authenticate($this->providerName); + $this->authAdapter= $this->hybridauth->authenticate(ucfirst($this->providerName)); return $this->authAdapter->getUserProfile(); } - public function setProvider($provider) - { - // TODO: Implement setProvider() method. - } - - /** - * @return mixed - */ - public function getProvider() - { - // TODO: Implement getProvider() method. - } } \ No newline at end of file diff --git a/application/modules/auth/controllers/auth.php b/application/modules/auth/controllers/auth.php index 8fb29897..95b20c3e 100644 --- a/application/modules/auth/controllers/auth.php +++ b/application/modules/auth/controllers/auth.php @@ -8,7 +8,6 @@ namespace Application; -use Application\Auth\AuthFactory; use Application\Auth\AuthProvider; use Application\Users; use Bluz\Proxy\Messages; @@ -28,7 +27,6 @@ function () { try{ $provider = Request::getParam('provider'); $auth = new AuthProvider($provider); - $auth->setProvider($provider); $auth->setResponse($this); $auth->setIdentity($this->user()); $auth->authProcess(); diff --git a/application/modules/index/controllers/index.php b/application/modules/index/controllers/index.php index 129ad090..edfe2385 100644 --- a/application/modules/index/controllers/index.php +++ b/application/modules/index/controllers/index.php @@ -21,22 +21,5 @@ */ function () { - /* try{ - $hybridauth = new \Hybrid_Auth( Config::getData('hybridauth') ); - - $twitter = $hybridauth->authenticate( "Facebook" ); - - $user_profile = $twitter->getUserProfile(); - - echo "Hi there! " . $user_profile->displayName; - - $twitter->setUserStatus( "Hello world!" ); - - $user_contacts = $twitter->getUserContacts(); - $hybridauth->logoutAllProviders(); - } - catch( Exception $e ){ - echo "Ooophs, we got an error: " . $e->getMessage(); - }*/ }; diff --git a/application/modules/users/controllers/profile.php b/application/modules/users/controllers/profile.php index c4cad2c7..0ae55ac9 100644 --- a/application/modules/users/controllers/profile.php +++ b/application/modules/users/controllers/profile.php @@ -8,6 +8,7 @@ namespace Application; use Application\Users; +use Bluz\Proxy\Config; use Bluz\Proxy\Layout; return diff --git a/application/modules/users/views/profile.phtml b/application/modules/users/views/profile.phtml index 6700fb7b..0e8bf57d 100644 --- a/application/modules/users/views/profile.phtml +++ b/application/modules/users/views/profile.phtml @@ -6,7 +6,7 @@ ?>