| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * SocialConnect project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @author: Patsura Dmitry https://github.com/ovr <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace SocialConnect\OAuth2\Provider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SocialConnect\OAuth2\AccessToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SocialConnect\Provider\AccessTokenInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SocialConnect\Provider\Exception\InvalidAccessToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SocialConnect\Provider\Exception\InvalidResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use SocialConnect\Common\Entity\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SocialConnect\Common\Http\Client\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use SocialConnect\Common\Hydrator\ObjectMap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class Facebook extends \SocialConnect\OAuth2\AbstractProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const NAME = 'facebook'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * By default AbstractProvider use POST method, FB does not accept POST and return HTML page ᕙ(⇀‸↼‶)ᕗ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     protected $requestHttpMethod = Client::GET; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 3 |  |     public function getBaseUri() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 3 |  |         return 'https://graph.facebook.com/v3.3/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 2 |  |     public function getAuthorizeUri() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 2 |  |         return 'https://www.facebook.com/dialog/oauth'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 2 |  |     public function getRequestTokenUri() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 2 |  |         return 'https://graph.facebook.com/oauth/access_token'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |     public function getName() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  |         return self::NAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |     public function parseToken($body) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |         if (empty($body)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             throw new InvalidAccessToken('Provider response with empty body'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 3 |  |         $result = json_decode($body, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 3 |  |         if ($result) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |             return new AccessToken($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |         throw new InvalidAccessToken('Provider response with not valid JSON'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 2 |  |     public function getIdentity(AccessTokenInterface $accessToken) | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         $parameters = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 2 |  |             'access_token' => $accessToken->getToken(), | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 | 2 |  |         $fields = $this->getArrayOption('identity.fields', []); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 2 |  |         if ($fields) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |             $parameters['fields'] = implode(',', $fields); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 2 |  |         $response = $this->httpClient->request( | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 2 |  |             $this->getBaseUri() . 'me', | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 2 |  |             $parameters | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 2 |  |         if (!$response->isSuccess()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 1 |  |             throw new InvalidResponse( | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 1 |  |                 'API response with error code', | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 1 |  |                 $response | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 | 1 |  |         $result = $response->json(); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 | 1 |  |         if (!$result) { | 
            
                                                                        
                            
            
                                    
            
            
                | 93 | 1 |  |             throw new InvalidResponse( | 
            
                                                                        
                            
            
                                    
            
            
                | 94 | 1 |  |                 'API response is not a valid JSON object', | 
            
                                                                        
                            
            
                                    
            
            
                | 95 | 1 |  |                 $response | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         $hydrator = new ObjectMap( | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |                 'id' => 'id', | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |                 'first_name' => 'firstname', | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                 'last_name' => 'lastname', | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                 'email' => 'email', | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |                 'gender' => 'sex', | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                 'link' => 'url', | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                 'locale' => 'locale', | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                 'name' => 'fullname', | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |                 'timezone' => 'timezone', | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |                 'updated_time' => 'dateModified', | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                 'verified' => 'verified' | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         /** @var User $user */ | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |         $user = $hydrator->hydrate(new User(), $result); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         $user->emailVerified = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |         if (!empty($result->picture)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             $user->pictureURL = $result->picture->data->url; | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         return $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  |