ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
sspmod_authfacebook_Facebook Class Reference

Extends the BaseFacebook class with the intent of using PHP sessions to store user ids and access tokens. More...

+ Inheritance diagram for sspmod_authfacebook_Facebook:
+ Collaboration diagram for sspmod_authfacebook_Facebook:

Public Member Functions

 __construct (array $config, &$ssp_state)
 Identical to the parent constructor, except that we start a PHP session to store the user ID and access token if during the course of execution we discover them. More...
 
- Public Member Functions inherited from BaseFacebook
 __construct ($config)
 Initialize a Facebook Application. More...
 
 setAppId ($appId)
 Set the Application ID. More...
 
 getAppId ()
 Get the Application ID. More...
 
 setApiSecret ($apiSecret)
 Set the App Secret. More...
 
 setAppSecret ($appSecret)
 Set the App Secret. More...
 
 getApiSecret ()
 Get the App Secret. More...
 
 getAppSecret ()
 Get the App Secret. More...
 
 setFileUploadSupport ($fileUploadSupport)
 Set the file upload support status. More...
 
 getFileUploadSupport ()
 Get the file upload support status. More...
 
 useFileUploadSupport ()
 DEPRECATED! Please use getFileUploadSupport instead. More...
 
 setAccessToken ($access_token)
 Sets the access token for api calls. More...
 
 setExtendedAccessToken ()
 Extend an access token, while removing the short-lived token that might have been generated via client-side flow. More...
 
 getAccessToken ()
 Determines the access token that should be used for API calls. More...
 
 getSignedRequest ()
 Retrieve the signed request, either from a request parameter or, if not present, from a cookie. More...
 
 getUser ()
 Get the UID of the connected user, or 0 if the Facebook user is not connected. More...
 
 getLoginUrl ($params=array())
 Get a Login URL for use with redirects. More...
 
 getLogoutUrl ($params=array())
 Get a Logout URL suitable for use with redirects. More...
 
 getLoginStatusUrl ($params=array())
 Get a login status URL to fetch the status from Facebook. More...
 
 api ()
 Make an API call. More...
 
 destroySession ()
 Destroy the current session. More...
 

Data Fields

const FBSS_COOKIE_NAME = 'fbss'
 
const FBSS_COOKIE_EXPIRE = 31556926
 
- Data Fields inherited from BaseFacebook
const VERSION = '3.2.2'
 Version. More...
 
const SIGNED_REQUEST_ALGORITHM = 'HMAC-SHA256'
 Signed Request Algorithm. More...
 

Protected Member Functions

 initSharedSession ()
 
 setPersistentData ($key, $value)
 Provides the implementations of the inherited abstract methods. More...
 
 getPersistentData ($key, $default=false)
 
 clearPersistentData ($key)
 
 clearAllPersistentData ()
 
 deleteSharedSessionCookie ()
 
 getSharedSessionCookieName ()
 
 constructSessionVariableName ($key)
 
 establishCSRFTokenState ()
 
