OAuthCommonStorageRedis

Stores a token in a Redis server.

Defined (1)

The class is defined in the following location(s).

/lib/PHPoAuthLib/src/OAuth/Common/Storage/Redis.php  
  1. class Redis implements TokenStorageInterface 
  2. /** 
  3. * @var string 
  4. */ 
  5. protected $key; 
  6.  
  7. protected $stateKey; 
  8.  
  9. /** 
  10. * @var object|\Redis 
  11. */ 
  12. protected $redis; 
  13.  
  14. /** 
  15. * @var object|TokenInterface 
  16. */ 
  17. protected $cachedTokens; 
  18.  
  19. /** 
  20. * @var object 
  21. */ 
  22. protected $cachedStates; 
  23.  
  24. /** 
  25. * @param Predis $redis An instantiated and connected redis client 
  26. * @param string $key The key to store the token under in redis 
  27. * @param string $stateKey The key to store the state under in redis. 
  28. */ 
  29. public function __construct(Predis $redis, $key, $stateKey) 
  30. $this->redis = $redis; 
  31. $this->key = $key; 
  32. $this->stateKey = $stateKey; 
  33. $this->cachedTokens = array(); 
  34. $this->cachedStates = array(); 
  35.  
  36. /** 
  37. * {@inheritDoc} 
  38. */ 
  39. public function retrieveAccessToken($service) 
  40. if (!$this->hasAccessToken($service)) { 
  41. throw new TokenNotFoundException('Token not found in redis'); 
  42.  
  43. if (isset($this->cachedTokens[$service])) { 
  44. return $this->cachedTokens[$service]; 
  45.  
  46. $val = $this->redis->hget($this->key, $service); 
  47.  
  48. return $this->cachedTokens[$service] = unserialize($val); 
  49.  
  50. /** 
  51. * {@inheritDoc} 
  52. */ 
  53. public function storeAccessToken($service, TokenInterface $token) 
  54. // (over)write the token 
  55. $this->redis->hset($this->key, $service, serialize($token)); 
  56. $this->cachedTokens[$service] = $token; 
  57.  
  58. // allow chaining 
  59. return $this; 
  60.  
  61. /** 
  62. * {@inheritDoc} 
  63. */ 
  64. public function hasAccessToken($service) 
  65. if (isset($this->cachedTokens[$service]) 
  66. && $this->cachedTokens[$service] instanceof TokenInterface 
  67. ) { 
  68. return true; 
  69.  
  70. return $this->redis->hexists($this->key, $service); 
  71.  
  72. /** 
  73. * {@inheritDoc} 
  74. */ 
  75. public function clearToken($service) 
  76. $this->redis->hdel($this->key, $service); 
  77. unset($this->cachedTokens[$service]); 
  78.  
  79. // allow chaining 
  80. return $this; 
  81.  
  82. /** 
  83. * {@inheritDoc} 
  84. */ 
  85. public function clearAllTokens() 
  86. // memory 
  87. $this->cachedTokens = array(); 
  88.  
  89. // redis 
  90. $keys = $this->redis->hkeys($this->key); 
  91. $me = $this; // 5.3 compat 
  92.  
  93. // pipeline for performance 
  94. $this->redis->pipeline( 
  95. function ($pipe) use ($keys, $me) { 
  96. foreach ($keys as $k) { 
  97. $pipe->hdel($me->getKey(), $k); 
  98. ); 
  99.  
  100. // allow chaining 
  101. return $this; 
  102.  
  103. /** 
  104. * {@inheritDoc} 
  105. */ 
  106. public function retrieveAuthorizationState($service) 
  107. if (!$this->hasAuthorizationState($service)) { 
  108. throw new AuthorizationStateNotFoundException('State not found in redis'); 
  109.  
  110. if (isset($this->cachedStates[$service])) { 
  111. return $this->cachedStates[$service]; 
  112.  
  113. $val = $this->redis->hget($this->stateKey, $service); 
  114.  
  115. return $this->cachedStates[$service] = unserialize($val); 
  116.  
  117. /** 
  118. * {@inheritDoc} 
  119. */ 
  120. public function storeAuthorizationState($service, $state) 
  121. // (over)write the token 
  122. $this->redis->hset($this->stateKey, $service, $state); 
  123. $this->cachedStates[$service] = $state; 
  124.  
  125. // allow chaining 
  126. return $this; 
  127.  
  128. /** 
  129. * {@inheritDoc} 
  130. */ 
  131. public function hasAuthorizationState($service) 
  132. if (isset($this->cachedStates[$service]) 
  133. && null !== $this->cachedStates[$service] 
  134. ) { 
  135. return true; 
  136.  
  137. return $this->redis->hexists($this->stateKey, $service); 
  138.  
  139. /** 
  140. * {@inheritDoc} 
  141. */ 
  142. public function clearAuthorizationState($service) 
  143. $this->redis->hdel($this->stateKey, $service); 
  144. unset($this->cachedStates[$service]); 
  145.  
  146. // allow chaining 
  147. return $this; 
  148.  
  149. /** 
  150. * {@inheritDoc} 
  151. */ 
  152. public function clearAllAuthorizationStates() 
  153. // memory 
  154. $this->cachedStates = array(); 
  155.  
  156. // redis 
  157. $keys = $this->redis->hkeys($this->stateKey); 
  158. $me = $this; // 5.3 compat 
  159.  
  160. // pipeline for performance 
  161. $this->redis->pipeline( 
  162. function ($pipe) use ($keys, $me) { 
  163. foreach ($keys as $k) { 
  164. $pipe->hdel($me->getKey(), $k); 
  165. ); 
  166.  
  167. // allow chaining 
  168. return $this; 
  169.  
  170. /** 
  171. * @return Predis $redis 
  172. */ 
  173. public function getRedis() 
  174. return $this->redis; 
  175.  
  176. /** 
  177. * @return string $key 
  178. */ 
  179. public function getKey() 
  180. return $this->key;