diff options
Diffstat (limited to 'plugins/openid/lib/Auth/OpenID/MemcachedStore.php')
-rw-r--r-- | plugins/openid/lib/Auth/OpenID/MemcachedStore.php | 115 |
1 files changed, 72 insertions, 43 deletions
diff --git a/plugins/openid/lib/Auth/OpenID/MemcachedStore.php b/plugins/openid/lib/Auth/OpenID/MemcachedStore.php index 10785860..5badd0ab 100644 --- a/plugins/openid/lib/Auth/OpenID/MemcachedStore.php +++ b/plugins/openid/lib/Auth/OpenID/MemcachedStore.php @@ -22,10 +22,10 @@ require_once 'Auth/OpenID/Interface.php'; /** * This is a memcached-based store for OpenID associations and - * nonces. - * - * As memcache has limit of 250 chars for key length, - * server_url, handle and salt are hashed with sha1(). + * nonces. + * + * As memcache has limit of 250 chars for key length, + * server_url, handle and salt are hashed with sha1(). * * Most of the methods of this class are implementation details. * People wishing to just use this store need only pay attention to @@ -34,12 +34,18 @@ require_once 'Auth/OpenID/Interface.php'; * @package OpenID */ class Auth_OpenID_MemcachedStore extends Auth_OpenID_OpenIDStore { + /** @var int */ + private $compress = 0; + + /** @var Memcache */ + private $connection; /** * Initializes a new {@link Auth_OpenID_MemcachedStore} instance. * Just saves memcached object as property. * - * @param resource connection Memcache connection resourse + * @param Memcache $connection Memcache connection resource + * @param bool $compress */ function __construct($connection, $compress = false) { @@ -48,29 +54,32 @@ class Auth_OpenID_MemcachedStore extends Auth_OpenID_OpenIDStore { } /** - * Store association until its expiration time in memcached. - * Overwrites any existing association with same server_url and - * handle. Handles list of associations for every server. + * Store association until its expiration time in memcached. + * Overwrites any existing association with same server_url and + * handle. Handles list of associations for every server. + * + * @param string $server_url + * @param Auth_OpenID_Association $association */ function storeAssociation($server_url, $association) { - // create memcached keys for association itself + // create memcached keys for association itself // and list of associations for this server - $associationKey = $this->associationKey($server_url, + $associationKey = $this->associationKey($server_url, $association->handle); $serverKey = $this->associationServerKey($server_url); - - // get list of associations + + // get list of associations $serverAssociations = $this->connection->get($serverKey); - + // if no such list, initialize it with empty array if (!$serverAssociations) { $serverAssociations = array(); } // and store given association key in it $serverAssociations[$association->issued] = $associationKey; - - // save associations' keys list + + // save associations' keys list $this->connection->set( $serverKey, $serverAssociations, @@ -79,14 +88,18 @@ class Auth_OpenID_MemcachedStore extends Auth_OpenID_OpenIDStore { // save association itself $this->connection->set( $associationKey, - $association, - $this->compress, + $association, + $this->compress, $association->issued + $association->lifetime); } /** - * Read association from memcached. If no handle given + * Read association from memcached. If no handle given * and multiple associations found, returns latest issued + * + * @param string $server_url + * @param null $handle + * @return Auth_OpenID_Association|null */ function getAssociation($server_url, $handle = null) { @@ -97,23 +110,23 @@ class Auth_OpenID_MemcachedStore extends Auth_OpenID_OpenIDStore { $this->associationKey($server_url, $handle)); return $association ? $association : null; } - + // no handle given, working with list // create key for list of associations $serverKey = $this->associationServerKey($server_url); - + // get list of associations $serverAssociations = $this->connection->get($serverKey); // return null if failed or got empty list if (!$serverAssociations) { return null; } - + // get key of most recently issued association $keys = array_keys($serverAssociations); sort($keys); $lastKey = $serverAssociations[array_pop($keys)]; - + // get association, return null if failed $association = $this->connection->get($lastKey); return $association ? $association : null; @@ -121,32 +134,36 @@ class Auth_OpenID_MemcachedStore extends Auth_OpenID_OpenIDStore { /** * Immediately delete association from memcache. + * + * @param string $server_url + * @param string $handle + * @return bool|mixed */ function removeAssociation($server_url, $handle) { - // create memcached keys for association itself + // create memcached keys for association itself // and list of associations for this server $serverKey = $this->associationServerKey($server_url); - $associationKey = $this->associationKey($server_url, + $associationKey = $this->associationKey($server_url, $handle); - + // get list of associations $serverAssociations = $this->connection->get($serverKey); // return null if failed or got empty list if (!$serverAssociations) { return false; } - + // ensure that given association key exists in list $serverAssociations = array_flip($serverAssociations); if (!array_key_exists($associationKey, $serverAssociations)) { return false; } - + // remove given association key from list unset($serverAssociations[$associationKey]); $serverAssociations = array_flip($serverAssociations); - + // save updated list $this->connection->set( $serverKey, @@ -154,48 +171,60 @@ class Auth_OpenID_MemcachedStore extends Auth_OpenID_OpenIDStore { $this->compress ); - // delete association + // delete association return $this->connection->delete($associationKey); } /** - * Create nonce for server and salt, expiring after + * Create nonce for server and salt, expiring after * $Auth_OpenID_SKEW seconds. + * + * @param string $server_url + * @param int $timestamp + * @param string $salt + * @return bool */ function useNonce($server_url, $timestamp, $salt) { global $Auth_OpenID_SKEW; - - // save one request to memcache when nonce obviously expired + + // save one request to memcache when nonce obviously expired if (abs($timestamp - time()) > $Auth_OpenID_SKEW) { return false; } - + // returns false when nonce already exists // otherwise adds nonce return $this->connection->add( - 'openid_nonce_' . sha1($server_url) . '_' . sha1($salt), - 1, // any value here - $this->compress, + 'openid_nonce_' . sha1($server_url) . '_' . sha1($salt), + 1, // any value here + $this->compress, $Auth_OpenID_SKEW); } - + /** - * Memcache key is prefixed with 'openid_association_' string. + * Memcache key is prefixed with 'openid_association_' string. + * + * @param string $server_url + * @param null $handle + * @return string */ - function associationKey($server_url, $handle = null) + function associationKey($server_url, $handle = null) { return 'openid_association_' . sha1($server_url) . '_' . sha1($handle); } - + /** - * Memcache key is prefixed with 'openid_association_' string. + * Memcache key is prefixed with 'openid_association_' string. + * + * @param string $server_url + * @return string */ - function associationServerKey($server_url) + function associationServerKey($server_url) { return 'openid_association_server_' . sha1($server_url); } - + /** * Report that this storage doesn't support cleanup */ |