From 951330c9f83c8c8ee98f65fdccb5797e2e59d1f3 Mon Sep 17 00:00:00 2001 From: BlueRaja Date: Sat, 1 Jun 2013 20:10:36 -0500 Subject: A partial commit of the auth stuff, in case my upcoming changes break anything --- .../HybridAuth/thirdparty/Facebook/facebook.php | 160 +++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 includes/HybridAuth/thirdparty/Facebook/facebook.php (limited to 'includes/HybridAuth/thirdparty/Facebook/facebook.php') diff --git a/includes/HybridAuth/thirdparty/Facebook/facebook.php b/includes/HybridAuth/thirdparty/Facebook/facebook.php new file mode 100644 index 0000000..a2238ef --- /dev/null +++ b/includes/HybridAuth/thirdparty/Facebook/facebook.php @@ -0,0 +1,160 @@ +initSharedSession(); + } + } + + protected static $kSupportedKeys = + array('state', 'code', 'access_token', 'user_id'); + + protected function initSharedSession() { + $cookie_name = $this->getSharedSessionCookieName(); + if (isset($_COOKIE[$cookie_name])) { + $data = $this->parseSignedRequest($_COOKIE[$cookie_name]); + if ($data && !empty($data['domain']) && + self::isAllowedDomain($this->getHttpHost(), $data['domain'])) { + // good case + $this->sharedSessionID = $data['id']; + return; + } + // ignoring potentially unreachable data + } + // evil/corrupt/missing case + $base_domain = $this->getBaseDomain(); + $this->sharedSessionID = md5(uniqid(mt_rand(), true)); + $cookie_value = $this->makeSignedRequest( + array( + 'domain' => $base_domain, + 'id' => $this->sharedSessionID, + ) + ); + $_COOKIE[$cookie_name] = $cookie_value; + if (!headers_sent()) { + $expire = time() + self::FBSS_COOKIE_EXPIRE; + setcookie($cookie_name, $cookie_value, $expire, '/', '.'.$base_domain); + } else { + // @codeCoverageIgnoreStart + self::errorLog( + 'Shared session ID cookie could not be set! You must ensure you '. + 'create the Facebook instance before headers have been sent. This '. + 'will cause authentication issues after the first request.' + ); + // @codeCoverageIgnoreEnd + } + } + + /** + * 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. + */ + protected function setPersistentData($key, $value) { + if (!in_array($key, self::$kSupportedKeys)) { + self::errorLog('Unsupported key passed to setPersistentData.'); + return; + } + + $session_var_name = $this->constructSessionVariableName($key); + $_SESSION[$session_var_name] = $value; + } + + protected function getPersistentData($key, $default = false) { + if (!in_array($key, self::$kSupportedKeys)) { + self::errorLog('Unsupported key passed to getPersistentData.'); + return $default; + } + + $session_var_name = $this->constructSessionVariableName($key); + return isset($_SESSION[$session_var_name]) ? + $_SESSION[$session_var_name] : $default; + } + + protected function clearPersistentData($key) { + if (!in_array($key, self::$kSupportedKeys)) { + self::errorLog('Unsupported key passed to clearPersistentData.'); + return; + } + + $session_var_name = $this->constructSessionVariableName($key); + unset($_SESSION[$session_var_name]); + } + + protected function clearAllPersistentData() { + foreach (self::$kSupportedKeys as $key) { + $this->clearPersistentData($key); + } + if ($this->sharedSessionID) { + $this->deleteSharedSessionCookie(); + } + } + + protected function deleteSharedSessionCookie() { + $cookie_name = $this->getSharedSessionCookieName(); + unset($_COOKIE[$cookie_name]); + $base_domain = $this->getBaseDomain(); + setcookie($cookie_name, '', 1, '/', '.'.$base_domain); + } + + protected function getSharedSessionCookieName() { + return self::FBSS_COOKIE_NAME . '_' . $this->getAppId(); + } + + protected function constructSessionVariableName($key) { + $parts = array('fb', $this->getAppId(), $key); + if ($this->sharedSessionID) { + array_unshift($parts, $this->sharedSessionID); + } + return implode('_', $parts); + } +} -- cgit v1.2.3