Source code for file /openid/Services/Yadis/Manager.php
Documentation is available at Manager.php
* Yadis service manager to be used during yadis-driven authentication
* The base session class used by the Services_Yadis_Manager. This
* class wraps the default PHP session machinery and should be
* subclassed if your application doesn't use PHP sessioning.
* Set a session key/value pair.
* @param string $name The name of the session key to add.
* @param string $value The value to add to the session.
function set($name, $value)
$_SESSION[$name] =
$value;
* Get a key's value from the session.
* @param string $name The name of the key to retrieve.
* @param string $default The optional value to return if the key
* is not found in the session.
* @return string $result The key's value in the session or
* $default if it isn't found.
function get($name, $default=
null)
* Remove a key/value pair from the session.
* @param string $name The name of the key to remove.
* Return the contents of the session in array form.
* A session helper class designed to translate between arrays and
* objects. Note that the class used must have a constructor that
* takes no parameters. This is not a general solution, but it works
* for dumb objects that just need to have attributes set. The idea
* is that you'll subclass this and override $this->check($data) ->
* bool to implement your own session data validation.
* Given a session data value (an array), this creates an object
* (returned by $this->newObject()) whose attributes and values
* are those in $data. Returns null if $data lacks keys found in
* $this->requiredKeys(). Returns null if $this->check($data)
* evaluates to false. Returns null if $this->newObject()
$required =
$this->requiredKeys();
foreach ($required as $k) {
if (!$this->check($data)) {
foreach ($required as $k) {
* Prepares the data array by making any necessary changes.
* Returns an array whose keys and values will be used to update
* the original data array before calling $this->newObject($data).
* Returns a new instance of this loader's class, using the
* session data to construct it if necessary. The object need
* only be created; $this->fromSession() will take care of setting
* the object's attributes.
* Returns an array of keys and values built from the attributes
* of $obj. If $this->prepareForSave($obj) returns an array, its keys
* and values are used to update the $data array of attributes
foreach ($obj as $k =>
$v) {
foreach ($extra as $k =>
$v) {
foreach ($obj as $k =>
$v) {
return array('starting_url',
foreach ($data['services'] as $s) {
$services[] =
$loader->fromSession($s);
return array('services' =>
$services);
foreach ($obj->services as $s) {
$services[] =
$loader->toSession($s);
return array('services' =>
$services);
* The Yadis service manager which stores state in a session and
* iterates over <Service> elements in a Yadis XRDS document and lets
* a caller attempt to use each one. This is used by the Yadis
* Intialize a new yadis service manager.
function Services_Yadis_Manager($starting_url, $yadis_url,
// The URL that was used to initiate the Yadis protocol
$this->starting_url =
$starting_url;
// The URL after following redirects (the identifier)
$this->yadis_url =
$yadis_url;
// List of service elements
$this->services =
$services;
$this->session_key =
$session_key;
// Reference to the current service object
// Stale flag for cleanup if PHP lib has trouble.
// How many untried services remain?
return count($this->services);
* Return the next service
* $this->current() will continue to return that service until the
* next call to this method.
// Return the current service.
// Returns None if there are no services left.
return in_array($url, array($this->starting_url, $this->yadis_url));
// Has the first service been returned?
return $this->_current !==
null;
* State management for discovery.
* High-level usage pattern is to call .getNextService(discover) in
* order to find the next available service for this user for this
* session. Once a request completes, call .finish() to clean up the
var $DEFAULT_SUFFIX =
'auth';
var $PREFIX =
'_yadis_services_';
* Initialize a discovery object.
* @param Services_Yadis_PHPSession $session An object which
* implements the Services_Yadis_PHPSession API.
* @param string $url The URL on which to attempt discovery.
* @param string $session_key_suffix The optional session key
$session_key_suffix =
null)
/// Initialize a discovery object
$this->session =
& $session;
if ($session_key_suffix ===
null) {
$session_key_suffix =
$this->DEFAULT_SUFFIX;
$this->session_key_suffix =
$session_key_suffix;
$this->session_key =
$this->PREFIX .
$this->session_key_suffix;
* Return the next authentication service for the pair of
* user_input and session. This function handles fallback.
$manager =
$this->getManager();
if (!$manager ||
(!$manager->services)) {
$http_response =
array();
$manager =
$this->createManager($services, $this->url);
$service =
$manager->nextService();
$this->session->set($this->session_key,
* Clean up Yadis-related services in the session and return the
* most-recently-attempted service from the manager, if one
$manager =
$this->getManager();
$service =
$manager->current();
// Get the session key for this starting URL and suffix
return $this->PREFIX .
$this->session_key_suffix;
// Extract the YadisServiceManager for this object's URL and
// suffix from the session.
$manager_str =
$this->session->get($this->getSessionKey());
if ($manager_str !==
null) {
$manager =
$loader->fromSession(unserialize($manager_str));
if ($manager &&
$manager->forURL($this->url)) {
function &createManager($services, $yadis_url =
null)
$key =
$this->getSessionKey();
if ($this->getManager()) {
return $this->getManager();
$this->session->set($this->session_key,
function destroyManager()
if ($this->getManager() !==
null) {
$key =
$this->getSessionKey();
$this->session->del($key);