From 5edf99ee2212ed1700d794ed19d782a28c5e698e Mon Sep 17 00:00:00 2001 From: Ceeram Date: Tue, 19 Feb 2013 12:36:53 +0100 Subject: [PATCH 01/21] initial refactor for 1.0 --- FacebookStrategy.php | 146 ------------------------------------------- Strategy.php | 134 +++++++++++++++++++++++++++++++++++++++ composer.json | 11 ++-- 3 files changed, 139 insertions(+), 152 deletions(-) delete mode 100644 FacebookStrategy.php create mode 100644 Strategy.php diff --git a/FacebookStrategy.php b/FacebookStrategy.php deleted file mode 100644 index d25605c..0000000 --- a/FacebookStrategy.php +++ /dev/null @@ -1,146 +0,0 @@ - 'email'); - */ - public $defaults = array( - 'redirect_uri' => '{complete_url_to_strategy}int_callback' - ); - - /** - * Auth request - */ - public function request(){ - $url = 'https://www.facebook.com/dialog/oauth'; - $params = array( - 'client_id' => $this->strategy['app_id'], - 'redirect_uri' => $this->strategy['redirect_uri'] - ); - - if (!empty($this->strategy['scope'])) $params['scope'] = $this->strategy['scope']; - if (!empty($this->strategy['state'])) $params['state'] = $this->strategy['state']; - if (!empty($this->strategy['response_type'])) $params['response_type'] = $this->strategy['response_type']; - if (!empty($this->strategy['display'])) $params['display'] = $this->strategy['display']; - if (!empty($this->strategy['auth_type'])) $params['auth_type'] = $this->strategy['auth_type']; - - $this->clientGet($url, $params); - } - - /** - * Internal callback, after Facebook's OAuth - */ - public function int_callback(){ - if (array_key_exists('code', $_GET) && !empty($_GET['code'])){ - $url = 'https://graph.facebook.com/oauth/access_token'; - $params = array( - 'client_id' =>$this->strategy['app_id'], - 'client_secret' => $this->strategy['app_secret'], - 'redirect_uri'=> $this->strategy['redirect_uri'], - 'code' => trim($_GET['code']) - ); - $response = $this->serverGet($url, $params, null, $headers); - - parse_str($response, $results); - - if (!empty($results) && !empty($results['access_token'])){ - $me = $this->me($results['access_token']); - - $this->auth = array( - 'provider' => 'Facebook', - 'uid' => $me->id, - 'info' => array( - 'name' => $me->name, - 'image' => 'https://graph.facebook.com/'.$me->id.'/picture?type=square' - ), - 'credentials' => array( - 'token' => $results['access_token'], - 'expires' => date('c', time() + $results['expires']) - ), - 'raw' => $me - ); - - if (!empty($me->email)) $this->auth['info']['email'] = $me->email; - if (!empty($me->username)) $this->auth['info']['nickname'] = $me->username; - if (!empty($me->first_name)) $this->auth['info']['first_name'] = $me->first_name; - if (!empty($me->last_name)) $this->auth['info']['last_name'] = $me->last_name; - if (!empty($me->location)) $this->auth['info']['location'] = $me->location->name; - if (!empty($me->link)) $this->auth['info']['urls']['facebook'] = $me->link; - if (!empty($me->website)) $this->auth['info']['urls']['website'] = $me->website; - - /** - * Missing optional info values - * - description - * - phone: not accessible via Facebook Graph API - */ - - $this->callback(); - } - else{ - $error = array( - 'provider' => 'Facebook', - 'code' => 'access_token_error', - 'message' => 'Failed when attempting to obtain access token', - 'raw' => $headers - ); - - $this->errorCallback($error); - } - } - else{ - $error = array( - 'provider' => 'Facebook', - 'code' => $_GET['error'], - 'message' => $_GET['error_description'], - 'raw' => $_GET - ); - - $this->errorCallback($error); - } - } - - /** - * Queries Facebook Graph API for user info - * - * @param string $access_token - * @return array Parsed JSON results - */ - private function me($access_token){ - $me = $this->serverGet('https://graph.facebook.com/me', array('access_token' => $access_token), null, $headers); - if (!empty($me)){ - return json_decode($me); - } - else{ - $error = array( - 'provider' => 'Facebook', - 'code' => 'me_error', - 'message' => 'Failed when attempting to query for user information', - 'raw' => array( - 'response' => $me, - 'headers' => $headers - ) - ); - - $this->errorCallback($error); - } - } -} \ No newline at end of file diff --git a/Strategy.php b/Strategy.php new file mode 100644 index 0000000..368e6b5 --- /dev/null +++ b/Strategy.php @@ -0,0 +1,134 @@ + 'client_id' + ); + $params = $this->addParams($strategyKeys); + $params['redirect_uri'] = $this->callbackUrl(); + HttpClient::redirect($url, $params); + } + + /** + * Internal callback, after Facebook's OAuth + */ + public function callback() { + if (array_key_exists('code', $_GET) && !empty($_GET['code'])){ + $url = 'https://graph.facebook.com/oauth/access_token'; + $params = array( + 'redirect_uri'=> $this->callbackUrl(), + 'code' => trim($_GET['code']) + ); + $strategyKeys = array( + 'app_id' => 'client_id', + 'app_secret' => 'client_secret' + ); + $params = $this->addParams($strategyKeys, $params); + $response = HttpClient::get($url, $params); + + parse_str($response, $results); + + if (!empty($results) && !empty($results['access_token'])){ + $me = $this->me($results['access_token']); + + $response = new Response($this->strategy['provider'], $this->recursiveGetObjectVars($me)); + $response->credentials = array( + 'token' => $results['access_token'], + 'expires' => date('c', time() + $results['expires']) + ); + $response->info['image'] = 'https://graph.facebook.com/'. $me->id. '/picture?type=square'; + $response->setMap(array( + 'name' => 'username', + 'uid' => 'id', + 'info.name' => 'name', + 'info.email' => 'email', + 'info.nickname' => 'username', + 'info.first_name' => 'first_name', + 'info.last_name' => 'last_name', + 'info.location' => 'location.name', + 'info.urls.website' => 'website' + )); + return $response; + } + else{ + $error = array( + 'provider' => 'Facebook', + 'code' => 'access_token_error', + 'message' => 'Failed when attempting to obtain access token', + 'raw' => HttpClient::$responseHeaders + ); + + return $this->errorCallback($error); + } + } + else{ + $error = array( + 'provider' => 'Facebook', + 'code' => $_GET['error'], + 'message' => $_GET['error_description'], + 'raw' => $_GET + ); + + return $this->errorCallback($error); + } + } + + /** + * Queries Facebook Graph API for user info + * + * @param string $access_token + * @return array Parsed JSON results + */ + private function me($access_token) { + $me = HttpClient::get('https://graph.facebook.com/me', array('access_token' => $access_token)); + if (!empty($me)) { + return json_decode($me); + } + else{ + $error = array( + 'provider' => 'Facebook', + 'code' => 'me_error', + 'message' => 'Failed when attempting to query for user information', + 'raw' => array( + 'response' => $me, + 'headers' => HttpClient::$responseHeaders + ) + ); + + return $this->errorCallback($error); + } + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 2b7b6fc..d1a0d43 100644 --- a/composer.json +++ b/composer.json @@ -12,12 +12,11 @@ } ], "require": { - "php": ">=5.2.0", - "opauth/opauth": ">=0.2.0" + "php": ">=5.3.0", + "opauth/opauth": "dev-wip/1.0" }, "autoload": { - "psr-0": { - "": "." - } - } + "psr-0": { "Opauth\\Provider\\Facebook": "" } + }, + "target-dir": "Opauth/Provider/Facebook" } \ No newline at end of file From 5aaf30885d7ee4340815cb4bc16313982db33443 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Tue, 19 Feb 2013 12:45:17 +0100 Subject: [PATCH 02/21] update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index d1a0d43..6eb0197 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "require": { "php": ">=5.3.0", - "opauth/opauth": "dev-wip/1.0" + "opauth/opauth": "wip/1.0" }, "autoload": { "psr-0": { "Opauth\\Provider\\Facebook": "" } From 21bb68c192a3ff1260e5c9eed6d02067dc9b653a Mon Sep 17 00:00:00 2001 From: Ceeram Date: Tue, 19 Feb 2013 12:57:12 +0100 Subject: [PATCH 03/21] undo composer.json change --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6eb0197..d1a0d43 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,7 @@ ], "require": { "php": ">=5.3.0", - "opauth/opauth": "wip/1.0" + "opauth/opauth": "dev-wip/1.0" }, "autoload": { "psr-0": { "Opauth\\Provider\\Facebook": "" } From f9708fc7869dc6e76169160998a03410ca81fe2f Mon Sep 17 00:00:00 2001 From: Ceeram Date: Tue, 19 Feb 2013 18:54:31 +0100 Subject: [PATCH 04/21] rename Provider back to Strategy --- Strategy.php | 2 +- composer.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Strategy.php b/Strategy.php index 368e6b5..2db8992 100644 --- a/Strategy.php +++ b/Strategy.php @@ -10,7 +10,7 @@ * @package Opauth.FacebookStrategy * @license MIT License */ -namespace Opauth\Provider\Facebook; +namespace Opauth\Strategy\Facebook; use Opauth\AbstractStrategy; use Opauth\HttpClient; diff --git a/composer.json b/composer.json index d1a0d43..a518e0a 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "opauth/opauth": "dev-wip/1.0" }, "autoload": { - "psr-0": { "Opauth\\Provider\\Facebook": "" } + "psr-0": { "Opauth\\Strategy\\Facebook": "" } }, - "target-dir": "Opauth/Provider/Facebook" + "target-dir": "Opauth/Strategy/Facebook" } \ No newline at end of file From 21f46ec477973394c252c84de8b87c3752b78ab7 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Wed, 20 Feb 2013 00:45:08 +0100 Subject: [PATCH 05/21] add responseMap property, splitting up callback() into several methods --- Strategy.php | 147 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 87 insertions(+), 60 deletions(-) diff --git a/Strategy.php b/Strategy.php index 2db8992..e9a599f 100644 --- a/Strategy.php +++ b/Strategy.php @@ -24,6 +24,23 @@ class Strategy extends AbstractStrategy { */ public $expects = array('app_id', 'app_secret'); + /** + * Map response from raw data + * + * @var array + */ + public $responseMap = array( + 'name' => 'username', + 'uid' => 'id', + 'info.name' => 'name', + 'info.email' => 'email', + 'info.nickname' => 'username', + 'info.first_name' => 'first_name', + 'info.last_name' => 'last_name', + 'info.location' => 'location.name', + 'info.urls.website' => 'website' + ); + /** * Auth request */ @@ -46,64 +63,76 @@ public function request() { * Internal callback, after Facebook's OAuth */ public function callback() { - if (array_key_exists('code', $_GET) && !empty($_GET['code'])){ - $url = 'https://graph.facebook.com/oauth/access_token'; - $params = array( - 'redirect_uri'=> $this->callbackUrl(), - 'code' => trim($_GET['code']) - ); - $strategyKeys = array( - 'app_id' => 'client_id', - 'app_secret' => 'client_secret' - ); - $params = $this->addParams($strategyKeys, $params); - $response = HttpClient::get($url, $params); - - parse_str($response, $results); - - if (!empty($results) && !empty($results['access_token'])){ - $me = $this->me($results['access_token']); - - $response = new Response($this->strategy['provider'], $this->recursiveGetObjectVars($me)); - $response->credentials = array( - 'token' => $results['access_token'], - 'expires' => date('c', time() + $results['expires']) - ); - $response->info['image'] = 'https://graph.facebook.com/'. $me->id. '/picture?type=square'; - $response->setMap(array( - 'name' => 'username', - 'uid' => 'id', - 'info.name' => 'name', - 'info.email' => 'email', - 'info.nickname' => 'username', - 'info.first_name' => 'first_name', - 'info.last_name' => 'last_name', - 'info.location' => 'location.name', - 'info.urls.website' => 'website' - )); - return $response; - } - else{ - $error = array( - 'provider' => 'Facebook', - 'code' => 'access_token_error', - 'message' => 'Failed when attempting to obtain access token', - 'raw' => HttpClient::$responseHeaders - ); - - return $this->errorCallback($error); - } + if (!array_key_exists('code', $_GET) || empty($_GET['code'])) { + return $this->codeError(); } - else{ - $error = array( - 'provider' => 'Facebook', - 'code' => $_GET['error'], - 'message' => $_GET['error_description'], - 'raw' => $_GET - ); - return $this->errorCallback($error); + $url = 'https://graph.facebook.com/oauth/access_token'; + $params = $this->buildParams(); + $response = HttpClient::get($url, $params); + parse_str($response, $results); + + if (empty($results) || empty($results['access_token'])) { + return $this->tokenError(); } + + $me = $this->me($results['access_token']); + + $response = new Response($this->strategy['provider'], $me); + $response->credentials = array( + 'token' => $results['access_token'], + 'expires' => date('c', time() + $results['expires']) + ); + $response->info['image'] = 'https://graph.facebook.com/'. $me['id'] . '/picture?type=square'; + $response->setMap($this->responseMap); + return $response; + } + + /** + * Helper method for callback() + * + * @return array Parameter array + */ + protected function buildParams() { + $params = array( + 'redirect_uri'=> $this->callbackUrl(), + 'code' => trim($_GET['code']) + ); + $strategyKeys = array( + 'app_id' => 'client_id', + 'app_secret' => 'client_secret' + ); + return $this->addParams($strategyKeys, $params); + } + + /** + * + * @return type + */ + protected function codeError() { + $error = array( + 'provider' => $this->strategy['provider'], + 'code' => $_GET['error'], + 'message' => $_GET['error_description'], + 'raw' => $_GET + ); + + return $this->errorCallback($error); + } + + /** + * + * @return returns + */ + protected function tokenError() { + $error = array( + 'provider' => $this->strategy['provider'], + 'code' => 'access_token_error', + 'message' => 'Failed when attempting to obtain access token', + 'raw' => HttpClient::$responseHeaders + ); + + return $this->errorCallback($error); } /** @@ -112,12 +141,9 @@ public function callback() { * @param string $access_token * @return array Parsed JSON results */ - private function me($access_token) { + protected function me($access_token) { $me = HttpClient::get('https://graph.facebook.com/me', array('access_token' => $access_token)); - if (!empty($me)) { - return json_decode($me); - } - else{ + if (empty($me)) { $error = array( 'provider' => 'Facebook', 'code' => 'me_error', @@ -130,5 +156,6 @@ private function me($access_token) { return $this->errorCallback($error); } + return $this->recursiveGetObjectVars(json_decode($me)); } } \ No newline at end of file From 30b9ba2b3319f2f58a510bff1298aa19ce50f077 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Wed, 20 Feb 2013 01:50:05 +0100 Subject: [PATCH 06/21] update composer.json --- composer.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/composer.json b/composer.json index a518e0a..2ecb511 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,10 @@ "name": "U-Zyn Chua", "email": "chua@uzyn.com", "homepage": "http://uzyn.com" + }, + { + "name": "Ceeram", + "email": "c33ram@gmail.com", } ], "require": { From 3bf4b3470af8d969820a74d19764d41e519a8a18 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Wed, 20 Feb 2013 13:06:47 +0100 Subject: [PATCH 07/21] rename method --- Strategy.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Strategy.php b/Strategy.php index e9a599f..94ea882 100644 --- a/Strategy.php +++ b/Strategy.php @@ -68,7 +68,7 @@ public function callback() { } $url = 'https://graph.facebook.com/oauth/access_token'; - $params = $this->buildParams(); + $params = $this->callbackParams(); $response = HttpClient::get($url, $params); parse_str($response, $results); @@ -93,7 +93,7 @@ public function callback() { * * @return array Parameter array */ - protected function buildParams() { + protected function callbackParams() { $params = array( 'redirect_uri'=> $this->callbackUrl(), 'code' => trim($_GET['code']) From 173664065f9d5b007ea7e4502bebeb58e144fca2 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Wed, 20 Feb 2013 14:00:13 +0100 Subject: [PATCH 08/21] fix composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 2ecb511..a944ad7 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ }, { "name": "Ceeram", - "email": "c33ram@gmail.com", + "email": "c33ram@gmail.com" } ], "require": { From 14f9916757ca7cacc5b9301b1427f61669421ad7 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Fri, 7 Jun 2013 14:45:14 +0200 Subject: [PATCH 09/21] update for latest changes in opauth 1.0 --- Strategy.php | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/Strategy.php b/Strategy.php index 94ea882..5fa1c5c 100644 --- a/Strategy.php +++ b/Strategy.php @@ -78,13 +78,12 @@ public function callback() { $me = $this->me($results['access_token']); - $response = new Response($this->strategy['provider'], $me); + $response = $this->response($me); $response->credentials = array( 'token' => $results['access_token'], 'expires' => date('c', time() + $results['expires']) ); $response->info['image'] = 'https://graph.facebook.com/'. $me['id'] . '/picture?type=square'; - $response->setMap($this->responseMap); return $response; } @@ -111,13 +110,11 @@ protected function callbackParams() { */ protected function codeError() { $error = array( - 'provider' => $this->strategy['provider'], 'code' => $_GET['error'], 'message' => $_GET['error_description'], - 'raw' => $_GET ); - return $this->errorCallback($error); + return $this->response($_GET, $error); } /** @@ -126,13 +123,11 @@ protected function codeError() { */ protected function tokenError() { $error = array( - 'provider' => $this->strategy['provider'], 'code' => 'access_token_error', 'message' => 'Failed when attempting to obtain access token', - 'raw' => HttpClient::$responseHeaders ); - return $this->errorCallback($error); + return $this->response(HttpClient::$responseHeaders, $error); } /** @@ -145,16 +140,11 @@ protected function me($access_token) { $me = HttpClient::get('https://graph.facebook.com/me', array('access_token' => $access_token)); if (empty($me)) { $error = array( - 'provider' => 'Facebook', 'code' => 'me_error', - 'message' => 'Failed when attempting to query for user information', - 'raw' => array( - 'response' => $me, - 'headers' => HttpClient::$responseHeaders - ) + 'message' => 'Failed when attempting to query for user information' ); - return $this->errorCallback($error); + return $this->response(HttpClient::$responseHeaders, $error); } return $this->recursiveGetObjectVars(json_decode($me)); } From 614a23abf807b8d1c467fd4ec39eab1c79fc2f78 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Fri, 7 Jun 2013 18:50:38 +0200 Subject: [PATCH 10/21] minor tweaks --- Strategy.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Strategy.php b/Strategy.php index 5fa1c5c..619737c 100644 --- a/Strategy.php +++ b/Strategy.php @@ -72,11 +72,18 @@ public function callback() { $response = HttpClient::get($url, $params); parse_str($response, $results); - if (empty($results) || empty($results['access_token'])) { - return $this->tokenError(); + if (empty($results['access_token'])) { + return $this->tokenError($response); } $me = $this->me($results['access_token']); + if (!$me) { + $error = array( + 'code' => 'me_error', + 'message' => 'Failed when attempting to query for user information' + ); + return $this->response(null, $error); + } $response = $this->response($me); $response->credentials = array( @@ -121,13 +128,13 @@ protected function codeError() { * * @return returns */ - protected function tokenError() { + protected function tokenError($raw) { $error = array( 'code' => 'access_token_error', 'message' => 'Failed when attempting to obtain access token', ); - return $this->response(HttpClient::$responseHeaders, $error); + return $this->response($raw, $error); } /** @@ -139,12 +146,7 @@ protected function tokenError() { protected function me($access_token) { $me = HttpClient::get('https://graph.facebook.com/me', array('access_token' => $access_token)); if (empty($me)) { - $error = array( - 'code' => 'me_error', - 'message' => 'Failed when attempting to query for user information' - ); - - return $this->response(HttpClient::$responseHeaders, $error); + return false; } return $this->recursiveGetObjectVars(json_decode($me)); } From 7412a06a606b09beab9496373742c1b4793193d6 Mon Sep 17 00:00:00 2001 From: ceeram Date: Mon, 10 Jun 2013 18:09:42 +0300 Subject: [PATCH 11/21] Update Strategy.php --- Strategy.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Strategy.php b/Strategy.php index 619737c..fc7cfa8 100644 --- a/Strategy.php +++ b/Strategy.php @@ -13,8 +13,6 @@ namespace Opauth\Strategy\Facebook; use Opauth\AbstractStrategy; -use Opauth\HttpClient; -use Opauth\Response; class Strategy extends AbstractStrategy { @@ -56,7 +54,7 @@ public function request() { ); $params = $this->addParams($strategyKeys); $params['redirect_uri'] = $this->callbackUrl(); - HttpClient::redirect($url, $params); + $this->http->redirect($url, $params); } /** @@ -69,7 +67,7 @@ public function callback() { $url = 'https://graph.facebook.com/oauth/access_token'; $params = $this->callbackParams(); - $response = HttpClient::get($url, $params); + $response = $this->http->get($url, $params); parse_str($response, $results); if (empty($results['access_token'])) { @@ -144,10 +142,10 @@ protected function tokenError($raw) { * @return array Parsed JSON results */ protected function me($access_token) { - $me = HttpClient::get('https://graph.facebook.com/me', array('access_token' => $access_token)); + $me = $this->http->get('https://graph.facebook.com/me', array('access_token' => $access_token)); if (empty($me)) { return false; } return $this->recursiveGetObjectVars(json_decode($me)); } -} \ No newline at end of file +} From 173e08cd1f550fc8c81d09c720f311d8969d9a55 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Sun, 23 Feb 2014 18:35:04 +0100 Subject: [PATCH 12/21] change to psr4 --- composer.json | 7 +++---- Strategy.php => src/Facebook.php | 6 +++--- 2 files changed, 6 insertions(+), 7 deletions(-) rename Strategy.php => src/Facebook.php (96%) diff --git a/composer.json b/composer.json index a944ad7..b755a9d 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,6 @@ "opauth/opauth": "dev-wip/1.0" }, "autoload": { - "psr-0": { "Opauth\\Strategy\\Facebook": "" } - }, - "target-dir": "Opauth/Strategy/Facebook" -} \ No newline at end of file + "psr-4": { "Opauth\\Facebook\\Strategy\\": "src" } + } +} diff --git a/Strategy.php b/src/Facebook.php similarity index 96% rename from Strategy.php rename to src/Facebook.php index fc7cfa8..01c7eaa 100644 --- a/Strategy.php +++ b/src/Facebook.php @@ -10,11 +10,11 @@ * @package Opauth.FacebookStrategy * @license MIT License */ -namespace Opauth\Strategy\Facebook; +namespace Opauth\Facebook\Strategy; -use Opauth\AbstractStrategy; +use Opauth\Opauth\AbstractStrategy; -class Strategy extends AbstractStrategy { +class Facebook extends AbstractStrategy { /** * Compulsory config keys, listed as unassociative arrays From 66183ada792b693c7f2336d123f3408e115293bf Mon Sep 17 00:00:00 2001 From: Ceeram Date: Mon, 24 Mar 2014 17:16:41 +0100 Subject: [PATCH 13/21] update readme --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cfdd38a..e65831b 100644 --- a/README.md +++ b/README.md @@ -8,8 +8,8 @@ Getting started ---------------- 1. Install Opauth-Facebook: ```bash - cd path_to_opauth/Strategy - git clone git://github.com/uzyn/opauth-facebook.git Facebook + cd path/to/app/root + composer require opauth/facebook:dev-wip/1.0 ``` 2. Create Facebook application at https://developers.facebook.com/apps/ @@ -33,13 +33,13 @@ Required parameters: ) ``` -Even though `scope` is an optional configuration parameter for Opauth-Facebook, for most cases you would like to explicitly define it. It should be defined in a comma-separated string. +Even though `scope` is an optional configuration parameter for Opauth-Facebook, for most cases you would like to explicitly define it. It should be defined in a comma-separated string. Refer to [Facebook Permissions Reference](https://developers.facebook.com/docs/authentication/permissions/) for list of valid permissions.. License --------- -Opauth-Facebook is MIT Licensed +Opauth-Facebook is MIT Licensed Copyright © 2012 U-Zyn Chua (http://uzyn.com) [1]: https://github.com/uzyn/opauth \ No newline at end of file From 00cd980a9cffb7139be7584f733486c5766d98c5 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Fri, 28 Mar 2014 09:51:49 +0100 Subject: [PATCH 14/21] tabs to spaces --- composer.json | 48 ++++---- src/Facebook.php | 278 ++++++++++++++++++++++++----------------------- 2 files changed, 169 insertions(+), 157 deletions(-) diff --git a/composer.json b/composer.json index b755a9d..f9450ca 100644 --- a/composer.json +++ b/composer.json @@ -1,25 +1,27 @@ { - "name": "opauth/facebook", - "description": "Facebook strategy for Opauth", - "keywords": ["authentication","auth","facebook"], - "homepage": "http://opauth.org", - "license": "MIT", - "authors": [ - { - "name": "U-Zyn Chua", - "email": "chua@uzyn.com", - "homepage": "http://uzyn.com" - }, - { - "name": "Ceeram", - "email": "c33ram@gmail.com" - } - ], - "require": { - "php": ">=5.3.0", - "opauth/opauth": "dev-wip/1.0" - }, - "autoload": { - "psr-4": { "Opauth\\Facebook\\Strategy\\": "src" } - } + "name": "opauth/facebook", + "description": "Facebook strategy for Opauth", + "keywords": ["authentication", "auth", "facebook"], + "homepage": "http://opauth.org", + "license": "MIT", + "authors": [ + { + "name": "U-Zyn Chua", + "email": "chua@uzyn.com", + "homepage": "http://uzyn.com" + }, + { + "name": "Ceeram", + "email": "c33ram@gmail.com" + } + ], + "require": { + "php": ">=5.3.0", + "opauth/opauth": "dev-wip/1.0" + }, + "autoload": { + "psr-4": { + "Opauth\\Facebook\\Strategy\\": "src" + } + } } diff --git a/src/Facebook.php b/src/Facebook.php index 01c7eaa..1cc5c4e 100644 --- a/src/Facebook.php +++ b/src/Facebook.php @@ -14,138 +14,148 @@ use Opauth\Opauth\AbstractStrategy; -class Facebook extends AbstractStrategy { - - /** - * Compulsory config keys, listed as unassociative arrays - * eg. array('app_id', 'app_secret'); - */ - public $expects = array('app_id', 'app_secret'); - - /** - * Map response from raw data - * - * @var array - */ - public $responseMap = array( - 'name' => 'username', - 'uid' => 'id', - 'info.name' => 'name', - 'info.email' => 'email', - 'info.nickname' => 'username', - 'info.first_name' => 'first_name', - 'info.last_name' => 'last_name', - 'info.location' => 'location.name', - 'info.urls.website' => 'website' - ); - - /** - * Auth request - */ - public function request() { - $url = 'https://www.facebook.com/dialog/oauth'; - $strategyKeys = array( - 'scope', - 'state', - 'response_type', - 'display', - 'auth_type', - 'app_id' => 'client_id' - ); - $params = $this->addParams($strategyKeys); - $params['redirect_uri'] = $this->callbackUrl(); - $this->http->redirect($url, $params); - } - - /** - * Internal callback, after Facebook's OAuth - */ - public function callback() { - if (!array_key_exists('code', $_GET) || empty($_GET['code'])) { - return $this->codeError(); - } - - $url = 'https://graph.facebook.com/oauth/access_token'; - $params = $this->callbackParams(); - $response = $this->http->get($url, $params); - parse_str($response, $results); - - if (empty($results['access_token'])) { - return $this->tokenError($response); - } - - $me = $this->me($results['access_token']); - if (!$me) { - $error = array( - 'code' => 'me_error', - 'message' => 'Failed when attempting to query for user information' - ); - return $this->response(null, $error); - } - - $response = $this->response($me); - $response->credentials = array( - 'token' => $results['access_token'], - 'expires' => date('c', time() + $results['expires']) - ); - $response->info['image'] = 'https://graph.facebook.com/'. $me['id'] . '/picture?type=square'; - return $response; - } - - /** - * Helper method for callback() - * - * @return array Parameter array - */ - protected function callbackParams() { - $params = array( - 'redirect_uri'=> $this->callbackUrl(), - 'code' => trim($_GET['code']) - ); - $strategyKeys = array( - 'app_id' => 'client_id', - 'app_secret' => 'client_secret' - ); - return $this->addParams($strategyKeys, $params); - } - - /** - * - * @return type - */ - protected function codeError() { - $error = array( - 'code' => $_GET['error'], - 'message' => $_GET['error_description'], - ); - - return $this->response($_GET, $error); - } - - /** - * - * @return returns - */ - protected function tokenError($raw) { - $error = array( - 'code' => 'access_token_error', - 'message' => 'Failed when attempting to obtain access token', - ); - - return $this->response($raw, $error); - } - - /** - * Queries Facebook Graph API for user info - * - * @param string $access_token - * @return array Parsed JSON results - */ - protected function me($access_token) { - $me = $this->http->get('https://graph.facebook.com/me', array('access_token' => $access_token)); - if (empty($me)) { - return false; - } - return $this->recursiveGetObjectVars(json_decode($me)); - } +class Facebook extends AbstractStrategy +{ + + /** + * Compulsory config keys, listed as numeric indexed arrays + * eg. array('app_id', 'app_secret'); + */ + public $expects = array('app_id', 'app_secret'); + + /** + * Map response from raw data + * + * @var array + */ + public $responseMap = array( + 'name' => 'username', + 'uid' => 'id', + 'info.name' => 'name', + 'info.email' => 'email', + 'info.nickname' => 'username', + 'info.first_name' => 'first_name', + 'info.last_name' => 'last_name', + 'info.location' => 'location.name', + 'info.urls.website' => 'website' + ); + + /** + * Auth request + * + * @return void + */ + public function request() + { + $url = 'https://www.facebook.com/dialog/oauth'; + $strategyKeys = array( + 'scope', + 'state', + 'response_type', + 'display', + 'auth_type', + 'app_id' => 'client_id' + ); + $params = $this->addParams($strategyKeys); + $params['redirect_uri'] = $this->callbackUrl(); + $this->http->redirect($url, $params); + } + + /** + * Internal callback, after Facebook's OAuth + * + * @return \Opauth\Opauth\Response + */ + public function callback() + { + if (!array_key_exists('code', $_GET) || empty($_GET['code'])) { + return $this->codeError(); + } + + $url = 'https://graph.facebook.com/oauth/access_token'; + $params = $this->callbackParams(); + $response = $this->http->get($url, $params); + parse_str($response, $results); + + if (empty($results['access_token'])) { + return $this->tokenError($response); + } + + $me = $this->me($results['access_token']); + if (!$me) { + $error = array( + 'code' => 'me_error', + 'message' => 'Failed when attempting to query for user information' + ); + return $this->response(null, $error); + } + + $response = $this->response($me); + $response->credentials = array( + 'token' => $results['access_token'], + 'expires' => date('c', time() + $results['expires']) + ); + $response->info['image'] = 'https://graph.facebook.com/' . $me['id'] . '/picture?type=square'; + return $response; + } + + /** + * Helper method for callback() + * + * @return array Parameter array + */ + protected function callbackParams() + { + $params = array( + 'redirect_uri' => $this->callbackUrl(), + 'code' => trim($_GET['code']) + ); + $strategyKeys = array( + 'app_id' => 'client_id', + 'app_secret' => 'client_secret' + ); + return $this->addParams($strategyKeys, $params); + } + + /** + * @return \Opauth\Opauth\Response + */ + protected function codeError() + { + $error = array( + 'code' => $_GET['error'], + 'message' => $_GET['error_description'], + ); + + return $this->response($_GET, $error); + } + + /** + * @param string $raw + * @return \Opauth\Opauth\Response + */ + protected function tokenError($raw) + { + $error = array( + 'code' => 'access_token_error', + 'message' => 'Failed when attempting to obtain access token', + ); + + return $this->response($raw, $error); + } + + /** + * Queries Facebook Graph API for user info + * + * @param string $access_token + * @return array Parsed JSON results + */ + protected function me($access_token) + { + $me = $this->http->get('https://graph.facebook.com/me', array('access_token' => $access_token)); + if (empty($me)) { + return false; + } + return $this->recursiveGetObjectVars(json_decode($me)); + } } From 1e65b232ae2fa83c8d9ba719d567a62f5df8ccc8 Mon Sep 17 00:00:00 2001 From: U-Zyn Chua Date: Mon, 31 Mar 2014 00:33:51 +0800 Subject: [PATCH 15/21] Update error return. --- src/Facebook.php | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/Facebook.php b/src/Facebook.php index 1cc5c4e..a46186f 100644 --- a/src/Facebook.php +++ b/src/Facebook.php @@ -83,11 +83,7 @@ public function callback() $me = $this->me($results['access_token']); if (!$me) { - $error = array( - 'code' => 'me_error', - 'message' => 'Failed when attempting to query for user information' - ); - return $this->response(null, $error); + return $this->error('Failed when attempting to query for user information.', 'me_error'); } $response = $this->response($me); @@ -122,12 +118,7 @@ protected function callbackParams() */ protected function codeError() { - $error = array( - 'code' => $_GET['error'], - 'message' => $_GET['error_description'], - ); - - return $this->response($_GET, $error); + return $this->error($_GET['error_description'], $_GET['error'], $_GET); } /** @@ -136,12 +127,7 @@ protected function codeError() */ protected function tokenError($raw) { - $error = array( - 'code' => 'access_token_error', - 'message' => 'Failed when attempting to obtain access token', - ); - - return $this->response($raw, $error); + return $this->error('Failed when attempting to obtain access token.', 'access_token_error', $raw); } /** From 978b20fbf01b84fd31b71ee1f990e71e1bec7fc5 Mon Sep 17 00:00:00 2001 From: U-Zyn Chua Date: Mon, 31 Mar 2014 00:55:19 +0800 Subject: [PATCH 16/21] Updated Opauth version requirement --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index f9450ca..7bdabb5 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": ">=5.3.0", - "opauth/opauth": "dev-wip/1.0" + "opauth/opauth": "1.*" }, "autoload": { "psr-4": { From 267b68773822ff2c0d9761956d00dc657b3d1203 Mon Sep 17 00:00:00 2001 From: U-Zyn Chua Date: Mon, 31 Mar 2014 01:01:39 +0800 Subject: [PATCH 17/21] Require opauth 1.0.*@dev --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7bdabb5..36dc692 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": ">=5.3.0", - "opauth/opauth": "1.*" + "opauth/opauth": "1.0.*@dev" }, "autoload": { "psr-4": { From be72cda6a8a53ced58751281e15a1d9b1271b032 Mon Sep 17 00:00:00 2001 From: augusto-cdxs Date: Thu, 3 Apr 2014 17:27:32 -0300 Subject: [PATCH 18/21] Fixed token received with no expiration --- src/Facebook.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Facebook.php b/src/Facebook.php index a46186f..784f93c 100644 --- a/src/Facebook.php +++ b/src/Facebook.php @@ -89,7 +89,7 @@ public function callback() $response = $this->response($me); $response->credentials = array( 'token' => $results['access_token'], - 'expires' => date('c', time() + $results['expires']) + 'expires' => isset($results['expires']) ? date('c', time() + $results['expires']) : null ); $response->info['image'] = 'https://graph.facebook.com/' . $me['id'] . '/picture?type=square'; return $response; From b0548d451c7c8b8488910f2aebce72eebc9cd655 Mon Sep 17 00:00:00 2001 From: Ceeram Date: Fri, 4 Apr 2014 01:45:50 +0200 Subject: [PATCH 19/21] update redirect call --- src/Facebook.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Facebook.php b/src/Facebook.php index a46186f..58add2d 100644 --- a/src/Facebook.php +++ b/src/Facebook.php @@ -58,7 +58,7 @@ public function request() ); $params = $this->addParams($strategyKeys); $params['redirect_uri'] = $this->callbackUrl(); - $this->http->redirect($url, $params); + $this->redirect($url, $params); } /** From 21c4827eaccb597b66c39f841774f0839e78b6c5 Mon Sep 17 00:00:00 2001 From: U-Zyn Chua Date: Tue, 8 Apr 2014 00:43:24 +0800 Subject: [PATCH 20/21] opauth/opauth: ~1.0 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 36dc692..75d513d 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ ], "require": { "php": ">=5.3.0", - "opauth/opauth": "1.0.*@dev" + "opauth/opauth": "~1.0" }, "autoload": { "psr-4": { From bde5076627d5840d16753f93d7802869d7ac9a9a Mon Sep 17 00:00:00 2001 From: joecohens Date: Fri, 9 May 2014 01:02:29 -0500 Subject: [PATCH 21/21] Remove nickname and change name to name Facebook has deprecated the username in the new version 2.0 of the Graph Api, so Opauth is throwing an error Invalid response, missing required parameters. https://developers.facebook.com/docs/apps/changelog --- src/Facebook.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Facebook.php b/src/Facebook.php index e8f052c..023a1bc 100644 --- a/src/Facebook.php +++ b/src/Facebook.php @@ -29,11 +29,10 @@ class Facebook extends AbstractStrategy * @var array */ public $responseMap = array( - 'name' => 'username', + 'name' => 'name', 'uid' => 'id', 'info.name' => 'name', 'info.email' => 'email', - 'info.nickname' => 'username', 'info.first_name' => 'first_name', 'info.last_name' => 'last_name', 'info.location' => 'location.name',