00001 <?php
00002
00021 require_once 'Auth/OpenID/Interface.php';
00022
00036 class Auth_OpenID_MemcachedStore extends Auth_OpenID_OpenIDStore {
00037
00044 function Auth_OpenID_MemcachedStore($connection, $compress = false)
00045 {
00046 $this->connection = $connection;
00047 $this->compress = $compress ? MEMCACHE_COMPRESSED : 0;
00048 }
00049
00055 function storeAssociation($server_url, $association)
00056 {
00057
00058
00059 $associationKey = $this->associationKey($server_url,
00060 $association->handle);
00061 $serverKey = $this->associationServerKey($server_url);
00062
00063
00064 $serverAssociations = $this->connection->get($serverKey);
00065
00066
00067 if (!$serverAssociations) {
00068 $serverAssociations = array();
00069 }
00070
00071 $serverAssociations[$association->issued] = $associationKey;
00072
00073
00074 $this->connection->set(
00075 $serverKey,
00076 $serverAssociations,
00077 $this->compress
00078 );
00079
00080 $this->connection->set(
00081 $associationKey,
00082 $association,
00083 $this->compress,
00084 $association->issued + $association->lifetime);
00085 }
00086
00091 function getAssociation($server_url, $handle = null)
00092 {
00093
00094 if ($handle !== null) {
00095
00096 $association = $this->connection->get(
00097 $this->associationKey($server_url, $handle));
00098 return $association ? $association : null;
00099 }
00100
00101
00102
00103 $serverKey = $this->associationServerKey($server_url);
00104
00105
00106 $serverAssociations = $this->connection->get($serverKey);
00107
00108 if (!$serverAssociations) {
00109 return null;
00110 }
00111
00112
00113 $keys = array_keys($serverAssociations);
00114 sort($keys);
00115 $lastKey = $serverAssociations[array_pop($keys)];
00116
00117
00118 $association = $this->connection->get($lastKey);
00119 return $association ? $association : null;
00120 }
00121
00125 function removeAssociation($server_url, $handle)
00126 {
00127
00128
00129 $serverKey = $this->associationServerKey($server_url);
00130 $associationKey = $this->associationKey($server_url,
00131 $handle);
00132
00133
00134 $serverAssociations = $this->connection->get($serverKey);
00135
00136 if (!$serverAssociations) {
00137 return false;
00138 }
00139
00140
00141 $serverAssociations = array_flip($serverAssociations);
00142 if (!array_key_exists($associationKey, $serverAssociations)) {
00143 return false;
00144 }
00145
00146
00147 unset($serverAssociations[$associationKey]);
00148 $serverAssociations = array_flip($serverAssociations);
00149
00150
00151 $this->connection->set(
00152 $serverKey,
00153 $serverAssociations,
00154 $this->compress
00155 );
00156
00157
00158 return $this->connection->delete($associationKey);
00159 }
00160
00165 function useNonce($server_url, $timestamp, $salt)
00166 {
00167 global $Auth_OpenID_SKEW;
00168
00169
00170 if (abs($timestamp - time()) > $Auth_OpenID_SKEW) {
00171 return false;
00172 }
00173
00174
00175
00176 return $this->connection->add(
00177 'openid_nonce_' . sha1($server_url) . '_' . sha1($salt),
00178 1,
00179 $this->compress,
00180 $Auth_OpenID_SKEW);
00181 }
00182
00186 function associationKey($server_url, $handle = null)
00187 {
00188 return 'openid_association_' . sha1($server_url) . '_' . sha1($handle);
00189 }
00190
00194 function associationServerKey($server_url)
00195 {
00196 return 'openid_association_server_' . sha1($server_url);
00197 }
00198
00202 function supportsCleanup()
00203 {
00204 return false;
00205 }
00206 }
00207
00208 ?>