1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
<?php
/**
* Copyright 2017 Facebook, Inc.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
* use, copy, modify, and distribute this software in source code or binary
* form for use in connection with the web services and APIs provided by
* Facebook.
*
* As with any software that integrates with the Facebook platform, your use
* of this software is subject to the Facebook Developer Principles and
* Policies [http://developers.facebook.com/policy/]. This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
namespace Facebook\Authentication;
use Facebook\Facebook;
use Facebook\FacebookApp;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookClient;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
/**
* Class OAuth2Client
*
* @package Facebook
*/
class OAuth2Client
{
/**
* @const string The base authorization URL.
*/
const BASE_AUTHORIZATION_URL = 'https://www.facebook.com';
/**
* The FacebookApp entity.
*
* @var FacebookApp
*/
protected $app;
/**
* The Facebook client.
*
* @var FacebookClient
*/
protected $client;
/**
* The version of the Graph API to use.
*
* @var string
*/
protected $graphVersion;
/**
* The last request sent to Graph.
*
* @var FacebookRequest|null
*/
protected $lastRequest;
/**
* @param FacebookApp $app
* @param FacebookClient $client
* @param string|null $graphVersion The version of the Graph API to use.
*/
public function __construct(FacebookApp $app, FacebookClient $client, $graphVersion = null)
{
$this->app = $app;
$this->client = $client;
$this->graphVersion = $graphVersion ?: Facebook::DEFAULT_GRAPH_VERSION;
}
/**
* Returns the last FacebookRequest that was sent.
* Useful for debugging and testing.
*
* @return FacebookRequest|null
*/
public function getLastRequest()
{
return $this->lastRequest;
}
/**
* Get the metadata associated with the access token.
*
* @param AccessToken|string $accessToken The access token to debug.
*
* @return AccessTokenMetadata
*/
public function debugToken($accessToken)
{
$accessToken = $accessToken instanceof AccessToken ? $accessToken->getValue() : $accessToken;
$params = ['input_token' => $accessToken];
$this->lastRequest = new FacebookRequest(
$this->app,
$this->app->getAccessToken(),
'GET',
'/debug_token',
$params,
null,
$this->graphVersion
);
$response = $this->client->sendRequest($this->lastRequest);
$metadata = $response->getDecodedBody();
return new AccessTokenMetadata($metadata);
}
/**
* Generates an authorization URL to begin the process of authenticating a user.
*
* @param string $redirectUrl The callback URL to redirect to.
* @param string $state The CSPRNG-generated CSRF value.
* @param array $scope An array of permissions to request.
* @param array $params An array of parameters to generate URL.
* @param string $separator The separator to use in http_build_query().
*
* @return string
*/
public function getAuthorizationUrl($redirectUrl, $state, array $scope = [], array $params = [], $separator = '&')
{
$params += [
'client_id' => $this->app->getId(),
'state' => $state,
'response_type' => 'code',
'sdk' => 'php-sdk-' . Facebook::VERSION,
'redirect_uri' => $redirectUrl,
'scope' => implode(',', $scope)
];
return static::BASE_AUTHORIZATION_URL . '/' . $this->graphVersion . '/dialog/oauth?' . http_build_query($params, null, $separator);
}
/**
* Get a valid access token from a code.
*
* @param string $code
* @param string $redirectUri
*
* @return AccessToken
*
* @throws FacebookSDKException
*/
public function getAccessTokenFromCode($code, $redirectUri = '')
{
$params = [
'code' => $code,
'redirect_uri' => $redirectUri,
];
return $this->requestAnAccessToken($params);
}
/**
* Exchanges a short-lived access token with a long-lived access token.
*
* @param AccessToken|string $accessToken
*
* @return AccessToken
*
* @throws FacebookSDKException
*/
public function getLongLivedAccessToken($accessToken)
{
$accessToken = $accessToken instanceof AccessToken ? $accessToken->getValue() : $accessToken;
$params = [
'grant_type' => 'fb_exchange_token',
'fb_exchange_token' => $accessToken,
];
return $this->requestAnAccessToken($params);
}
/**
* Get a valid code from an access token.
*
* @param AccessToken|string $accessToken
* @param string $redirectUri
*
* @return AccessToken
*
* @throws FacebookSDKException
*/
public function getCodeFromLongLivedAccessToken($accessToken, $redirectUri = '')
{
$params = [
'redirect_uri' => $redirectUri,
];
$response = $this->sendRequestWithClientParams('/oauth/client_code', $params, $accessToken);
$data = $response->getDecodedBody();
if (!isset($data['code'])) {
throw new FacebookSDKException('Code was not returned from Graph.', 401);
}
return $data['code'];
}
/**
* Send a request to the OAuth endpoint.
*
* @param array $params
*
* @return AccessToken
*
* @throws FacebookSDKException
*/
protected function requestAnAccessToken(array $params)
{
$response = $this->sendRequestWithClientParams('/oauth/access_token', $params);
$data = $response->getDecodedBody();
if (!isset($data['access_token'])) {
throw new FacebookSDKException('Access token was not returned from Graph.', 401);
}
// Graph returns two different key names for expiration time
// on the same endpoint. Doh! :/
$expiresAt = 0;
if (isset($data['expires'])) {
// For exchanging a short lived token with a long lived token.
// The expiration time in seconds will be returned as "expires".
$expiresAt = time() + $data['expires'];
} elseif (isset($data['expires_in'])) {
// For exchanging a code for a short lived access token.
// The expiration time in seconds will be returned as "expires_in".
// See: https://developers.facebook.com/docs/facebook-login/access-tokens#long-via-code
$expiresAt = time() + $data['expires_in'];
}
return new AccessToken($data['access_token'], $expiresAt);
}
/**
* Send a request to Graph with an app access token.
*
* @param string $endpoint
* @param array $params
* @param AccessToken|string|null $accessToken
*
* @return FacebookResponse
*
* @throws FacebookResponseException
*/
protected function sendRequestWithClientParams($endpoint, array $params, $accessToken = null)
{
$params += $this->getClientParams();
$accessToken = $accessToken ?: $this->app->getAccessToken();
$this->lastRequest = new FacebookRequest(
$this->app,
$accessToken,
'GET',
$endpoint,
$params,
null,
$this->graphVersion
);
return $this->client->sendRequest($this->lastRequest);
}
/**
* Returns the client_* params for OAuth requests.
*
* @return array
*/
protected function getClientParams()
{
return [
'client_id' => $this->app->getId(),
'client_secret' => $this->app->getSecret(),
];
}
}
|