PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/src/OAuth2/Storage/Redis.php

https://github.com/rich-choy/oauth2-server-php
PHP | 207 lines | 158 code | 26 blank | 23 comment | 13 complexity | 73489aa613ae8acf9a6284d679cf6347 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. namespace OAuth2\Storage;
  3. /**
  4. * redis storage for all storage types
  5. *
  6. * Register client:
  7. * <code>
  8. * $storage = new OAuth2_Storage_Redis($redis);
  9. * $storage->registerClient($client_id, $client_secret, $redirect_uri);
  10. * </code>
  11. */
  12. class Redis implements AuthorizationCodeInterface,
  13. AccessTokenInterface,
  14. ClientCredentialsInterface,
  15. UserCredentialsInterface,
  16. RefreshTokenInterface,
  17. JwtBearerInterface
  18. {
  19. private $redis;
  20. private $config;
  21. private $cache;
  22. /**
  23. * Redis Storage!
  24. *
  25. * @param \Predis\Client $redis
  26. * @param array $config
  27. */
  28. public function __construct($redis, $config=array())
  29. {
  30. $this->redis = $redis;
  31. $this->config = array_merge(array(
  32. 'client_key' => 'oauth_clients:',
  33. 'access_token_key' => 'oauth_access_tokens:',
  34. 'refresh_token_key' => 'oauth_refresh_tokens:',
  35. 'code_key' => 'oauth_authorization_codes:',
  36. 'user_key' => 'oauth_users:',
  37. 'jwt_key' => 'oauth_jwt:',
  38. ), $config);
  39. }
  40. protected function getValue($key)
  41. {
  42. if ( isset($this->cache[$key]) ) {
  43. return $this->cache[$key];
  44. }
  45. $value = $this->redis->get($key);
  46. if ( isset($value) ) {
  47. return json_decode($value, true);
  48. } else {
  49. return false;
  50. }
  51. }
  52. protected function setValue($key, $value, $expire=0)
  53. {
  54. $this->cache[$key] = $value;
  55. $str = json_encode($value);
  56. if ( $expire > 0 ) {
  57. $seconds = $expire - time();
  58. return $this->redis->setex($key, $seconds, $str);
  59. } else {
  60. return $this->redis->set($key, $str);
  61. }
  62. }
  63. protected function expireValue($key)
  64. {
  65. unset($this->cache[$key]);
  66. return $this->redis->expire($key);
  67. }
  68. /* AuthorizationCodeInterface */
  69. public function getAuthorizationCode($code)
  70. {
  71. return $this->getValue($this->config['code_key'] . $code);
  72. }
  73. public function setAuthorizationCode($authorization_code, $client_id, $user_id, $redirect_uri, $expires, $scope = null)
  74. {
  75. return $this->setValue(
  76. $this->config['code_key'] . $authorization_code,
  77. compact('authorization_code', 'client_id', 'user_id', 'redirect_uri', 'expires', 'scope'),
  78. $expires
  79. );
  80. }
  81. public function expireAuthorizationCode($code)
  82. {
  83. $key = $this->config['code_key'] . $code;
  84. unset($this->cache[$key]);
  85. return $this->expireValue($key);
  86. }
  87. /* UserCredentialsInterface */
  88. public function checkUserCredentials($username, $password)
  89. {
  90. $user = $this->getUserDetails($username);
  91. return $user && $user['password'] === $password;
  92. }
  93. public function getUserDetails($username)
  94. {
  95. return $this->getUser($username);
  96. }
  97. public function getUser($username)
  98. {
  99. if (!$userInfo = $this->getValue($this->config['user_key'] . $username)) {
  100. return false;
  101. }
  102. // the default behavior is to use "username" as the user_id
  103. return array_merge(array(
  104. 'user_id' => $username,
  105. ), $userInfo);
  106. }
  107. public function setUser($username, $password, $first_name = null, $last_name = null)
  108. {
  109. return $this->setValue(
  110. $this->config['user_key'] . $username,
  111. compact('username', 'password', 'first_name', 'last_name')
  112. );
  113. }
  114. /* ClientCredentialsInterface */
  115. public function checkClientCredentials($client_id, $client_secret = null)
  116. {
  117. $client = $this->getClientDetails($client_id);
  118. return isset($client['client_secret'])
  119. && $client['client_secret'] == $client_secret;
  120. }
  121. public function getClientDetails($client_id)
  122. {
  123. return $this->getValue($this->config['client_key'] . $client_id);
  124. }
  125. public function checkRestrictedGrantType($client_id, $grant_type)
  126. {
  127. $details = $this->getClientDetails($client_id);
  128. if (isset($details['grant_types'])) {
  129. $grant_types = explode(' ', $details['grant_types']);
  130. return in_array($grant_type, (array) $grant_types);
  131. }
  132. // if grant_types are not defined, then none are restricted
  133. return true;
  134. }
  135. public function registerClient($client_id, $client_secret, $redirect_uri)
  136. {
  137. return $this->setValue(
  138. $this->config['client_key'] . $client_id,
  139. compact('client_id', 'client_secret', 'redirect_uri')
  140. );
  141. }
  142. /* RefreshTokenInterface */
  143. public function getRefreshToken($refresh_token)
  144. {
  145. return $this->getValue($this->config['refresh_token_key'] . $refresh_token);
  146. }
  147. public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = null)
  148. {
  149. return $this->setValue(
  150. $this->config['refresh_token_key'] . $refresh_token,
  151. compact('refresh_token', 'client_id', 'user_id', 'expires', 'scope'),
  152. $expires
  153. );
  154. }
  155. public function unsetRefreshToken($refresh_token)
  156. {
  157. return $this->expireValue($this->config['refresh_token_key'] . $refresh_token);
  158. }
  159. /* AccessTokenInterface */
  160. public function getAccessToken($access_token)
  161. {
  162. return $this->getValue($this->config['access_token_key'].$access_token);
  163. }
  164. public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope = null)
  165. {
  166. return $this->setValue(
  167. $this->config['access_token_key'].$access_token,
  168. compact('access_token', 'client_id', 'user_id', 'expires', 'scope'),
  169. $expires
  170. );
  171. }
  172. /*JWTBearerInterface */
  173. public function getClientKey($client_id, $subject)
  174. {
  175. $jwt = $this->getValue($this->config['jwt_key'] . $client_id);
  176. if ( isset($jwt['subject']) && $jwt['subject'] == $subject ) {
  177. return $jwt['key'];
  178. }
  179. return null;
  180. }
  181. }