- Protected Member Functions inherited from BaseFacebook
 getUserAccessToken ()
 Determines and returns the user access token, first using the signed request if present, and then falling back on the authorization code if present. More...
 
 getUserFromAvailableData ()
 Determines the connected user by first examining any signed requests, then considering an authorization code, and then falling back to any persistent store storing the user. More...
 
 getSignedRequestCookieName ()
 Constructs and returns the name of the cookie that potentially houses the signed request for the app user. More...
 
 getMetadataCookieName ()
 Constructs and returns the name of the coookie that potentially contain metadata. More...
 
 getCode ()
 Get the authorization code from the query parameters, if it exists, and otherwise return false to signal no authorization code was discoverable. More...
 
 getUserFromAccessToken ()
 Retrieves the UID with the understanding that $this->accessToken has already been set and is seemingly legitimate. More...
 
 getApplicationAccessToken ()
 Returns the access token that should be used for logged out users when no authorization code is available. More...
 
 establishCSRFTokenState ()
 Lays down a CSRF state token for this process. More...
 
 getAccessTokenFromCode ($code, $redirect_uri=null)
 Retrieves an access token for the given authorization code (previously generated from www.facebook.com on behalf of a specific user). More...
 
 _restserver ($params)
 Invoke the old restserver.php endpoint. More...
 
 isVideoPost ($path, $method='GET')
 Return true if this is video post. More...
 
 _graph ($path, $method='GET', $params=array())
 Invoke the Graph API. More...
 
 _oauthRequest ($url, $params)
 Make a OAuth Request. More...
 
 makeRequest ($url, $params, $ch=null)
 Makes an HTTP request. More...
 
 parseSignedRequest ($signed_request)
 Parses a signed_request and validates the signature. More...
 
 makeSignedRequest ($data)
 Makes a signed_request blob using the given data. More...
 
 getApiUrl ($method)
 Build the URL for api given parameters. More...
 
 getUrl ($name, $path='', $params=array())
 Build the URL for given domain alias, path and parameters. More...
 
 getHttpHost ()
 
 getHttpProtocol ()
 
 getBaseDomain ()
 Get the base domain used for the cookie. More...
 
 getCurrentUrl ()
 Returns the Current URL, stripping it of known FB parameters that should not persist. More...
 
 throwAPIException ($result)
 Analyzes the supplied result to see if it was thrown because the access token is no longer valid. More...
 
 getMetadataCookie ()
 Parses the metadata cookie that our Javascript API set. More...
 
 setPersistentData ($key, $value)
 Each of the following four methods should be overridden in a concrete subclass, as they are in the provided Facebook class. More...
 
 getPersistentData ($key, $default=false)
 Get the data for $key, persisted by BaseFacebook::setPersistentData() More...
 
 clearPersistentData ($key)
 Clear the data with $key from the persistent storage. More...
 
 clearAllPersistentData ()
 Clear all data from the persistent storage. More...
 

Protected Attributes

 $sharedSessionID
 
 $ssp_state
 
- Protected Attributes inherited from BaseFacebook
 $appId
 
 $appSecret
 
 $user
 
 $signedRequest
 The data from the signed_request token. More...
 
 $state
 A CSRF state variable to assist in the defense against CSRF attacks. More...
 
 $accessToken = null
 
 $fileUploadSupport = false
 
 $trustForwarded = false
 

Static Protected Attributes

static $kSupportedKeys
 

Additional Inherited Members

- Static Public Attributes inherited from BaseFacebook
static $CURL_OPTS
 Default options for curl. More...
 
static $DOMAIN_MAP
 Maps aliases to Facebook domains. More...
 
- Static Protected Member Functions inherited from BaseFacebook
static errorLog ($msg)
 Prints to the error log if you aren't in command line mode. More...
 
static base64UrlDecode ($input)
 Base64 encoding that doesn't need to be urlencode()ed. More...
 
static base64UrlEncode ($input)
 Base64 encoding that doesn't need to be urlencode()ed. More...
 
static isAllowedDomain ($big, $small)
 
static endsWith ($big, $small)
 

Detailed Description

Extends the BaseFacebook class with the intent of using PHP sessions to store user ids and access tokens.

Definition at line 9 of file Facebook.php.

Constructor & Destructor Documentation

◆ __construct()

sspmod_authfacebook_Facebook::__construct ( array  $config,
$ssp_state 
)

Identical to the parent constructor, except that we start a PHP session to store the user ID and access token if during the course of execution we discover them.

Parameters
Array$configthe application configuration. Additionally accepts "sharedSession" as a boolean to turn on a secondary cookie for environments with a shared session (that is, your app shares the domain with other apps).
See also
BaseFacebook::__construct in base_facebook.php

Definition at line 35 of file Facebook.php.

References $ssp_state, and initSharedSession().

35  {
36  $this->ssp_state = &$ssp_state;
37 
38  parent::__construct($config);
39  if (!empty($config['sharedSession'])) {
40  $this->initSharedSession();
41  }
42  }
$config
Definition: bootstrap.php:15
+ Here is the call graph for this function:

Member Function Documentation

◆ clearAllPersistentData()

sspmod_authfacebook_Facebook::clearAllPersistentData ( )
protected

Definition at line 122 of file Facebook.php.

References $key, clearPersistentData(), and deleteSharedSessionCookie().

