Memory.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <?php
  2. namespace OAuth2\Storage;
  3. use OAuth2\OpenID\Storage\UserClaimsInterface;
  4. use OAuth2\OpenID\Storage\AuthorizationCodeInterface as OpenIDAuthorizationCodeInterface;
  5. /**
  6. * Simple in-memory storage for all storage types
  7. *
  8. * NOTE: This class should never be used in production, and is
  9. * a stub class for example use only
  10. *
  11. * @author Brent Shaffer <bshafs at gmail dot com>
  12. */
  13. class Memory implements AuthorizationCodeInterface,
  14. UserCredentialsInterface,
  15. UserClaimsInterface,
  16. AccessTokenInterface,
  17. ClientCredentialsInterface,
  18. RefreshTokenInterface,
  19. JwtBearerInterface,
  20. ScopeInterface,
  21. PublicKeyInterface,
  22. OpenIDAuthorizationCodeInterface
  23. {
  24. public $authorizationCodes;
  25. public $userCredentials;
  26. public $clientCredentials;
  27. public $refreshTokens;
  28. public $accessTokens;
  29. public $jwt;
  30. public $jti;
  31. public $supportedScopes;
  32. public $defaultScope;
  33. public $keys;
  34. public function __construct($params = array())
  35. {
  36. $params = array_merge(array(
  37. 'authorization_codes' => array(),
  38. 'user_credentials' => array(),
  39. 'client_credentials' => array(),
  40. 'refresh_tokens' => array(),
  41. 'access_tokens' => array(),
  42. 'jwt' => array(),
  43. 'jti' => array(),
  44. 'default_scope' => null,
  45. 'supported_scopes' => array(),
  46. 'keys' => array(),
  47. ), $params);
  48. $this->authorizationCodes = $params['authorization_codes'];
  49. $this->userCredentials = $params['user_credentials'];
  50. $this->clientCredentials = $params['client_credentials'];
  51. $this->refreshTokens = $params['refresh_tokens'];
  52. $this->accessTokens = $params['access_tokens'];
  53. $this->jwt = $params['jwt'];
  54. $this->jti = $params['jti'];
  55. $this->supportedScopes = $params['supported_scopes'];
  56. $this->defaultScope = $params['default_scope'];
  57. $this->keys = $params['keys'];
  58. }
  59. /* AuthorizationCodeInterface */
  60. public function getAuthorizationCode($code)
  61. {
  62. if (!isset($this->authorizationCodes[$code])) {
  63. return false;
  64. }
  65. return array_merge(array(
  66. 'authorization_code' => $code,
  67. ), $this->authorizationCodes[$code]);
  68. }
  69. public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null, $id_token = null)
  70. {
  71. $this->authorizationCodes[$code] = compact('code', 'client_id', 'user_id', 'redirect_uri', 'expires', 'scope', 'id_token');
  72. return true;
  73. }
  74. public function setAuthorizationCodes($authorization_codes)
  75. {
  76. $this->authorizationCodes = $authorization_codes;
  77. }
  78. public function expireAuthorizationCode($code)
  79. {
  80. unset($this->authorizationCodes[$code]);
  81. }
  82. /* UserCredentialsInterface */
  83. public function checkUserCredentials($username, $password)
  84. {
  85. $userDetails = $this->getUserDetails($username);
  86. return $userDetails && $userDetails['password'] && $userDetails['password'] === $password;
  87. }
  88. public function setUser($username, $password, $firstName = null, $lastName = null)
  89. {
  90. $this->userCredentials[$username] = array(
  91. 'password' => $password,
  92. 'first_name' => $firstName,
  93. 'last_name' => $lastName,
  94. );
  95. return true;
  96. }
  97. public function getUserDetails($username)
  98. {
  99. if (!isset($this->userCredentials[$username])) {
  100. return false;
  101. }
  102. return array_merge(array(
  103. 'user_id' => $username,
  104. 'password' => null,
  105. 'first_name' => null,
  106. 'last_name' => null,
  107. ), $this->userCredentials[$username]);
  108. }
  109. /* UserClaimsInterface */
  110. public function getUserClaims($user_id, $claims)
  111. {
  112. if (!$userDetails = $this->getUserDetails($user_id)) {
  113. return false;
  114. }
  115. $claims = explode(' ', trim($claims));
  116. $userClaims = array();
  117. // for each requested claim, if the user has the claim, set it in the response
  118. $validClaims = explode(' ', self::VALID_CLAIMS);
  119. foreach ($validClaims as $validClaim) {
  120. if (in_array($validClaim, $claims)) {
  121. if ($validClaim == 'address') {
  122. // address is an object with subfields
  123. $userClaims['address'] = $this->getUserClaim($validClaim, $userDetails['address'] ?: $userDetails);
  124. } else {
  125. $userClaims = array_merge($this->getUserClaim($validClaim, $userDetails));
  126. }
  127. }
  128. }
  129. return $userClaims;
  130. }
  131. protected function getUserClaim($claim, $userDetails)
  132. {
  133. $userClaims = array();
  134. $claimValuesString = constant(sprintf('self::%s_CLAIM_VALUES', strtoupper($claim)));
  135. $claimValues = explode(' ', $claimValuesString);
  136. foreach ($claimValues as $value) {
  137. $userClaims[$value] = isset($userDetails[$value]) ? $userDetails[$value] : null;
  138. }
  139. return $userClaims;
  140. }
  141. /* ClientCredentialsInterface */
  142. public function checkClientCredentials($client_id, $client_secret = null)
  143. {
  144. return isset($this->clientCredentials[$client_id]['client_secret']) && $this->clientCredentials[$client_id]['client_secret'] === $client_secret;
  145. }
  146. public function isPublicClient($client_id)
  147. {
  148. if (!isset($this->clientCredentials[$client_id])) {
  149. return false;
  150. }
  151. return empty($this->clientCredentials[$client_id]['client_secret']);
  152. }
  153. /* ClientInterface */
  154. public function getClientDetails($client_id)
  155. {
  156. if (!isset($this->clientCredentials[$client_id])) {
  157. return false;
  158. }
  159. $clientDetails = array_merge(array(
  160. 'client_id' => $client_id,
  161. 'client_secret' => null,
  162. 'redirect_uri' => null,
  163. 'scope' => null,
  164. ), $this->clientCredentials[$client_id]);
  165. return $clientDetails;
  166. }
  167. public function checkRestrictedGrantType($client_id, $grant_type)
  168. {
  169. if (isset($this->clientCredentials[$client_id]['grant_types'])) {
  170. $grant_types = explode(' ', $this->clientCredentials[$client_id]['grant_types']);
  171. return in_array($grant_type, $grant_types);
  172. }
  173. // if grant_types are not defined, then none are restricted
  174. return true;
  175. }
  176. public function setClientDetails($client_id, $client_secret = null, $redirect_uri = null, $grant_types = null, $scope = null, $user_id = null)
  177. {
  178. $this->clientCredentials[$client_id] = array(
  179. 'client_id' => $client_id,
  180. 'client_secret' => $client_secret,
  181. 'redirect_uri' => $redirect_uri,
  182. 'grant_types' => $grant_types,
  183. 'scope' => $scope,
  184. 'user_id' => $user_id,
  185. );
  186. return true;
  187. }
  188. /* RefreshTokenInterface */
  189. public function getRefreshToken($refresh_token)
  190. {
  191. return isset($this->refreshTokens[$refresh_token]) ? $this->refreshTokens[$refresh_token] : false;
  192. }
  193. public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = null)
  194. {
  195. $this->refreshTokens[$refresh_token] = compact('refresh_token', 'client_id', 'user_id', 'expires', 'scope');
  196. return true;
  197. }
  198. public function unsetRefreshToken($refresh_token)
  199. {
  200. unset($this->refreshTokens[$refresh_token]);
  201. }
  202. public function setRefreshTokens($refresh_tokens)
  203. {
  204. $this->refreshTokens = $refresh_tokens;
  205. }
  206. /* AccessTokenInterface */
  207. public function getAccessToken($access_token)
  208. {
  209. return isset($this->accessTokens[$access_token]) ? $this->accessTokens[$access_token] : false;
  210. }
  211. public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope = null, $id_token = null)
  212. {
  213. $this->accessTokens[$access_token] = compact('access_token', 'client_id', 'user_id', 'expires', 'scope', 'id_token');
  214. return true;
  215. }
  216. public function unsetAccessToken($access_token)
  217. {
  218. unset($this->accessTokens[$access_token]);
  219. }
  220. public function scopeExists($scope)
  221. {
  222. $scope = explode(' ', trim($scope));
  223. return (count(array_diff($scope, $this->supportedScopes)) == 0);
  224. }
  225. public function getDefaultScope($client_id = null)
  226. {
  227. return $this->defaultScope;
  228. }
  229. /*JWTBearerInterface */
  230. public function getClientKey($client_id, $subject)
  231. {
  232. if (isset($this->jwt[$client_id])) {
  233. $jwt = $this->jwt[$client_id];
  234. if ($jwt) {
  235. if ($jwt["subject"] == $subject) {
  236. return $jwt["key"];
  237. }
  238. }
  239. }
  240. return false;
  241. }
  242. public function getClientScope($client_id)
  243. {
  244. if (!$clientDetails = $this->getClientDetails($client_id)) {
  245. return false;
  246. }
  247. if (isset($clientDetails['scope'])) {
  248. return $clientDetails['scope'];
  249. }
  250. return null;
  251. }
  252. public function getJti($client_id, $subject, $audience, $expires, $jti)
  253. {
  254. foreach ($this->jti as $storedJti) {
  255. if ($storedJti['issuer'] == $client_id && $storedJti['subject'] == $subject && $storedJti['audience'] == $audience && $storedJti['expires'] == $expires && $storedJti['jti'] == $jti) {
  256. return array(
  257. 'issuer' => $storedJti['issuer'],
  258. 'subject' => $storedJti['subject'],
  259. 'audience' => $storedJti['audience'],
  260. 'expires' => $storedJti['expires'],
  261. 'jti' => $storedJti['jti']
  262. );
  263. }
  264. }
  265. return null;
  266. }
  267. public function setJti($client_id, $subject, $audience, $expires, $jti)
  268. {
  269. $this->jti[] = array('issuer' => $client_id, 'subject' => $subject, 'audience' => $audience, 'expires' => $expires, 'jti' => $jti);
  270. }
  271. /*PublicKeyInterface */
  272. public function getPublicKey($client_id = null)
  273. {
  274. if (isset($this->keys[$client_id])) {
  275. return $this->keys[$client_id]['public_key'];
  276. }
  277. // use a global encryption pair
  278. if (isset($this->keys['public_key'])) {
  279. return $this->keys['public_key'];
  280. }
  281. return false;
  282. }
  283. public function getPrivateKey($client_id = null)
  284. {
  285. if (isset($this->keys[$client_id])) {
  286. return $this->keys[$client_id]['private_key'];
  287. }
  288. // use a global encryption pair
  289. if (isset($this->keys['private_key'])) {
  290. return $this->keys['private_key'];
  291. }
  292. return false;
  293. }
  294. public function getEncryptionAlgorithm($client_id = null)
  295. {
  296. if (isset($this->keys[$client_id]['encryption_algorithm'])) {
  297. return $this->keys[$client_id]['encryption_algorithm'];
  298. }
  299. // use a global encryption algorithm
  300. if (isset($this->keys['encryption_algorithm'])) {
  301. return $this->keys['encryption_algorithm'];
  302. }
  303. return 'RS256';
  304. }
  305. }