/extensions/adapter/security/auth/Facebook.php

https://github.com/foetus/li3_facebook · PHP · 150 lines · 64 code · 19 blank · 67 comment · 5 complexity · ea912733144b69941f725fcfa6bbd4e8 MD5 · raw file

  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace li3_facebook\extensions\adapter\security\auth;
  9. use li3_facebook\extensions\FacebookProxy;
  10. use lithium\security\Auth;
  11. use lithium\storage\Session;
  12. use lithium\core\Libraries;
  13. use \Exception;
  14. /**
  15. * Extends Lithium's Auth adapter to look for a Facebook session
  16. * and use that to set auth if available.
  17. *
  18. * @see lithium\security\Auth
  19. * @see li3_facebook\extensions\FacebookProxy
  20. */
  21. class Facebook extends \lithium\core\Object {
  22. /**
  23. * Called by the `Auth` class to run an authentication check against the Facebook API
  24. * and returns an array of user information on success, or `false` on failure.
  25. *
  26. * @todo move the FacebookConfig::checkConfiguration part into the __init?
  27. *
  28. * @throws lithium\core\ConfigException if the facebook App credentials arent set
  29. *
  30. * @param object $credentials A data container which wraps the authentication credentials used
  31. * to query the model (usually a `Request` object). See the documentation for this
  32. * class for further details.
  33. * @param array $options Options which include the options for session key names and also FB API method options.
  34. * @return array Returns an array containing user information on success, or `false` on failure.
  35. */
  36. public function check($credentials, array $options = array()) {
  37. FacebookProxy::checkConfiguration();
  38. //get Url
  39. $base = $credentials->env('HTTPS') ? 'https://' : 'http://';
  40. $base .= $credentials->env('HTTP_HOST');
  41. $base .= $credentials->env('base');
  42. $facebook_config = Libraries::get('li3_facebook');
  43. // get the options from the li3_facebook library configuration if set there
  44. $options += $facebook_config;
  45. // otherwise, set some defaults
  46. $defaults = array(
  47. 'logout_url_options' => array(
  48. 'next' => $base
  49. ),
  50. 'login_url_options' => array(
  51. ),
  52. 'logout_url_session_key' => 'fb_logout_url',
  53. 'login_url_session_key' => 'fb_login_url',
  54. 'local_fb_session_name' => 'fb_session'
  55. );
  56. /**
  57. * If the adapter config() has those keys set, then use those as the default values.
  58. * This allows various adapters to be created all which can change the options for logging in and out
  59. * for Facebook, so when Auth::check() is called, each check can be used for different reasons.
  60. * If the options are set with the Facebook library ($facebook_config) then there can only be one
  61. * "configuration" for these login and logout parameters.
  62. *
  63. * So for example, Auth::check('popup', $this->request); or Auth::check('page', $this->request);
  64. * The difference maybe between the two Auth configurations is the "login_url_options" array values
  65. * of "display" being "page" or "popup" which tells the FB API how to display the login.
  66. *
  67. * We could also pass these options in the configuration under Libraries::add('li3_facebook'), but then
  68. * it wouldn't be quite as easy to switch behaviors while using Auth::check();
  69. */
  70. $defaults['logout_url_options'] = (isset($this->_config['logout_url_options'])) ? $this->_config['logout_url_options']:$defaults['logout_url_options'];
  71. $defaults['login_url_options'] = (isset($this->_config['login_url_options'])) ? $this->_config['login_url_options']:$defaults['login_url_options'];
  72. $defaults['logout_url_session_key'] = (isset($this->_config['logout_url_session_key'])) ? $this->_config['logout_url_session_key']:$defaults['logout_url_session_key'];
  73. $defaults['login_url_session_key'] = (isset($this->_config['login_url_session_key'])) ? $this->_config['login_url_session_key']:$defaults['login_url_session_key'];
  74. $defaults['local_fb_session_name'] = (isset($this->_config['local_fb_session_name'])) ? $this->_config['local_fb_session_name']:$defaults['local_fb_session_name'];
  75. // combine the defults with the options passed, giving those passed options the priority
  76. $options += $defaults;
  77. $user_data = false;
  78. $session = FacebookProxy::getSession();
  79. $uid = null;
  80. // Session based API call.
  81. if ($session) {
  82. // Set the session locally
  83. Session::write($options['local_fb_session_name'], $session);
  84. try {
  85. $uid = FacebookProxy::getUser();
  86. } catch (Exception $e) {
  87. //error_log($e);
  88. }
  89. }
  90. // If $uid is set, then write the fb_logout_url session key
  91. if (!empty($uid)) {
  92. if($options['logout_url_session_key']) {
  93. Session::write($options['logout_url_session_key'], FacebookProxy::getLogoutUrl($options['logout_url_options']));
  94. }
  95. // Get the user data to return
  96. $user_data = array();
  97. try {
  98. $user_data = FacebookProxy::api('/me');
  99. } catch(Exception $e) {
  100. //error_log($e);
  101. }
  102. } else {
  103. // Else, the user hasn't logged in yet, write the fb_login_url session key
  104. if($options['login_url_session_key']) {
  105. Session::write($options['login_url_session_key'], FacebookProxy::getLoginUrl($options['login_url_options']));
  106. }
  107. }
  108. return $user_data;
  109. }
  110. /**
  111. * A pass-through method called by `Auth`. Returns the value of `$data`, which is written to
  112. * a user's session. When implementing a custom adapter, this method may be used to modify or
  113. * reject data before it is written to the session.
  114. *
  115. * @param array $data User data to be written to the session.
  116. * @param array $options Adapter-specific options. Not implemented in the `Facebook` adapter.
  117. * @return array Returns the value of `$data`.
  118. */
  119. public function set($data, array $options = array()) {
  120. return $data;
  121. }
  122. /**
  123. * Called by `Auth` when a user session is terminated. Not implemented in the `Facebook` adapter.
  124. *
  125. * @param array $options Adapter-specific options. Not implemented in the `Facebook` adapter.
  126. * @return void
  127. */
  128. public function clear(array $options = array()) {
  129. }
  130. }
  131. ?>