122  {
123  foreach (self::$kSupportedKeys as $key) {
124  $this->clearPersistentData($key);
125  }
126  if ($this->sharedSessionID) {
127  $this->deleteSharedSessionCookie();
128  }
129  }
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:

◆ clearPersistentData()

sspmod_authfacebook_Facebook::clearPersistentData (   $key)
protected

Definition at line 110 of file Facebook.php.

References $key, constructSessionVariableName(), and SimpleSAML\Logger\debug().

Referenced by clearAllPersistentData().

110  {
111  if (!in_array($key, self::$kSupportedKeys)) {
112  SimpleSAML\Logger::debug("Unsupported key passed to clearPersistentData: " . var_export($key, TRUE));
113  return;
114  }
115 
116  $session_var_name = $this->constructSessionVariableName($key);
117  if (isset($this->ssp_state[$session_var_name])) {
118  unset($this->ssp_state[$session_var_name]);
119  }
120  }
static debug($string)
Definition: Logger.php:211
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ constructSessionVariableName()

sspmod_authfacebook_Facebook::constructSessionVariableName (   $key)
protected

Definition at line 142 of file Facebook.php.

References $key, and BaseFacebook\getAppId().

Referenced by clearPersistentData(), getPersistentData(), and setPersistentData().

142  {
143  $parts = array('authfacebook:authdata:fb', $this->getAppId(), $key);
144  if ($this->sharedSessionID) {
145  array_unshift($parts, $this->sharedSessionID);
146  }
147  return implode('_', $parts);
148  }
getAppId()
Get the Application ID.
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ deleteSharedSessionCookie()

sspmod_authfacebook_Facebook::deleteSharedSessionCookie ( )
protected

Definition at line 131 of file Facebook.php.

References $_COOKIE, BaseFacebook\getBaseDomain(), and getSharedSessionCookieName().

Referenced by clearAllPersistentData().

131  {
132  $cookie_name = $this->getSharedSessionCookieName();
133  unset($_COOKIE[$cookie_name]);
134  $base_domain = $this->getBaseDomain();
135  setcookie($cookie_name, '', 1, '/', '.'.$base_domain);
136  }
getBaseDomain()
Get the base domain used for the cookie.
$_COOKIE['client_id']
Definition: server.php:9
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ establishCSRFTokenState()

sspmod_authfacebook_Facebook::establishCSRFTokenState ( )
protected

Definition at line 150 of file Facebook.php.

References SimpleSAML_Auth_State\getStateId(), and setPersistentData().

150  {
151  if ($this->state === null) {
152  $this->state = SimpleSAML_Auth_State::getStateId($this->ssp_state);
153  $this->setPersistentData('state', $this->state);
154  }
155  }
static getStateId(&$state, $rawId=false)
Retrieve the ID of a state array.
Definition: State.php:145
setPersistentData($key, $value)
Provides the implementations of the inherited abstract methods.
Definition: Facebook.php:89
+ Here is the call graph for this function:

◆ getPersistentData()

sspmod_authfacebook_Facebook::getPersistentData (   $key,
  $default = false 
)
protected

Definition at line 99 of file Facebook.php.

References $default, $key, constructSessionVariableName(), and SimpleSAML\Logger\debug().

99  {
100  if (!in_array($key, self::$kSupportedKeys)) {
101  SimpleSAML\Logger::debug("Unsupported key passed to getPersistentData: " . var_export($key, TRUE));
102  return $default;
103  }
104 
105  $session_var_name = $this->constructSessionVariableName($key);
106  return isset($this->ssp_state[$session_var_name]) ?
107  $this->ssp_state[$session_var_name] : $default;
108  }
static debug($string)
Definition: Logger.php:211
$default
Definition: build.php:20
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:

◆ getSharedSessionCookieName()

sspmod_authfacebook_Facebook::getSharedSessionCookieName ( )
protected

Definition at line 138 of file Facebook.php.

References BaseFacebook\getAppId().

Referenced by deleteSharedSessionCookie(), and initSharedSession().

138  {
139  return self::FBSS_COOKIE_NAME . '_' . $this->getAppId();
140  }
getAppId()
Get the Application ID.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initSharedSession()

