| [ Index ] |
PHP Cross Reference of Joomla 1.5.26 DE |
[Summary view] [Print] [Text view]
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 class Auth_OpenID_MemcachedStore extends Auth_OpenID_OpenIDStore { 40 41 /** 42 * Initializes a new {@link Auth_OpenID_MemcachedStore} instance. 43 * Just saves memcached object as property. 44 * 45 * @param resource connection Memcache connection resourse 46 */ 47 function Auth_OpenID_MemcachedStore($connection, $compress = false) 48 { 49 $this->connection = $connection; 50 $this->compress = $compress ? MEMCACHE_COMPRESSED : 0; 51 } 52 53 /** 54 * Store association until its expiration time in memcached. 55 * Overwrites any existing association with same server_url and 56 * handle. Handles list of associations for every server. 57 */ 58 function storeAssociation($server_url, $association) 59 { 60 // create memcached keys for association itself 61 // and list of associations for this server 62 $associationKey = $this->associationKey($server_url, 63 $association->handle); 64 $serverKey = $this->associationServerKey($server_url); 65 66 // get list of associations 67 $serverAssociations = $this->connection->get($serverKey); 68 69 // if no such list, initialize it with empty array 70 if (!$serverAssociations) { 71 $serverAssociations = array(); 72 } 73 // and store given association key in it 74 $serverAssociations[$association->issued] = $associationKey; 75 76 // save associations' keys list 77 $this->connection->set( 78 $serverKey, 79 $serverAssociations, 80 $this->compress 81 ); 82 // save association itself 83 $this->connection->set( 84 $associationKey, 85 $association, 86 $this->compress, 87 $association->issued + $association->lifetime); 88 } 89 90 /** 91 * Read association from memcached. If no handle given 92 * and multiple associations found, returns latest issued 93 */ 94 function getAssociation($server_url, $handle = null) 95 { 96 // simple case: handle given 97 if ($handle !== null) { 98 // get association, return null if failed 99 $association = $this->connection->get( 100 $this->associationKey($server_url, $handle)); 101 return $association ? $association : null; 102 } 103 104 // no handle given, working with list 105 // create key for list of associations 106 $serverKey = $this->associationServerKey($server_url); 107 108 // get list of associations 109 $serverAssociations = $this->connection->get($serverKey); 110 // return null if failed or got empty list 111 if (!$serverAssociations) { 112 return null; 113 } 114 115 // get key of most recently issued association 116 $keys = array_keys($serverAssociations); 117 sort($keys); 118 $lastKey = $serverAssociations[array_pop($keys)]; 119 120 // get association, return null if failed 121 $association = $this->connection->get($lastKey); 122 return $association ? $association : null; 123 } 124 125 /** 126 * Immediately delete association from memcache. 127 */ 128 function removeAssociation($server_url, $handle) 129 { 130 // create memcached keys for association itself 131 // and list of associations for this server 132 $serverKey = $this->associationServerKey($server_url); 133 $associationKey = $this->associationKey($server_url, 134 $handle); 135 136 // get list of associations 137 $serverAssociations = $this->connection->get($serverKey); 138 // return null if failed or got empty list 139 if (!$serverAssociations) { 140 return false; 141 } 142 143 // ensure that given association key exists in list 144 $serverAssociations = array_flip($serverAssociations); 145 if (!array_key_exists($associationKey, $serverAssociations)) { 146 return false; 147 } 148 149 // remove given association key from list 150 unset($serverAssociations[$associationKey]); 151 $serverAssociations = array_flip($serverAssociations); 152 153 // save updated list 154 $this->connection->set( 155 $serverKey, 156 $serverAssociations, 157 $this->compress 158 ); 159 160 // delete association 161 return $this->connection->delete($associationKey); 162 } 163 164 /** 165 * Create nonce for server and salt, expiring after 166 * $Auth_OpenID_SKEW seconds. 167 */ 168 function useNonce($server_url, $timestamp, $salt) 169 { 170 global $Auth_OpenID_SKEW; 171 172 // save one request to memcache when nonce obviously expired 173 if (abs($timestamp - time()) > $Auth_OpenID_SKEW) { 174 return false; 175 } 176 177 // returns false when nonce already exists 178 // otherwise adds nonce 179 return $this->connection->add( 180 'openid_nonce_' . sha1($server_url) . '_' . sha1($salt), 181 1, // any value here 182 $this->compress, 183 $Auth_OpenID_SKEW); 184 } 185 186 /** 187 * Memcache key is prefixed with 'openid_association_' string. 188 */ 189 function associationKey($server_url, $handle = null) 190 { 191 return 'openid_association_' . sha1($server_url) . '_' . sha1($handle); 192 } 193 194 /** 195 * Memcache key is prefixed with 'openid_association_' string. 196 */ 197 function associationServerKey($server_url) 198 { 199 return 'openid_association_server_' . sha1($server_url); 200 } 201 202 /** 203 * Report that this storage doesn't support cleanup 204 */ 205 function supportsCleanup() 206 { 207 return false; 208 } 209 } 210 211 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Wed Mar 28 15:54:07 2012 | Cross-referenced by PHPXref 0.7.1 |