Cheetah
FacebookRedirectLoginHelper.php
Go to the documentation of this file.
1 <?php
24 namespace Facebook\Helpers;
25 
36 
43 {
47  const CSRF_LENGTH = 32;
48 
52  protected $oAuth2Client;
53 
58 
63 
68 
76  {
77  $this->oAuth2Client = $oAuth2Client;
78  $this->persistentDataHandler = $persistentDataHandler ?: new FacebookSessionPersistentDataHandler();
79  $this->urlDetectionHandler = $urlHandler ?: new FacebookUrlDetectionHandler();
80  $this->pseudoRandomStringGenerator = PseudoRandomStringGeneratorFactory::createPseudoRandomStringGenerator($prsg);
81  }
82 
88  public function getPersistentDataHandler()
89  {
91  }
92 
98  public function getUrlDetectionHandler()
99  {
101  }
102 
109  {
111  }
112 
123  private function makeUrl($redirectUrl, array $scope, array $params = [], $separator = '&')
124  {
125  $state = $this->persistentDataHandler->get('state') ?: $this->pseudoRandomStringGenerator->getPseudoRandomString(static::CSRF_LENGTH);
126  $this->persistentDataHandler->set('state', $state);
127 
128  return $this->oAuth2Client->getAuthorizationUrl($redirectUrl, $state, $scope, $params, $separator);
129  }
130 
140  public function getLoginUrl($redirectUrl, array $scope = [], $separator = '&')
141  {
142  return $this->makeUrl($redirectUrl, $scope, [], $separator);
143  }
144 
156  public function getLogoutUrl($accessToken, $next, $separator = '&')
157  {
158  if (!$accessToken instanceof AccessToken) {
159  $accessToken = new AccessToken($accessToken);
160  }
161 
162  if ($accessToken->isAppAccessToken()) {
163  throw new FacebookSDKException('Cannot generate a logout URL with an app access token.', 722);
164  }
165 
166  $params = [
167  'next' => $next,
168  'access_token' => $accessToken->getValue(),
169  ];
170 
171  return 'https://www.facebook.com/logout.php?' . http_build_query($params, null, $separator);
172  }
173 
183  public function getReRequestUrl($redirectUrl, array $scope = [], $separator = '&')
184  {
185  $params = ['auth_type' => 'rerequest'];
186 
187  return $this->makeUrl($redirectUrl, $scope, $params, $separator);
188  }
189 
199  public function getReAuthenticationUrl($redirectUrl, array $scope = [], $separator = '&')
200  {
201  $params = ['auth_type' => 'reauthenticate'];
202 
203  return $this->makeUrl($redirectUrl, $scope, $params, $separator);
204  }
205 
215  public function getAccessToken($redirectUrl = null)
216  {
217  if (!$code = $this->getCode()) {
218  return null;
219  }
220 
221  $this->validateCsrf();
222  $this->resetCsrf();
223 
224  $redirectUrl = $redirectUrl ?: $this->urlDetectionHandler->getCurrentUrl();
225  // At minimum we need to remove the 'state' and 'code' params
226  $redirectUrl = FacebookUrlManipulator::removeParamsFromUrl($redirectUrl, ['code', 'state']);
227 
228  return $this->oAuth2Client->getAccessTokenFromCode($code, $redirectUrl);
229  }
230 
236  protected function validateCsrf()
237  {
238  $state = $this->getState();
239  if (!$state) {
240  throw new FacebookSDKException('Cross-site request forgery validation failed. Required GET param "state" missing.');
241  }
242  $savedState = $this->persistentDataHandler->get('state');
243  if (!$savedState) {
244  throw new FacebookSDKException('Cross-site request forgery validation failed. Required param "state" missing from persistent data.');
245  }
246 
247  if (\hash_equals($savedState, $state)) {
248  return;
249  }
250 
251  throw new FacebookSDKException('Cross-site request forgery validation failed. The "state" param from the URL and session do not match.');
252  }
253 
257  private function resetCsrf()
258  {
259  $this->persistentDataHandler->set('state', null);
260  }
261 
267  protected function getCode()
268  {
269  return $this->getInput('code');
270  }
271 
277  protected function getState()
278  {
279  return $this->getInput('state');
280  }
281 
287  public function getErrorCode()
288  {
289  return $this->getInput('error_code');
290  }
291 
297  public function getError()
298  {
299  return $this->getInput('error');
300  }
301 
307  public function getErrorReason()
308  {
309  return $this->getInput('error_reason');
310  }
311 
317  public function getErrorDescription()
318  {
319  return $this->getInput('error_description');
320  }
321 
329  private function getInput($key)
330  {
331  return isset($_GET[$key]) ? $_GET[$key] : null;
332  }
333 }
Facebook\Helpers\FacebookRedirectLoginHelper\validateCsrf
validateCsrf()
Definition: FacebookRedirectLoginHelper.php:236
Facebook\Helpers\FacebookRedirectLoginHelper\$pseudoRandomStringGenerator
$pseudoRandomStringGenerator
Definition: FacebookRedirectLoginHelper.php:67
Facebook\Helpers\FacebookRedirectLoginHelper\getLogoutUrl
getLogoutUrl($accessToken, $next, $separator='&')
Definition: FacebookRedirectLoginHelper.php:156
Facebook\Url\UrlDetectionInterface
Definition: UrlDetectionInterface.php:32
Facebook\Helpers\FacebookRedirectLoginHelper\getLoginUrl
getLoginUrl($redirectUrl, array $scope=[], $separator='&')
Definition: FacebookRedirectLoginHelper.php:140
Facebook\PseudoRandomString\PseudoRandomStringGeneratorFactory
Definition: PseudoRandomStringGeneratorFactory.php:30
Facebook\Exceptions\FacebookSDKException
Definition: FacebookSDKException.php:32
Facebook\PseudoRandomString\PseudoRandomStringGeneratorFactory\createPseudoRandomStringGenerator
static createPseudoRandomStringGenerator($generator)
Definition: PseudoRandomStringGeneratorFactory.php:45
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
Facebook\PersistentData\FacebookSessionPersistentDataHandler
Definition: FacebookSessionPersistentDataHandler.php:34
php
Facebook\Helpers\FacebookRedirectLoginHelper\__construct
__construct(OAuth2Client $oAuth2Client, PersistentDataInterface $persistentDataHandler=null, UrlDetectionInterface $urlHandler=null, PseudoRandomStringGeneratorInterface $prsg=null)
Definition: FacebookRedirectLoginHelper.php:75
Facebook\Helpers\FacebookRedirectLoginHelper\getError
getError()
Definition: FacebookRedirectLoginHelper.php:297
Facebook\Authentication\AccessToken
Definition: AccessToken.php:32
Facebook\Helpers\FacebookRedirectLoginHelper\$persistentDataHandler
$persistentDataHandler
Definition: FacebookRedirectLoginHelper.php:62
Facebook\Url\FacebookUrlDetectionHandler
Definition: FacebookUrlDetectionHandler.php:32
Facebook\Helpers\FacebookRedirectLoginHelper
Definition: FacebookRedirectLoginHelper.php:43
Facebook\Helpers\FacebookRedirectLoginHelper\$oAuth2Client
$oAuth2Client
Definition: FacebookRedirectLoginHelper.php:52
Facebook\Helpers\FacebookRedirectLoginHelper\getErrorReason
getErrorReason()
Definition: FacebookRedirectLoginHelper.php:307
Facebook\Url\FacebookUrlManipulator
Definition: FacebookUrlManipulator.php:32
Facebook\Helpers\FacebookRedirectLoginHelper\getErrorDescription
getErrorDescription()
Definition: FacebookRedirectLoginHelper.php:317
$_GET
$_GET['debug']
Definition: index.php:67
Facebook\Helpers\FacebookRedirectLoginHelper\getCode
getCode()
Definition: FacebookRedirectLoginHelper.php:267
Facebook\Helpers\FacebookRedirectLoginHelper\$urlDetectionHandler
$urlDetectionHandler
Definition: FacebookRedirectLoginHelper.php:57
Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface
Definition: PseudoRandomStringGeneratorInterface.php:32
Facebook\Helpers\FacebookRedirectLoginHelper\getErrorCode
getErrorCode()
Definition: FacebookRedirectLoginHelper.php:287
Facebook\Helpers\FacebookRedirectLoginHelper\getReRequestUrl
getReRequestUrl($redirectUrl, array $scope=[], $separator='&')
Definition: FacebookRedirectLoginHelper.php:183
Facebook\Helpers\FacebookRedirectLoginHelper\getPseudoRandomStringGenerator
getPseudoRandomStringGenerator()
Definition: FacebookRedirectLoginHelper.php:108
Facebook\Authentication\OAuth2Client
Definition: OAuth2Client.php:40
Facebook\Helpers\FacebookRedirectLoginHelper\getState
getState()
Definition: FacebookRedirectLoginHelper.php:277
Facebook\Url\FacebookUrlManipulator\removeParamsFromUrl
static removeParamsFromUrl($url, array $paramsToFilter)
Definition: FacebookUrlManipulator.php:41
Facebook\PersistentData\PersistentDataInterface
Definition: PersistentDataInterface.php:32
Facebook\Helpers\FacebookRedirectLoginHelper\getAccessToken
getAccessToken($redirectUrl=null)
Definition: FacebookRedirectLoginHelper.php:215
Facebook\Helpers\FacebookRedirectLoginHelper\getPersistentDataHandler
getPersistentDataHandler()
Definition: FacebookRedirectLoginHelper.php:88
Facebook\Helpers\FacebookRedirectLoginHelper\getReAuthenticationUrl
getReAuthenticationUrl($redirectUrl, array $scope=[], $separator='&')
Definition: FacebookRedirectLoginHelper.php:199
Facebook\Helpers\FacebookRedirectLoginHelper\CSRF_LENGTH
const CSRF_LENGTH
Definition: FacebookRedirectLoginHelper.php:47
Facebook\Helpers\FacebookRedirectLoginHelper\getUrlDetectionHandler
getUrlDetectionHandler()
Definition: FacebookRedirectLoginHelper.php:98
Facebook\Helpers
Definition: FacebookCanvasHelper.php:24