[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/openid/Auth/Yadis/ -> Manager.php (source)

   1  <?php
   2  
   3  /**
   4   * Yadis service manager to be used during yadis-driven authentication
   5   * attempts.
   6   *
   7   * @package OpenID
   8   */
   9  
  10  // Do not allow direct access
  11  defined( '_JEXEC' ) or die( 'Restricted access' );
  12  
  13  /**
  14   * The base session class used by the Auth_Yadis_Manager.  This
  15   * class wraps the default PHP session machinery and should be
  16   * subclassed if your application doesn't use PHP sessioning.
  17   *
  18   * @package OpenID
  19   */
  20  class Auth_Yadis_PHPSession {
  21      /**
  22       * Set a session key/value pair.
  23       *
  24       * @param string $name The name of the session key to add.
  25       * @param string $value The value to add to the session.
  26       */
  27      function set($name, $value)
  28      {
  29          $_SESSION[$name] = $value;
  30      }
  31  
  32      /**
  33       * Get a key's value from the session.
  34       *
  35       * @param string $name The name of the key to retrieve.
  36       * @param string $default The optional value to return if the key
  37       * is not found in the session.
  38       * @return string $result The key's value in the session or
  39       * $default if it isn't found.
  40       */
  41      function get($name, $default=null)
  42      {
  43          if (array_key_exists($name, $_SESSION)) {
  44              return $_SESSION[$name];
  45          } else {
  46              return $default;
  47          }
  48      }
  49  
  50      /**
  51       * Remove a key/value pair from the session.
  52       *
  53       * @param string $name The name of the key to remove.
  54       */
  55      function del($name)
  56      {
  57          unset($_SESSION[$name]);
  58      }
  59  
  60      /**
  61       * Return the contents of the session in array form.
  62       */
  63      function contents()
  64      {
  65          return $_SESSION;
  66      }
  67  }
  68  
  69  /**
  70   * A session helper class designed to translate between arrays and
  71   * objects.  Note that the class used must have a constructor that
  72   * takes no parameters.  This is not a general solution, but it works
  73   * for dumb objects that just need to have attributes set.  The idea
  74   * is that you'll subclass this and override $this->check($data) ->
  75   * bool to implement your own session data validation.
  76   *
  77   * @package OpenID
  78   */
  79  class Auth_Yadis_SessionLoader {
  80      /**
  81       * Override this.
  82       *
  83       * @access private
  84       */
  85      function check($data)
  86      {
  87          return true;
  88      }
  89  
  90      /**
  91       * Given a session data value (an array), this creates an object
  92       * (returned by $this->newObject()) whose attributes and values
  93       * are those in $data.  Returns null if $data lacks keys found in
  94       * $this->requiredKeys().  Returns null if $this->check($data)
  95       * evaluates to false.  Returns null if $this->newObject()
  96       * evaluates to false.
  97       *
  98       * @access private
  99       */
 100      function fromSession($data)
 101      {
 102          if (!$data) {
 103              return null;
 104          }
 105  
 106          $required = $this->requiredKeys();
 107  
 108          foreach ($required as $k) {
 109              if (!array_key_exists($k, $data)) {
 110                  return null;
 111              }
 112          }
 113  
 114          if (!$this->check($data)) {
 115              return null;
 116          }
 117  
 118          $data = array_merge($data, $this->prepareForLoad($data));
 119          $obj = $this->newObject($data);
 120  
 121          if (!$obj) {
 122              return null;
 123          }
 124  
 125          foreach ($required as $k) {
 126              $obj->$k = $data[$k];
 127          }
 128  
 129          return $obj;
 130      }
 131  
 132      /**
 133       * Prepares the data array by making any necessary changes.
 134       * Returns an array whose keys and values will be used to update
 135       * the original data array before calling $this->newObject($data).
 136       *
 137       * @access private
 138       */
 139      function prepareForLoad($data)
 140      {
 141          return array();
 142      }
 143  
 144      /**
 145       * Returns a new instance of this loader's class, using the
 146       * session data to construct it if necessary.  The object need
 147       * only be created; $this->fromSession() will take care of setting
 148       * the object's attributes.
 149       *
 150       * @access private
 151       */
 152      function newObject($data)
 153      {
 154          return null;
 155      }
 156  
 157      /**
 158       * Returns an array of keys and values built from the attributes
 159       * of $obj.  If $this->prepareForSave($obj) returns an array, its keys
 160       * and values are used to update the $data array of attributes
 161       * from $obj.
 162       *
 163       * @access private
 164       */
 165      function toSession($obj)
 166      {
 167          $data = array();
 168          foreach ($obj as $k => $v) {
 169              $data[$k] = $v;
 170          }
 171  
 172          $extra = $this->prepareForSave($obj);
 173  
 174          if ($extra && is_array($extra)) {
 175              foreach ($extra as $k => $v) {
 176                  $data[$k] = $v;
 177              }
 178          }
 179  
 180          return $data;
 181      }
 182  
 183      /**
 184       * Override this.
 185       *
 186       * @access private
 187       */
 188      function prepareForSave($obj)
 189      {
 190          return array();
 191      }
 192  }
 193  
 194  /**
 195   * A concrete loader implementation for Auth_OpenID_ServiceEndpoints.
 196   *
 197   * @package OpenID
 198   */
 199  class Auth_OpenID_ServiceEndpointLoader extends Auth_Yadis_SessionLoader {
 200      function newObject($data)
 201      {
 202          return new Auth_OpenID_ServiceEndpoint();
 203      }
 204  
 205      function requiredKeys()
 206      {
 207          $obj = new Auth_OpenID_ServiceEndpoint();
 208          $data = array();
 209          foreach ($obj as $k => $v) {
 210              $data[] = $k;
 211          }
 212          return $data;
 213      }
 214  
 215      function check($data)
 216      {
 217          return is_array($data['type_uris']);
 218      }
 219  }
 220  
 221  /**
 222   * A concrete loader implementation for Auth_Yadis_Managers.
 223   *
 224   * @package OpenID
 225   */
 226  class Auth_Yadis_ManagerLoader extends Auth_Yadis_SessionLoader {
 227      function requiredKeys()
 228      {
 229          return array('starting_url',
 230                       'yadis_url',
 231                       'services',
 232                       'session_key',
 233                       '_current',
 234                       'stale');
 235      }
 236  
 237      function newObject($data)
 238      {
 239          return new Auth_Yadis_Manager($data['starting_url'],
 240                                            $data['yadis_url'],
 241                                            $data['services'],
 242                                            $data['session_key']);
 243      }
 244  
 245      function check($data)
 246      {
 247          return is_array($data['services']);
 248      }
 249  
 250      function prepareForLoad($data)
 251      {
 252          $loader = new Auth_OpenID_ServiceEndpointLoader();
 253          $services = array();
 254          foreach ($data['services'] as $s) {
 255              $services[] = $loader->fromSession($s);
 256          }
 257          return array('services' => $services);
 258      }
 259  
 260      function prepareForSave($obj)
 261      {
 262          $loader = new Auth_OpenID_ServiceEndpointLoader();
 263          $services = array();
 264          foreach ($obj->services as $s) {
 265              $services[] = $loader->toSession($s);
 266          }
 267          return array('services' => $services);
 268      }
 269  }
 270  
 271  /**
 272   * The Yadis service manager which stores state in a session and
 273   * iterates over <Service> elements in a Yadis XRDS document and lets
 274   * a caller attempt to use each one.  This is used by the Yadis
 275   * library internally.
 276   *
 277   * @package OpenID
 278   */
 279  class Auth_Yadis_Manager {
 280  
 281      /**
 282       * Intialize a new yadis service manager.
 283       *
 284       * @access private
 285       */
 286      function Auth_Yadis_Manager($starting_url, $yadis_url,
 287                                      $services, $session_key)
 288      {
 289          // The URL that was used to initiate the Yadis protocol
 290          $this->starting_url = $starting_url;
 291  
 292          // The URL after following redirects (the identifier)
 293          $this->yadis_url = $yadis_url;
 294  
 295          // List of service elements
 296          $this->services = $services;
 297  
 298          $this->session_key = $session_key;
 299  
 300          // Reference to the current service object
 301          $this->_current = null;
 302  
 303          // Stale flag for cleanup if PHP lib has trouble.
 304          $this->stale = false;
 305      }
 306  
 307      /**
 308       * @access private
 309       */
 310      function length()
 311      {
 312          // How many untried services remain?
 313          return count($this->services);
 314      }
 315  
 316      /**
 317       * Return the next service
 318       *
 319       * $this->current() will continue to return that service until the
 320       * next call to this method.
 321       */
 322      function nextService()
 323      {
 324  
 325          if ($this->services) {
 326              $this->_current = array_shift($this->services);
 327          } else {
 328              $this->_current = null;
 329          }
 330  
 331          return $this->_current;
 332      }
 333  
 334      /**
 335       * @access private
 336       */
 337      function current()
 338      {
 339          // Return the current service.
 340          // Returns None if there are no services left.
 341          return $this->_current;
 342      }
 343  
 344      /**
 345       * @access private
 346       */
 347      function forURL($url)
 348      {
 349          return in_array($url, array($this->starting_url, $this->yadis_url));
 350      }
 351  
 352      /**
 353       * @access private
 354       */
 355      function started()
 356      {
 357          // Has the first service been returned?
 358          return $this->_current !== null;
 359      }
 360  }
 361  
 362  /**
 363   * State management for discovery.
 364   *
 365   * High-level usage pattern is to call .getNextService(discover) in
 366   * order to find the next available service for this user for this
 367   * session. Once a request completes, call .cleanup() to clean up the
 368   * session state.
 369   *
 370   * @package OpenID
 371   */
 372  class Auth_Yadis_Discovery {
 373  
 374      /**
 375       * @access private
 376       */
 377      var $DEFAULT_SUFFIX = 'auth';
 378  
 379      /**
 380       * @access private
 381       */
 382      var $PREFIX = '_yadis_services_';
 383  
 384      /**
 385       * Initialize a discovery object.
 386       *
 387       * @param Auth_Yadis_PHPSession $session An object which
 388       * implements the Auth_Yadis_PHPSession API.
 389       * @param string $url The URL on which to attempt discovery.
 390       * @param string $session_key_suffix The optional session key
 391       * suffix override.
 392       */
 393      function Auth_Yadis_Discovery(&$session, $url,
 394                                        $session_key_suffix = null)
 395      {
 396          /// Initialize a discovery object
 397          $this->session =& $session;
 398          $this->url = $url;
 399          if ($session_key_suffix === null) {
 400              $session_key_suffix = $this->DEFAULT_SUFFIX;
 401          }
 402  
 403          $this->session_key_suffix = $session_key_suffix;
 404          $this->session_key = $this->PREFIX . $this->session_key_suffix;
 405      }
 406  
 407      /**
 408       * Return the next authentication service for the pair of
 409       * user_input and session. This function handles fallback.
 410       */
 411      function getNextService($discover_cb, &$fetcher)
 412      {
 413          $manager = $this->getManager();
 414          if (!$manager || (!$manager->services)) {
 415              $this->destroyManager();
 416  
 417              list($yadis_url, $services) = call_user_func($discover_cb,
 418                                                           $this->url,
 419                                                           $fetcher);
 420  
 421              $manager = $this->createManager($services, $yadis_url);
 422          }
 423  
 424          if ($manager) {
 425              $loader = new Auth_Yadis_ManagerLoader();
 426              $service = $manager->nextService();
 427              $this->session->set($this->session_key,
 428                                  serialize($loader->toSession($manager)));
 429          } else {
 430              $service = null;
 431          }
 432  
 433          return $service;
 434      }
 435  
 436      /**
 437       * Clean up Yadis-related services in the session and return the
 438       * most-recently-attempted service from the manager, if one
 439       * exists.
 440       *
 441       * @param $force True if the manager should be deleted regardless
 442       * of whether it's a manager for $this->url.
 443       */
 444      function cleanup($force=false)
 445      {
 446          $manager = $this->getManager($force);
 447          if ($manager) {
 448              $service = $manager->current();
 449              $this->destroyManager($force);
 450          } else {
 451              $service = null;
 452          }
 453  
 454          return $service;
 455      }
 456  
 457      /**
 458       * @access private
 459       */
 460      function getSessionKey()
 461      {
 462          // Get the session key for this starting URL and suffix
 463          return $this->PREFIX . $this->session_key_suffix;
 464      }
 465  
 466      /**
 467       * @access private
 468       *
 469       * @param $force True if the manager should be returned regardless
 470       * of whether it's a manager for $this->url.
 471       */
 472      function &getManager($force=false)
 473      {
 474          // Extract the YadisServiceManager for this object's URL and
 475          // suffix from the session.
 476  
 477          $manager_str = $this->session->get($this->getSessionKey());
 478          $manager = null;
 479  
 480          if ($manager_str !== null) {
 481              $loader = new Auth_Yadis_ManagerLoader();
 482              $manager = $loader->fromSession(unserialize($manager_str));
 483          }
 484  
 485          if ($manager && ($manager->forURL($this->url) || $force)) {
 486              return $manager;
 487          } else {
 488              $unused = null;
 489              return $unused;
 490          }
 491      }
 492  
 493      /**
 494       * @access private
 495       */
 496      function &createManager($services, $yadis_url = null)
 497      {
 498          $key = $this->getSessionKey();
 499          if ($this->getManager()) {
 500              return $this->getManager();
 501          }
 502  
 503          if ($services) {
 504              $loader = new Auth_Yadis_ManagerLoader();
 505              $manager = new Auth_Yadis_Manager($this->url, $yadis_url,
 506                                                $services, $key);
 507              $this->session->set($this->session_key,
 508                                  serialize($loader->toSession($manager)));
 509              return $manager;
 510          } else {
 511              // Oh, PHP.
 512              $unused = null;
 513              return $unused;
 514          }
 515      }
 516  
 517      /**
 518       * @access private
 519       *
 520       * @param $force True if the manager should be deleted regardless
 521       * of whether it's a manager for $this->url.
 522       */
 523      function destroyManager($force=false)
 524      {
 525          if ($this->getManager($force) !== null) {
 526              $key = $this->getSessionKey();
 527              $this->session->del($key);
 528          }
 529      }
 530  }
 531  
 532  ?>


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1