sspmod_authfacebook_Facebook::initSharedSession ( )
protected

Definition at line 47 of file Facebook.php.

References $_COOKIE, $data, $expire, SimpleSAML\Logger\debug(), BaseFacebook\getBaseDomain(), BaseFacebook\getHttpHost(), getSharedSessionCookieName(), BaseFacebook\makeSignedRequest(), and BaseFacebook\parseSignedRequest().

Referenced by __construct().

47  {
48  $cookie_name = $this->getSharedSessionCookieName();
49  if (isset($_COOKIE[$cookie_name])) {
50  $data = $this->parseSignedRequest($_COOKIE[$cookie_name]);
51  if (!empty($data) && !empty($data['domain']) &&
52  self::isAllowedDomain($this->getHttpHost(), $data['domain'])) {
53  // good case
54  $this->sharedSessionID = $data['id'];
55  return;
56  }
57  // ignoring potentially unreachable data
58  }
59  // evil/corrupt/missing case
60  $base_domain = $this->getBaseDomain();
61  $this->sharedSessionID = md5(uniqid(mt_rand(), true));
62  $cookie_value = $this->makeSignedRequest(
63  array(
64  'domain' => $base_domain,
65  'id' => $this->sharedSessionID,
66  )
67  );
68  $_COOKIE[$cookie_name] = $cookie_value;
69  if (!headers_sent()) {
70  $expire = time() + self::FBSS_COOKIE_EXPIRE;
71  setcookie($cookie_name, $cookie_value, $expire, '/', '.'.$base_domain);
72  } else {
73  // @codeCoverageIgnoreStart
75  'Shared session ID cookie could not be set! You must ensure you '.
76  'create the Facebook instance before headers have been sent. This '.
77  'will cause authentication issues after the first request.'
78  );
79  // @codeCoverageIgnoreEnd
80  }
81  }
$expire
Definition: saml2-acs.php:140
getBaseDomain()
Get the base domain used for the cookie.
$_COOKIE['client_id']
Definition: server.php:9
static debug($string)
Definition: Logger.php:211
parseSignedRequest($signed_request)
Parses a signed_request and validates the signature.
makeSignedRequest($data)
Makes a signed_request blob using the given data.
$data
Definition: bench.php:6
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setPersistentData()

sspmod_authfacebook_Facebook::setPersistentData (   $key,
  $value 
)
protected

Provides the implementations of the inherited abstract methods.

The implementation uses PHP sessions to maintain a store for authorization codes, user ids, CSRF states, and access tokens.

Definition at line 89 of file Facebook.php.

References $key, constructSessionVariableName(), and SimpleSAML\Logger\debug().

Referenced by establishCSRFTokenState().

89  {
90  if (!in_array($key, self::$kSupportedKeys)) {
91  SimpleSAML\Logger::debug("Unsupported key passed to setPersistentData: " . var_export($key, TRUE));
92  return;
93  }
94 
95  $session_var_name = $this->constructSessionVariableName($key);
96  $this->ssp_state[$session_var_name] = $value;
97  }
static debug($string)
Definition: Logger.php:211
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Field Documentation

◆ $kSupportedKeys

sspmod_authfacebook_Facebook::$kSupportedKeys
staticprotected
Initial value:
=
array('state', 'code', 'access_token', 'user_id')

Definition at line 44 of file Facebook.php.

◆ $sharedSessionID

sspmod_authfacebook_Facebook::$sharedSessionID
protected

Definition at line 18 of file Facebook.php.

◆ $ssp_state

sspmod_authfacebook_Facebook::$ssp_state
protected

Definition at line 21 of file Facebook.php.

Referenced by __construct().

◆ FBSS_COOKIE_EXPIRE

const sspmod_authfacebook_Facebook::FBSS_COOKIE_EXPIRE = 31556926

Definition at line 15 of file Facebook.php.

◆ FBSS_COOKIE_NAME

const sspmod_authfacebook_Facebook::FBSS_COOKIE_NAME = 'fbss'

Definition at line 11 of file Facebook.php.


The documentation for this class was generated from the following file: