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/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 diff --git a/composer.json b/composer.json index 2b7b6fc..75d513d 100644 --- a/composer.json +++ b/composer.json @@ -1,23 +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" - } - ], - "require": { - "php": ">=5.2.0", - "opauth/opauth": ">=0.2.0" - }, - "autoload": { - "psr-0": { - "": "." - } - } -} \ No newline at end of file + "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": "~1.0" + }, + "autoload": { + "psr-4": { + "Opauth\\Facebook\\Strategy\\": "src" + } + } +} diff --git a/src/Facebook.php b/src/Facebook.php new file mode 100644 index 0000000..023a1bc --- /dev/null +++ b/src/Facebook.php @@ -0,0 +1,146 @@ + 'name', + 'uid' => 'id', + 'info.name' => 'name', + 'info.email' => 'email', + '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->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) { + return $this->error('Failed when attempting to query for user information.', 'me_error'); + } + + $response = $this->response($me); + $response->credentials = array( + 'token' => $results['access_token'], + 'expires' => isset($results['expires']) ? date('c', time() + $results['expires']) : null + ); + $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() + { + return $this->error($_GET['error_description'], $_GET['error'], $_GET); + } + + /** + * @param string $raw + * @return \Opauth\Opauth\Response + */ + protected function tokenError($raw) + { + return $this->error('Failed when attempting to obtain access token.', 'access_token_error', $raw); + } + + /** + * 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)); + } +}