Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Yadis

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /openid/Services/Yadis/Manager.php

Documentation is available at Manager.php

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

Documentation generated on Mon, 05 Mar 2007 21:10:22 +0000 by phpDocumentor 1.3.1