Source code for file /openid/Auth/OpenID/MemcachedStore.php
Documentation is available at MemcachedStore.php
* This file supplies a memcached store backend for OpenID servers and
* LICENSE: See the COPYING file included in this distribution.
* @author Artemy Tregubenko <me@arty.name>
* @copyright 2008 JanRain, Inc.
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache
* Contributed by Open Web Technologies <http://openwebtech.ru/>
// Do not allow direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
* Import the interface for creating a new store class.
require_once 'Auth/OpenID/Interface.php';
* This is a memcached-based store for OpenID associations and
* 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
* Initializes a new {@link Auth_OpenID_MemcachedStore} instance.
* Just saves memcached object as property.
* @param resource connection Memcache connection resourse
$this->connection =
$connection;
$this->compress =
$compress ?
MEMCACHE_COMPRESSED :
0;
* 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.
// create memcached keys for association itself
// and list of associations for this server
// 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 association itself
$association->issued +
$association->lifetime);
* Read association from memcached. If no handle given
* and multiple associations found, returns latest issued
// simple case: handle given
// get association, return null if failed
$association =
$this->connection->get(
return $association ?
$association :
null;
// no handle given, working with list
// create key for list of associations
// get list of associations
$serverAssociations =
$this->connection->get($serverKey);
// return null if failed or got empty list
if (!$serverAssociations) {
// get key of most recently issued association
$lastKey =
$serverAssociations[array_pop($keys)];
// get association, return null if failed
$association =
$this->connection->get($lastKey);
return $association ?
$association :
null;
* Immediately delete association from memcache.
// create memcached keys for association itself
// and list of associations for this server
// get list of associations
$serverAssociations =
$this->connection->get($serverKey);
// return null if failed or got empty list
if (!$serverAssociations) {
// ensure that given association key exists in list
$serverAssociations =
array_flip($serverAssociations);
// remove given association key from list
unset
($serverAssociations[$associationKey]);
$serverAssociations =
array_flip($serverAssociations);
return $this->connection->delete($associationKey);
* Create nonce for server and salt, expiring after
* $Auth_OpenID_SKEW seconds.
function useNonce($server_url, $timestamp, $salt)
global $Auth_OpenID_SKEW;
// save one request to memcache when nonce obviously expired
if (abs($timestamp -
time()) >
$Auth_OpenID_SKEW) {
// returns false when nonce already exists
return $this->connection->add(
'openid_nonce_' .
sha1($server_url) .
'_' .
sha1($salt),
* Memcache key is prefixed with 'openid_association_' string.
return 'openid_association_' .
sha1($server_url) .
'_' .
sha1($handle);
* Memcache key is prefixed with 'openid_association_' string.
return 'openid_association_server_' .
sha1($server_url);
* Report that this storage doesn't support cleanup