CouchbaseDB.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace OAuth2\Storage;
  3. use OAuth2\OpenID\Storage\AuthorizationCodeInterface as OpenIDAuthorizationCodeInterface;
  4. /**
  5. * Simple Couchbase storage for all storage types
  6. *
  7. * This class should be extended or overridden as required
  8. *
  9. * NOTE: Passwords are stored in plaintext, which is never
  10. * a good idea. Be sure to override this for your application
  11. *
  12. * @author Tom Park <tom@raucter.com>
  13. */
  14. class CouchbaseDB implements AuthorizationCodeInterface,
  15. AccessTokenInterface,
  16. ClientCredentialsInterface,
  17. UserCredentialsInterface,
  18. RefreshTokenInterface,
  19. JwtBearerInterface,
  20. OpenIDAuthorizationCodeInterface
  21. {
  22. protected $db;
  23. protected $config;
  24. public function __construct($connection, $config = array())
  25. {
  26. if ($connection instanceof \Couchbase) {
  27. $this->db = $connection;
  28. } else {
  29. if (!is_array($connection) || !is_array($connection['servers'])) {
  30. throw new \InvalidArgumentException('First argument to OAuth2\Storage\CouchbaseDB must be an instance of Couchbase or a configuration array containing a server array');
  31. }
  32. $this->db = new \Couchbase($connection['servers'], (!isset($connection['username'])) ? '' : $connection['username'], (!isset($connection['password'])) ? '' : $connection['password'], $connection['bucket'], false);
  33. }
  34. $this->config = array_merge(array(
  35. 'client_table' => 'oauth_clients',
  36. 'access_token_table' => 'oauth_access_tokens',
  37. 'refresh_token_table' => 'oauth_refresh_tokens',
  38. 'code_table' => 'oauth_authorization_codes',
  39. 'user_table' => 'oauth_users',
  40. 'jwt_table' => 'oauth_jwt',
  41. ), $config);
  42. }
  43. // Helper function to access couchbase item by type:
  44. protected function getObjectByType($name,$id)
  45. {
  46. return json_decode($this->db->get($this->config[$name].'-'.$id),true);
  47. }
  48. // Helper function to set couchbase item by type:
  49. protected function setObjectByType($name,$id,$array)
  50. {
  51. $array['type'] = $name;
  52. return $this->db->set($this->config[$name].'-'.$id,json_encode($array));
  53. }
  54. // Helper function to delete couchbase item by type, wait for persist to at least 1 node
  55. protected function deleteObjectByType($name,$id)
  56. {
  57. $this->db->delete($this->config[$name].'-'.$id,"",1);
  58. }
  59. /* ClientCredentialsInterface */
  60. public function checkClientCredentials($client_id, $client_secret = null)
  61. {
  62. if ($result = $this->getObjectByType('client_table',$client_id)) {
  63. return $result['client_secret'] == $client_secret;
  64. }
  65. return false;
  66. }
  67. public function isPublicClient($client_id)
  68. {
  69. if (!$result = $this->getObjectByType('client_table',$client_id)) {
  70. return false;
  71. }
  72. return empty($result['client_secret']);
  73. }
  74. /* ClientInterface */
  75. public function getClientDetails($client_id)
  76. {
  77. $result = $this->getObjectByType('client_table',$client_id);
  78. return is_null($result) ? false : $result;
  79. }
  80. public function setClientDetails($client_id, $client_secret = null, $redirect_uri = null, $grant_types = null, $scope = null, $user_id = null)
  81. {
  82. if ($this->getClientDetails($client_id)) {
  83. $this->setObjectByType('client_table',$client_id, array(
  84. 'client_id' => $client_id,
  85. 'client_secret' => $client_secret,
  86. 'redirect_uri' => $redirect_uri,
  87. 'grant_types' => $grant_types,
  88. 'scope' => $scope,
  89. 'user_id' => $user_id,
  90. ));
  91. } else {
  92. $this->setObjectByType('client_table',$client_id, array(
  93. 'client_id' => $client_id,
  94. 'client_secret' => $client_secret,
  95. 'redirect_uri' => $redirect_uri,
  96. 'grant_types' => $grant_types,
  97. 'scope' => $scope,
  98. 'user_id' => $user_id,
  99. ));
  100. }
  101. return true;
  102. }
  103. public function checkRestrictedGrantType($client_id, $grant_type)
  104. {
  105. $details = $this->getClientDetails($client_id);
  106. if (isset($details['grant_types'])) {
  107. $grant_types = explode(' ', $details['grant_types']);
  108. return in_array($grant_type, $grant_types);
  109. }
  110. // if grant_types are not defined, then none are restricted
  111. return true;
  112. }
  113. /* AccessTokenInterface */
  114. public function getAccessToken($access_token)
  115. {
  116. $token = $this->getObjectByType('access_token_table',$access_token);
  117. return is_null($token) ? false : $token;
  118. }
  119. public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope = null)
  120. {
  121. // if it exists, update it.
  122. if ($this->getAccessToken($access_token)) {
  123. $this->setObjectByType('access_token_table',$access_token, array(
  124. 'access_token' => $access_token,
  125. 'client_id' => $client_id,
  126. 'expires' => $expires,
  127. 'user_id' => $user_id,
  128. 'scope' => $scope
  129. ));
  130. } else {
  131. $this->setObjectByType('access_token_table',$access_token, array(
  132. 'access_token' => $access_token,
  133. 'client_id' => $client_id,
  134. 'expires' => $expires,
  135. 'user_id' => $user_id,
  136. 'scope' => $scope
  137. ));
  138. }
  139. return true;
  140. }
  141. /* AuthorizationCodeInterface */
  142. public function getAuthorizationCode($code)
  143. {
  144. $code = $this->getObjectByType('code_table',$code);
  145. return is_null($code) ? false : $code;
  146. }
  147. public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null, $id_token = null)
  148. {
  149. // if it exists, update it.
  150. if ($this->getAuthorizationCode($code)) {
  151. $this->setObjectByType('code_table',$code, array(
  152. 'authorization_code' => $code,
  153. 'client_id' => $client_id,
  154. 'user_id' => $user_id,
  155. 'redirect_uri' => $redirect_uri,
  156. 'expires' => $expires,
  157. 'scope' => $scope,
  158. 'id_token' => $id_token,
  159. ));
  160. } else {
  161. $this->setObjectByType('code_table',$code,array(
  162. 'authorization_code' => $code,
  163. 'client_id' => $client_id,
  164. 'user_id' => $user_id,
  165. 'redirect_uri' => $redirect_uri,
  166. 'expires' => $expires,
  167. 'scope' => $scope,
  168. 'id_token' => $id_token,
  169. ));
  170. }
  171. return true;
  172. }
  173. public function expireAuthorizationCode($code)
  174. {
  175. $this->deleteObjectByType('code_table',$code);
  176. return true;
  177. }
  178. /* UserCredentialsInterface */
  179. public function checkUserCredentials($username, $password)
  180. {
  181. if ($user = $this->getUser($username)) {
  182. return $this->checkPassword($user, $password);
  183. }
  184. return false;
  185. }
  186. public function getUserDetails($username)
  187. {
  188. if ($user = $this->getUser($username)) {
  189. $user['user_id'] = $user['username'];
  190. }
  191. return $user;
  192. }
  193. /* RefreshTokenInterface */
  194. public function getRefreshToken($refresh_token)
  195. {
  196. $token = $this->getObjectByType('refresh_token_table',$refresh_token);
  197. return is_null($token) ? false : $token;
  198. }
  199. public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = null)
  200. {
  201. $this->setObjectByType('refresh_token_table',$refresh_token, array(
  202. 'refresh_token' => $refresh_token,
  203. 'client_id' => $client_id,
  204. 'user_id' => $user_id,
  205. 'expires' => $expires,
  206. 'scope' => $scope
  207. ));
  208. return true;
  209. }
  210. public function unsetRefreshToken($refresh_token)
  211. {
  212. $this->deleteObjectByType('refresh_token_table',$refresh_token);
  213. return true;
  214. }
  215. // plaintext passwords are bad! Override this for your application
  216. protected function checkPassword($user, $password)
  217. {
  218. return $user['password'] == $password;
  219. }
  220. public function getUser($username)
  221. {
  222. $result = $this->getObjectByType('user_table',$username);
  223. return is_null($result) ? false : $result;
  224. }
  225. public function setUser($username, $password, $firstName = null, $lastName = null)
  226. {
  227. if ($this->getUser($username)) {
  228. $this->setObjectByType('user_table',$username, array(
  229. 'username' => $username,
  230. 'password' => $password,
  231. 'first_name' => $firstName,
  232. 'last_name' => $lastName
  233. ));
  234. } else {
  235. $this->setObjectByType('user_table',$username, array(
  236. 'username' => $username,
  237. 'password' => $password,
  238. 'first_name' => $firstName,
  239. 'last_name' => $lastName
  240. ));
  241. }
  242. return true;
  243. }
  244. public function getClientKey($client_id, $subject)
  245. {
  246. if (!$jwt = $this->getObjectByType('jwt_table',$client_id)) {
  247. return false;
  248. }
  249. if (isset($jwt['subject']) && $jwt['subject'] == $subject) {
  250. return $jwt['key'];
  251. }
  252. return false;
  253. }
  254. public function getClientScope($client_id)
  255. {
  256. if (!$clientDetails = $this->getClientDetails($client_id)) {
  257. return false;
  258. }
  259. if (isset($clientDetails['scope'])) {
  260. return $clientDetails['scope'];
  261. }
  262. return null;
  263. }
  264. public function getJti($client_id, $subject, $audience, $expiration, $jti)
  265. {
  266. //TODO: Needs couchbase implementation.
  267. throw new \Exception('getJti() for the Couchbase driver is currently unimplemented.');
  268. }
  269. public function setJti($client_id, $subject, $audience, $expiration, $jti)
  270. {
  271. //TODO: Needs couchbase implementation.
  272. throw new \Exception('setJti() for the Couchbase driver is currently unimplemented.');
  273. }
  274. }