├── .gitignore ├── composer.json ├── README.md └── FacebookStrategy.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .idea 3 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "sokolnikov911/opauth-facebook-updated", 3 | "description": "Facebook strategy for Opauth", 4 | "keywords": ["authentication","auth","facebook"], 5 | "homepage": "http://opauth.org", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Petro Sokolnykov", 10 | "email": "info@xyz.net.ua", 11 | "homepage": "http://xyz.net.ua" 12 | }, 13 | { 14 | "name": "U-Zyn Chua", 15 | "email": "chua@uzyn.com", 16 | "homepage": "http://uzyn.com" 17 | } 18 | ], 19 | "require": { 20 | "php": ">=5.2.0", 21 | "opauth/opauth": ">=0.2.0" 22 | }, 23 | "autoload": { 24 | "psr-0": { 25 | "": "." 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Opauth-Facebook 2 | ============= 3 | [Opauth][1] strategy for Facebook authentication. 4 | 5 | Implemented based on https://developers.facebook.com/docs/authentication/ 6 | 7 | Getting started 8 | ---------------- 9 | 1. Install Opauth-Facebook: 10 | ```bash 11 | cd path_to_opauth/Strategy 12 | git clone https://github.com/sokolnikov911/opauth-facebook-updated.git Facebook 13 | ``` 14 | 15 | 2. Create Facebook application at https://developers.facebook.com/apps/ 16 | - Remember to enter App Domains 17 | - "Website with Facebook Login" must be checked, but for "Site URL", you can enter any landing URL. 18 | 19 | 3. Configure Opauth-Facebook strategy with at least `App ID` and `App Secret`. 20 | 21 | 4. Direct user to `http://path_to_opauth/facebook` to authenticate 22 | 23 | Strategy configuration 24 | ---------------------- 25 | 26 | Required parameters: 27 | 28 | ```php 29 | array( 31 | 'app_id' => 'YOUR APP ID', 32 | 'app_secret' => 'YOUR APP SECRET' 33 | ) 34 | ``` 35 | 36 | 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. 37 | 38 | Refer to [Facebook Permissions Reference](https://developers.facebook.com/docs/authentication/permissions/) for list of valid permissions.. 39 | 40 | What difference between this fork and original opauth/facebook 41 | --------- 42 | 43 | 1. Using large version of users avatars; 44 | 2. Using latest version of Facebook API Graph. 45 | 46 | License 47 | --------- 48 | Opauth-Facebook is MIT Licensed 49 | Copyright © 2012 U-Zyn Chua (http://uzyn.com) 50 | 51 | [1]: https://github.com/opauth/opauth 52 | -------------------------------------------------------------------------------- /FacebookStrategy.php: -------------------------------------------------------------------------------- 1 | 'email'); 25 | */ 26 | public $defaults = array( 27 | 'redirect_uri' => '{complete_url_to_strategy}int_callback' 28 | ); 29 | 30 | /** 31 | * Auth request 32 | */ 33 | public function request(){ 34 | $url = 'https://www.facebook.com/v2.8/dialog/oauth'; 35 | $params = array( 36 | 'client_id' => $this->strategy['app_id'], 37 | 'redirect_uri' => $this->strategy['redirect_uri'] 38 | ); 39 | 40 | if (!empty($this->strategy['scope'])) $params['scope'] = $this->strategy['scope']; 41 | if (!empty($this->strategy['state'])) $params['state'] = $this->strategy['state']; 42 | if (!empty($this->strategy['response_type'])) $params['response_type'] = $this->strategy['response_type']; 43 | if (!empty($this->strategy['display'])) $params['display'] = $this->strategy['display']; 44 | if (!empty($this->strategy['auth_type'])) $params['auth_type'] = $this->strategy['auth_type']; 45 | 46 | $this->clientGet($url, $params); 47 | } 48 | 49 | /** 50 | * Internal callback, after Facebook's OAuth 51 | */ 52 | public function int_callback(){ 53 | if (array_key_exists('code', $_GET) && !empty($_GET['code'])){ 54 | $url = 'https://graph.facebook.com/oauth/access_token'; 55 | $params = array( 56 | 'client_id' =>$this->strategy['app_id'], 57 | 'client_secret' => $this->strategy['app_secret'], 58 | 'redirect_uri'=> $this->strategy['redirect_uri'], 59 | 'code' => trim($_GET['code']) 60 | ); 61 | $response = $this->serverGet($url, $params, null, $headers); 62 | $results = json_decode($response); 63 | 64 | if (!empty($results) && !empty($results->access_token)){ 65 | $me = $this->me($results->access_token); 66 | $this->auth = array( 67 | 'provider' => 'Facebook', 68 | 'uid' => $me->id, 69 | 'info' => array( 70 | 'name' => $me->name, 71 | 'image' => 'https://graph.facebook.com/v2.8/'.$me->id.'/picture?type=large' 72 | ), 73 | 'credentials' => array( 74 | 'token' => $results->access_token, 75 | 'expires' => date('c', time() + $results->expires_in) 76 | ), 77 | 'raw' => $me 78 | ); 79 | 80 | if (!empty($me->email)) $this->auth['info']['email'] = $me->email; 81 | if (!empty($me->name)) $this->auth['info']['nickname'] = $me->name; 82 | if (!empty($me->first_name)) $this->auth['info']['first_name'] = $me->first_name; 83 | if (!empty($me->last_name)) $this->auth['info']['last_name'] = $me->last_name; 84 | if (!empty($me->link)) $this->auth['info']['urls']['facebook'] = $me->link; 85 | 86 | /** 87 | * Missing optional info values 88 | * - description 89 | * - phone: not accessible via Facebook Graph API 90 | */ 91 | 92 | $this->callback(); 93 | } 94 | else{ 95 | $error = array( 96 | 'provider' => 'Facebook', 97 | 'code' => 'access_token_error', 98 | 'message' => 'Failed when attempting to obtain access token', 99 | 'raw' => $headers 100 | ); 101 | 102 | $this->errorCallback($error); 103 | } 104 | } 105 | else{ 106 | $error = array( 107 | 'provider' => 'Facebook', 108 | 'code' => $_GET['error_code'], 109 | 'message' => $_GET['error_message'], 110 | 'raw' => $_GET 111 | ); 112 | 113 | $this->errorCallback($error); 114 | } 115 | } 116 | 117 | /** 118 | * Queries Facebook Graph API for user info 119 | * 120 | * @param string $access_token 121 | * @return array Parsed JSON results 122 | */ 123 | private function me($access_token){ 124 | 125 | $fields = 'id,name,email,link'; //default value 126 | if ( isset($this->strategy['fields']) ) { 127 | $fields = $this->strategy['fields']; 128 | } 129 | 130 | $me = $this->serverGet('https://graph.facebook.com/v2.8/me', array('access_token' => $access_token, 'fields' => $fields), null, $headers); 131 | 132 | if (!empty($me)){ 133 | return json_decode($me); 134 | } 135 | else{ 136 | $error = array( 137 | 'provider' => 'Facebook', 138 | 'code' => 'me_error', 139 | 'message' => 'Failed when attempting to query for user information', 140 | 'raw' => array( 141 | 'response' => $me, 142 | 'headers' => $headers 143 | ) 144 | ); 145 | 146 | $this->errorCallback($error); 147 | } 148 | } 149 | } --------------------------------------------------------------------------------