|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Oauth\Client\Provider\Facebook; |
|
4
|
|
|
|
|
5
|
|
|
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; |
|
6
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Sludio\HelperBundle\Oauth\Client\Provider\BaseProvider; |
|
9
|
|
|
use Sludio\HelperBundle\Oauth\Exception\FacebookProviderException; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
11
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
12
|
|
|
|
|
13
|
|
|
class Facebook extends BaseProvider |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Production Graph API URL. |
|
17
|
|
|
* |
|
18
|
|
|
* @const string |
|
19
|
|
|
*/ |
|
20
|
|
|
const BASE_FACEBOOK_URL = 'https://www.facebook.com/'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Production Graph API URL. |
|
24
|
|
|
* |
|
25
|
|
|
* @const string |
|
26
|
|
|
*/ |
|
27
|
|
|
const BASE_GRAPH_URL = 'https://graph.facebook.com/'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Regular expression used to check for graph API version format |
|
31
|
|
|
* |
|
32
|
|
|
* @const string |
|
33
|
|
|
*/ |
|
34
|
|
|
const GRAPH_API_VERSION_REGEX = '~^v\d+\.\d+$~'; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The Graph API version to use for requests. |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $graphApiVersion; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param array $options |
|
45
|
|
|
* @param array $collaborators |
|
46
|
|
|
* @param UrlGeneratorInterface|null $generator |
|
47
|
|
|
* |
|
48
|
|
|
* @throws InvalidArgumentException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function __construct($options = [], array $collaborators = [], UrlGeneratorInterface $generator = null) |
|
51
|
|
|
{ |
|
52
|
|
|
parent::__construct($options, $collaborators, $generator); |
|
53
|
|
|
|
|
54
|
|
|
if (empty($options['graphApiVersion'])) { |
|
55
|
|
|
throw new InvalidArgumentException('error_facebook_graph_api_version_not_set'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (!preg_match(self::GRAPH_API_VERSION_REGEX, $options['graphApiVersion'])) { |
|
59
|
|
|
throw new InvalidArgumentException('error_facebook_wrong_graph_api_version'); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$this->graphApiVersion = $options['graphApiVersion']; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function getBaseAuthorizationUrl() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->getBaseFacebookUrl().$this->graphApiVersion.'/dialog/oauth'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get the base Facebook URL. |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
private function getBaseFacebookUrl() |
|
76
|
|
|
{ |
|
77
|
|
|
return static::BASE_FACEBOOK_URL; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function getBaseAccessTokenUrl(array $params) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->getBaseGraphUrl().$this->graphApiVersion.'/oauth/access_token'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get the base Graph API URL. |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
private function getBaseGraphUrl() |
|
91
|
|
|
{ |
|
92
|
|
|
return static::BASE_GRAPH_URL; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getDefaultScopes() |
|
96
|
|
|
{ |
|
97
|
|
|
return [ |
|
98
|
|
|
'public_profile', |
|
99
|
|
|
'email', |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
|
104
|
|
|
{ |
|
105
|
|
|
$fields = [ |
|
106
|
|
|
'id', |
|
107
|
|
|
'name', |
|
108
|
|
|
'first_name', |
|
109
|
|
|
'last_name', |
|
110
|
|
|
'email', |
|
111
|
|
|
'hometown', |
|
112
|
|
|
'picture.type(large){url,is_silhouette}', |
|
113
|
|
|
'cover{source}', |
|
114
|
|
|
'gender', |
|
115
|
|
|
'locale', |
|
116
|
|
|
'link', |
|
117
|
|
|
'timezone', |
|
118
|
|
|
'age_range', |
|
119
|
|
|
]; |
|
120
|
|
|
|
|
121
|
|
|
$appSecretProof = AppSecretProof::create($this->clientSecret, $token->getToken()); |
|
122
|
|
|
|
|
123
|
|
|
return $this->getBaseGraphUrl().$this->graphApiVersion.'/me?fields='.implode(',', $fields).'&access_token='.$token.'&appsecret_proof='.$appSecretProof; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getLongLivedAccessToken($accessToken) |
|
127
|
|
|
{ |
|
128
|
|
|
$params = [ |
|
129
|
|
|
'fb_exchange_token' => (string)$accessToken, |
|
130
|
|
|
]; |
|
131
|
|
|
|
|
132
|
|
|
return $this->getAccessToken('fb_exchange_token', $params); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public function getAccessToken($grant = 'authorization_code', array $params = [], array $attributes = []) |
|
136
|
|
|
{ |
|
137
|
|
|
if (isset($params['refresh_token'])) { |
|
138
|
|
|
throw new FacebookProviderException('error_facebook_token_refresh_not_supported'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return parent::getAccessToken($grant, $params, $attributes); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
protected function createResourceOwner(array $response, AccessToken $token) |
|
145
|
|
|
{ |
|
146
|
|
|
return new FacebookUser($response); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
protected function checkResponse(ResponseInterface $response, $data) |
|
150
|
|
|
{ |
|
151
|
|
|
if (!empty($data['error'])) { |
|
152
|
|
|
throw new IdentityProviderException('error_facebook_bad_response', 400, $response->getBody()); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @inheritdoc |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function getContentType(ResponseInterface $response) |
|
160
|
|
|
{ |
|
161
|
|
|
$type = parent::getContentType($response); |
|
162
|
|
|
|
|
163
|
|
|
// Fix for Facebook's pseudo-JSONP support |
|
164
|
|
|
if (strpos($type, 'javascript') !== false) { |
|
165
|
|
|
return 'application/json'; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
// Fix for Facebook's pseudo-urlencoded support |
|
169
|
|
|
if (strpos($type, 'plain') !== false) { |
|
170
|
|
|
return 'application/x-www-form-urlencoded'; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return $type; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
public function setState($state = null) |
|
177
|
|
|
{ |
|
178
|
|
|
$this->state = $state; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|