TYPO3 CMS  TYPO3_6-2
PredisStore.php
Go to the documentation of this file.
1 <?php
2 
20 require_once 'Auth/OpenID/Interface.php';
21 
30 
34  protected $redis;
35 
40  protected $prefix;
41 
48  function Auth_OpenID_PredisStore(\Predis\Client $redis, $prefix = '')
49  {
50  $this->prefix = $prefix;
51  $this->redis = $redis;
52  }
53 
59  function storeAssociation($server_url, $association)
60  {
61  // create Redis keys for association itself
62  // and list of associations for this server
63  $associationKey = $this->associationKey($server_url,
64  $association->handle);
65  $serverKey = $this->associationServerKey($server_url);
66 
67  // save association to server's associations' keys list
68  $this->redis->lpush(
69  $serverKey,
70  $associationKey
71  );
72 
73  // Will touch the association list expiration, to avoid filling up
74  $newExpiration = ($association->issued + $association->lifetime);
75 
76  $expirationKey = $serverKey.'_expires_at';
77  $expiration = $this->redis->get($expirationKey);
78  if (!$expiration || $newExpiration > $expiration) {
79  $this->redis->set($expirationKey, $newExpiration);
80  $this->redis->expireat($serverKey, $newExpiration);
81  $this->redis->expireat($expirationKey, $newExpiration);
82  }
83 
84  // save association itself, will automatically expire
85  $this->redis->setex(
86  $associationKey,
87  $newExpiration - time(),
88  serialize($association)
89  );
90  }
91 
96  function getAssociation($server_url, $handle = null)
97  {
98  // simple case: handle given
99  if ($handle !== null) {
100  return $this->getAssociationFromServer(
101  $this->associationKey($server_url, $handle)
102  );
103  }
104 
105  // no handle given, receiving the latest issued
106  $serverKey = $this->associationServerKey($server_url);
107  $lastKey = $this->redis->lindex($serverKey, -1);
108  if (!$lastKey) {
109  // no previous association with this server
110  return null;
111  }
112 
113  // get association, return null if failed
114  return $this->getAssociationFromServer($lastKey);
115  }
116 
121  private function getAssociationFromServer($associationKey)
122  {
123  $association = $this->redis->get($associationKey);
124  return $association ? unserialize($association) : null;
125  }
126 
130  function removeAssociation($server_url, $handle)
131  {
132  // create Redis keys
133  $serverKey = $this->associationServerKey($server_url);
134  $associationKey = $this->associationKey($server_url,
135  $handle);
136 
137  // Removing the association from the server's association list
138  $removed = $this->redis->lrem($serverKey, 0, $associationKey);
139  if ($removed < 1) {
140  return false;
141  }
142 
143  // Delete the association itself
144  return $this->redis->del($associationKey);
145  }
146 
151  function useNonce($server_url, $timestamp, $salt)
152  {
153  global $Auth_OpenID_SKEW;
154 
155  // save one request to memcache when nonce obviously expired
156  if (abs($timestamp - time()) > $Auth_OpenID_SKEW) {
157  return false;
158  }
159 
160  // SETNX will set the value only of the key doesn't exist yet.
161  $nonceKey = $this->nonceKey($server_url, $salt);
162  $added = $this->redis->setnx($nonceKey, "1");
163  if ($added) {
164  // Will set expiration
165  $this->redis->expire($nonceKey, $Auth_OpenID_SKEW);
166  return true;
167  } else {
168  return false;
169  }
170  }
171 
175  private function nonceKey($server_url, $salt)
176  {
177  return $this->prefix .
178  'openid_nonce_' .
179  sha1($server_url) . '_' . sha1($salt);
180  }
181 
185  function associationKey($server_url, $handle = null)
186  {
187  return $this->prefix .
188  'openid_association_' .
189  sha1($server_url) . '_' . sha1($handle);
190  }
191 
195  function associationServerKey($server_url)
196  {
197  return $this->prefix .
198  'openid_association_server_' .
199  sha1($server_url);
200  }
201 
205  function supportsCleanup()
206  {
207  return false;
208  }
209 
210 }
211 
storeAssociation($server_url, $association)
Definition: PredisStore.php:59
nonceKey($server_url, $salt)
removeAssociation($server_url, $handle)
getAssociationFromServer($associationKey)
getAssociation($server_url, $handle=null)
Definition: PredisStore.php:96
associationKey($server_url, $handle=null)
Auth_OpenID_PredisStore(\Predis\Client $redis, $prefix='')
Definition: PredisStore.php:48
associationServerKey($server_url)
global $Auth_OpenID_SKEW
Definition: Nonce.php:23
useNonce($server_url, $timestamp, $salt)