PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/src/OAuth2/Storage/Memory.php

https://github.com/rich-choy/oauth2-server-php
PHP | 230 lines | 178 code | 37 blank | 15 comment | 17 complexity | aa331333a5bb4381f7c9a19aafbc70f2 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. namespace OAuth2\Storage;
  3. /**
  4. * Simple in-memory storage for all storage types
  5. *
  6. * NOTE: This class should never be used in production, and is
  7. * a stub class for example use only
  8. *
  9. * @author Brent Shaffer <bshafs at gmail dot com>
  10. */
  11. class Memory implements AuthorizationCodeInterface,
  12. UserCredentialsInterface,
  13. AccessTokenInterface,
  14. ClientCredentialsInterface,
  15. RefreshTokenInterface,
  16. JwtBearerInterface,
  17. ScopeInterface
  18. {
  19. private $authorizationCodes;
  20. private $userCredentials;
  21. private $clientCredentials;
  22. private $refreshTokens;
  23. private $accessTokens;
  24. private $jwt;
  25. private $supportedScopes;
  26. private $clientSupportedScopes;
  27. private $clientDefaultScopes;
  28. private $defaultScope;
  29. public function __construct($params = array())
  30. {
  31. $params = array_merge(array(
  32. 'authorization_codes' => array(),
  33. 'user_credentials' => array(),
  34. 'client_credentials' => array(),
  35. 'refresh_tokens' => array(),
  36. 'access_tokens' => array(),
  37. 'jwt' => array(),
  38. 'default_scope' => null,
  39. 'client_supported_scopes' => array(),
  40. 'client_default_scopes' => array(),
  41. 'supported_scopes' => array(),
  42. ), $params);
  43. $this->authorizationCodes = $params['authorization_codes'];
  44. $this->userCredentials = $params['user_credentials'];
  45. $this->clientCredentials = $params['client_credentials'];
  46. $this->refreshTokens = $params['refresh_tokens'];
  47. $this->accessTokens = $params['access_tokens'];
  48. $this->jwt = $params['jwt'];
  49. $this->supportedScopes = $params['supported_scopes'];
  50. $this->clientSupportedScopes = $params['client_supported_scopes'];
  51. $this->clientDefaultScopes = $params['client_default_scopes'];
  52. $this->defaultScope = $params['default_scope'];
  53. }
  54. /* AuthorizationCodeInterface */
  55. public function getAuthorizationCode($code)
  56. {
  57. if (!isset($this->authorizationCodes[$code])) {
  58. return false;
  59. }
  60. return array_merge(array(
  61. 'authorization_code' => $code,
  62. ), $this->authorizationCodes[$code]);
  63. }
  64. public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null)
  65. {
  66. $this->authorizationCodes[$code] = compact('code', 'client_id', 'user_id', 'redirect_uri', 'expires', 'scope');
  67. return true;
  68. }
  69. public function setAuthorizationCodes($authorization_codes)
  70. {
  71. $this->authorizationCodes = $authorization_codes;
  72. }
  73. public function expireAuthorizationCode($code)
  74. {
  75. unset($this->authorizationCodes[$code]);
  76. }
  77. /* UserCredentialsInterface */
  78. public function checkUserCredentials($username, $password)
  79. {
  80. $userDetails = $this->getUserDetails($username);
  81. return $userDetails && $userDetails['password'] && $userDetails['password'] === $password;
  82. }
  83. public function setUser($username, $password, $firstName = null, $lastName = null)
  84. {
  85. $this->userCredentials[$username] = array(
  86. 'password' => $password,
  87. 'first_name' => $firstName,
  88. 'last_name' => $lastName,
  89. );
  90. return true;
  91. }
  92. public function getUserDetails($username)
  93. {
  94. if (!isset($this->userCredentials[$username])) {
  95. return false;
  96. }
  97. return array_merge(array(
  98. 'user_id' => $username,
  99. 'password' => null,
  100. 'first_name' => null,
  101. 'last_name' => null,
  102. ), $this->userCredentials[$username]);
  103. }
  104. /* ClientCredentialsInterface */
  105. public function checkClientCredentials($client_id, $client_secret = null)
  106. {
  107. return isset($this->clientCredentials[$client_id]['client_secret']) && $this->clientCredentials[$client_id]['client_secret'] === $client_secret;
  108. }
  109. public function getClientDetails($client_id)
  110. {
  111. if (!isset($this->clientCredentials[$client_id])) {
  112. return false;
  113. }
  114. $clientDetails = array_merge(array(
  115. 'client_id' => $client_id,
  116. 'client_secret' => null,
  117. 'redirect_uri' => null,
  118. ), $this->clientCredentials[$client_id]);
  119. return $clientDetails;
  120. }
  121. public function checkRestrictedGrantType($client_id, $grant_type)
  122. {
  123. if (isset($this->clientCredentials[$client_id]['grant_types'])) {
  124. $grant_types = explode(' ', $this->clientCredentials[$client_id]['grant_types']);
  125. return in_array($grant_type, $grant_types);
  126. }
  127. // if grant_types are not defined, then none are restricted
  128. return true;
  129. }
  130. public function setClientCredentials($client_credentials)
  131. {
  132. $this->clientCredentials = $client_credentials;
  133. }
  134. /* RefreshTokenInterface */
  135. public function getRefreshToken($refresh_token)
  136. {
  137. return isset($this->refreshTokens[$refresh_token]) ? $this->refreshTokens[$refresh_token] : false;
  138. }
  139. public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = null)
  140. {
  141. $this->refreshTokens[$refresh_token] = compact('refresh_token', 'client_id', 'user_id', 'expires', 'scope');
  142. return true;
  143. }
  144. public function unsetRefreshToken($refresh_token)
  145. {
  146. unset($this->refreshTokens[$refresh_token]);
  147. }
  148. public function setRefreshTokens($refresh_tokens)
  149. {
  150. $this->refreshTokens = $refresh_tokens;
  151. }
  152. /* AccessTokenInterface */
  153. public function getAccessToken($access_token)
  154. {
  155. return isset($this->accessTokens[$access_token]) ? $this->accessTokens[$access_token] : false;
  156. }
  157. public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope = null)
  158. {
  159. $this->accessTokens[$access_token] = compact('access_token', 'client_id', 'user_id', 'expires', 'scope');
  160. return true;
  161. }
  162. public function scopeExists($scope, $client_id = null)
  163. {
  164. $scope = explode(' ', trim($scope));
  165. if (!is_null($client_id) && array_key_exists($client_id, $this->clientSupportedScopes)) {
  166. $allowedScopes = array_merge($this->supportedScopes, $this->clientSupportedScopes[$client_id]);
  167. } else {
  168. $allowedScopes = $this->supportedScopes;
  169. }
  170. return (count(array_diff($scope, $allowedScopes)) == 0);
  171. }
  172. public function getDefaultScope($client_id = null)
  173. {
  174. if ($client_id && array_key_exists($client_id, $this->clientDefaultScopes)) {
  175. return implode(' ', $this->clientDefaultScopes[$client_id]);
  176. }else{
  177. return $this->defaultScope;
  178. }
  179. }
  180. /*JWTBearerInterface */
  181. public function getClientKey($client_id, $subject)
  182. {
  183. if (isset($this->jwt[$client_id])) {
  184. $jwt = $this->jwt[$client_id];
  185. if ($jwt) {
  186. if ($jwt["subject"] == $subject) {
  187. return $jwt["key"];
  188. }
  189. }
  190. }
  191. return false;
  192. }
  193. }