Support Joomla!

Packages

Package: OpenID

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /openid/Auth/OpenID/MemcachedStore.php

Documentation is available at MemcachedStore.php

  1. <?php
  2.  
  3. /**
  4.  * This file supplies a memcached store backend for OpenID servers and
  5.  * consumers.
  6.  *
  7.  * PHP versions 4 and 5
  8.  *
  9.  * LICENSE: See the COPYING file included in this distribution.
  10.  *
  11.  * @package OpenID
  12.  * @author Artemy Tregubenko <me@arty.name>
  13.  * @copyright 2008 JanRain, Inc.
  14.  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache
  15.  *  Contributed by Open Web Technologies <http://openwebtech.ru/>
  16.  */
  17.  
  18. // Do not allow direct access
  19. defined'_JEXEC' or die'Restricted access' );
  20.  
  21. /**
  22.  * Import the interface for creating a new store class.
  23.  */
  24. require_once 'Auth/OpenID/Interface.php';
  25.  
  26. /**
  27.  * This is a memcached-based store for OpenID associations and
  28.  * nonces.
  29.  * 
  30.  * As memcache has limit of 250 chars for key length,
  31.  * server_url, handle and salt are hashed with sha1().
  32.  *
  33.  * Most of the methods of this class are implementation details.
  34.  * People wishing to just use this store need only pay attention to
  35.  * the constructor.
  36.  *
  37.  * @package OpenID
  38.  */
  39.  
  40.     /**
  41.      * Initializes a new {@link Auth_OpenID_MemcachedStore} instance.
  42.      * Just saves memcached object as property.
  43.      *
  44.      * @param resource connection Memcache connection resourse
  45.      */
  46.     function Auth_OpenID_MemcachedStore($connection$compress false)
  47.     {
  48.         $this->connection $connection;
  49.         $this->compress $compress MEMCACHE_COMPRESSED 0;
  50.     }
  51.  
  52.     /**
  53.      * Store association until its expiration time in memcached.
  54.      * Overwrites any existing association with same server_url and
  55.      * handle. Handles list of associations for every server.
  56.      */
  57.     function storeAssociation($server_url$association)
  58.     {
  59.         // create memcached keys for association itself 
  60.         // and list of associations for this server
  61.         $associationKey $this->associationKey($server_url
  62.             $association->handle);
  63.         $serverKey $this->associationServerKey($server_url);
  64.         
  65.         // get list of associations 
  66.         $serverAssociations $this->connection->get($serverKey);
  67.         
  68.         // if no such list, initialize it with empty array
  69.         if (!$serverAssociations{
  70.             $serverAssociations array();
  71.         }
  72.         // and store given association key in it
  73.         $serverAssociations[$association->issued$associationKey;
  74.         
  75.         // save associations' keys list 
  76.         $this->connection->set(
  77.             $serverKey,
  78.             $serverAssociations,
  79.             $this->compress
  80.         );
  81.         // save association itself
  82.         $this->connection->set(
  83.             $associationKey,
  84.             $association
  85.             $this->compress
  86.             $association->issued $association->lifetime);
  87.     }
  88.  
  89.     /**
  90.      * Read association from memcached. If no handle given
  91.      * and multiple associations found, returns latest issued
  92.      */
  93.     function getAssociation($server_url$handle null)
  94.     {
  95.         // simple case: handle given
  96.         if ($handle !== null{
  97.             // get association, return null if failed
  98.             $association $this->connection->get(
  99.                 $this->associationKey($server_url$handle));
  100.             return $association $association null;
  101.         }
  102.         
  103.         // no handle given, working with list
  104.         // create key for list of associations
  105.         $serverKey $this->associationServerKey($server_url);
  106.         
  107.         // get list of associations
  108.         $serverAssociations $this->connection->get($serverKey);
  109.         // return null if failed or got empty list
  110.         if (!$serverAssociations{
  111.             return null;
  112.         }
  113.         
  114.         // get key of most recently issued association
  115.         $keys array_keys($serverAssociations);
  116.         sort($keys);
  117.         $lastKey $serverAssociations[array_pop($keys)];
  118.         
  119.         // get association, return null if failed
  120.         $association $this->connection->get($lastKey);
  121.         return $association $association null;
  122.     }
  123.  
  124.     /**
  125.      * Immediately delete association from memcache.
  126.      */
  127.     function removeAssociation($server_url$handle)
  128.     {
  129.         // create memcached keys for association itself 
  130.         // and list of associations for this server
  131.         $serverKey $this->associationServerKey($server_url);
  132.         $associationKey $this->associationKey($server_url
  133.             $handle);
  134.         
  135.         // get list of associations
  136.         $serverAssociations $this->connection->get($serverKey);
  137.         // return null if failed or got empty list
  138.         if (!$serverAssociations{
  139.             return false;
  140.         }
  141.         
  142.         // ensure that given association key exists in list
  143.         $serverAssociations array_flip($serverAssociations);
  144.         if (!array_key_exists($associationKey$serverAssociations)) {
  145.             return false;
  146.         }
  147.         
  148.         // remove given association key from list
  149.         unset($serverAssociations[$associationKey]);
  150.         $serverAssociations array_flip($serverAssociations);
  151.         
  152.         // save updated list
  153.         $this->connection->set(
  154.             $serverKey,
  155.             $serverAssociations,
  156.             $this->compress
  157.         );
  158.  
  159.         // delete association 
  160.         return $this->connection->delete($associationKey);
  161.     }
  162.  
  163.     /**
  164.      * Create nonce for server and salt, expiring after
  165.      * $Auth_OpenID_SKEW seconds.
  166.      */
  167.     function useNonce($server_url$timestamp$salt)
  168.     {
  169.         global $Auth_OpenID_SKEW;
  170.         
  171.         // save one request to memcache when nonce obviously expired 
  172.         if (abs($timestamp time()) $Auth_OpenID_SKEW{
  173.             return false;
  174.         }
  175.         
  176.         // returns false when nonce already exists
  177.         // otherwise adds nonce
  178.         return $this->connection->add(
  179.             'openid_nonce_' sha1($server_url'_' sha1($salt)
  180.             1// any value here 
  181.             $this->compress
  182.             $Auth_OpenID_SKEW);
  183.     }
  184.     
  185.     /**
  186.      * Memcache key is prefixed with 'openid_association_' string.
  187.      */
  188.     function associationKey($server_url$handle null
  189.     {
  190.         return 'openid_association_' sha1($server_url'_' sha1($handle);
  191.     }
  192.     
  193.     /**
  194.      * Memcache key is prefixed with 'openid_association_' string.
  195.      */
  196.     function associationServerKey($server_url
  197.     {
  198.         return 'openid_association_server_' sha1($server_url);
  199.     }
  200.     
  201.     /**
  202.      * Report that this storage doesn't support cleanup
  203.      */
  204.     function supportsCleanup()
  205.     {
  206.         return false;
  207.     }
  208. }
  209.  
  210. ?>

Documentation generated on Sat, 14 Nov 2009 11:15:45 +0000 by phpDocumentor 1.3.1