123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- <?php
- require_once 'Auth/OpenID/Interface.php';
- class Auth_OpenID_PredisStore extends Auth_OpenID_OpenIDStore {
-
- protected $redis;
-
- protected $prefix;
-
- function Auth_OpenID_PredisStore(\Predis\Client $redis, $prefix = '')
- {
- $this->prefix = $prefix;
- $this->redis = $redis;
- }
-
- function storeAssociation($server_url, $association)
- {
-
-
- $associationKey = $this->associationKey($server_url,
- $association->handle);
- $serverKey = $this->associationServerKey($server_url);
-
-
- $this->redis->lpush(
- $serverKey,
- $associationKey
- );
-
- $newExpiration = ($association->issued + $association->lifetime);
- $expirationKey = $serverKey.'_expires_at';
- $expiration = $this->redis->get($expirationKey);
- if (!$expiration || $newExpiration > $expiration) {
- $this->redis->set($expirationKey, $newExpiration);
- $this->redis->expireat($serverKey, $newExpiration);
- $this->redis->expireat($expirationKey, $newExpiration);
- }
-
- $this->redis->setex(
- $associationKey,
- $newExpiration - time(),
- serialize($association)
- );
- }
-
- function getAssociation($server_url, $handle = null)
- {
-
- if ($handle !== null) {
- return $this->getAssociationFromServer(
- $this->associationKey($server_url, $handle)
- );
- }
-
-
- $serverKey = $this->associationServerKey($server_url);
- $lastKey = $this->redis->lindex($serverKey, -1);
- if (!$lastKey) {
-
- return null;
- }
-
- return $this->getAssociationFromServer($lastKey);
- }
-
-
- private function getAssociationFromServer($associationKey)
- {
- $association = $this->redis->get($associationKey);
- return $association ? unserialize($association) : null;
- }
-
- function removeAssociation($server_url, $handle)
- {
-
- $serverKey = $this->associationServerKey($server_url);
- $associationKey = $this->associationKey($server_url,
- $handle);
-
-
- $removed = $this->redis->lrem($serverKey, 0, $associationKey);
- if ($removed < 1) {
- return false;
- }
-
- return $this->redis->del($associationKey);
- }
-
- function useNonce($server_url, $timestamp, $salt)
- {
- global $Auth_OpenID_SKEW;
-
-
- if (abs($timestamp - time()) > $Auth_OpenID_SKEW) {
- return false;
- }
-
-
- $nonceKey = $this->nonceKey($server_url, $salt);
- $added = $this->redis->setnx($nonceKey, "1");
- if ($added) {
-
- $this->redis->expire($nonceKey, $Auth_OpenID_SKEW);
- return true;
- } else {
- return false;
- }
- }
-
-
- private function nonceKey($server_url, $salt)
- {
- return $this->prefix .
- 'openid_nonce_' .
- sha1($server_url) . '_' . sha1($salt);
- }
-
-
- function associationKey($server_url, $handle = null)
- {
- return $this->prefix .
- 'openid_association_' .
- sha1($server_url) . '_' . sha1($handle);
- }
-
-
- function associationServerKey($server_url)
- {
- return $this->prefix .
- 'openid_association_server_' .
- sha1($server_url);
- }
-
-
- function supportsCleanup()
- {
- return false;
- }
- }
|