Pdo.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. <?php
  2. namespace OAuth2\Storage;
  3. use OAuth2\OpenID\Storage\UserClaimsInterface;
  4. use OAuth2\OpenID\Storage\AuthorizationCodeInterface as OpenIDAuthorizationCodeInterface;
  5. use InvalidArgumentException;
  6. /**
  7. * Simple PDO storage for all storage types
  8. *
  9. * NOTE: This class is meant to get users started
  10. * quickly. If your application requires further
  11. * customization, extend this class or create your own.
  12. *
  13. * NOTE: Passwords are stored in plaintext, which is never
  14. * a good idea. Be sure to override this for your application
  15. *
  16. * @author Brent Shaffer <bshafs at gmail dot com>
  17. */
  18. class Pdo implements
  19. AuthorizationCodeInterface,
  20. AccessTokenInterface,
  21. ClientCredentialsInterface,
  22. UserCredentialsInterface,
  23. RefreshTokenInterface,
  24. JwtBearerInterface,
  25. ScopeInterface,
  26. PublicKeyInterface,
  27. UserClaimsInterface,
  28. OpenIDAuthorizationCodeInterface
  29. {
  30. /**
  31. * @var \PDO
  32. */
  33. protected $db;
  34. /**
  35. * @var array
  36. */
  37. protected $config;
  38. /**
  39. * @param mixed $connection
  40. * @param array $config
  41. *
  42. * @throws InvalidArgumentException
  43. */
  44. public function __construct($connection, $config = array())
  45. {
  46. if (!$connection instanceof \PDO) {
  47. if (is_string($connection)) {
  48. $connection = array('dsn' => $connection);
  49. }
  50. if (!is_array($connection)) {
  51. throw new \InvalidArgumentException('First argument to OAuth2\Storage\Pdo must be an instance of PDO, a DSN string, or a configuration array');
  52. }
  53. if (!isset($connection['dsn'])) {
  54. throw new \InvalidArgumentException('configuration array must contain "dsn"');
  55. }
  56. // merge optional parameters
  57. $connection = array_merge(array(
  58. 'username' => null,
  59. 'password' => null,
  60. 'options' => array(),
  61. ), $connection);
  62. $connection = new \PDO($connection['dsn'], $connection['username'], $connection['password'], $connection['options']);
  63. }
  64. $this->db = $connection;
  65. // debugging
  66. $connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
  67. $this->config = array_merge(array(
  68. 'client_table' => 'oauth_clients',
  69. 'access_token_table' => 'oauth_access_tokens',
  70. 'refresh_token_table' => 'oauth_refresh_tokens',
  71. 'code_table' => 'oauth_authorization_codes',
  72. 'user_table' => 'oauth_users',
  73. 'jwt_table' => 'oauth_jwt',
  74. 'jti_table' => 'oauth_jti',
  75. 'scope_table' => 'oauth_scopes',
  76. 'public_key_table' => 'oauth_public_keys',
  77. ), $config);
  78. }
  79. /**
  80. * @param string $client_id
  81. * @param null|string $client_secret
  82. * @return bool
  83. */
  84. public function checkClientCredentials($client_id, $client_secret = null)
  85. {
  86. $stmt = $this->db->prepare(sprintf('SELECT * from %s where client_id = :client_id', $this->config['client_table']));
  87. $stmt->execute(compact('client_id'));
  88. $result = $stmt->fetch(\PDO::FETCH_ASSOC);
  89. // make this extensible
  90. return $result && $result['client_secret'] == $client_secret;
  91. }
  92. /**
  93. * @param string $client_id
  94. * @return bool
  95. */
  96. public function isPublicClient($client_id)
  97. {
  98. $stmt = $this->db->prepare(sprintf('SELECT * from %s where client_id = :client_id', $this->config['client_table']));
  99. $stmt->execute(compact('client_id'));
  100. if (!$result = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  101. return false;
  102. }
  103. return empty($result['client_secret']);
  104. }
  105. /**
  106. * @param string $client_id
  107. * @return array|mixed
  108. */
  109. public function getClientDetails($client_id)
  110. {
  111. $stmt = $this->db->prepare(sprintf('SELECT * from %s where client_id = :client_id', $this->config['client_table']));
  112. $stmt->execute(compact('client_id'));
  113. return $stmt->fetch(\PDO::FETCH_ASSOC);
  114. }
  115. /**
  116. * @param string $client_id
  117. * @param null|string $client_secret
  118. * @param null|string $redirect_uri
  119. * @param null|array $grant_types
  120. * @param null|string $scope
  121. * @param null|string $user_id
  122. * @return bool
  123. */
  124. public function setClientDetails($client_id, $client_secret = null, $redirect_uri = null, $grant_types = null, $scope = null, $user_id = null)
  125. {
  126. // if it exists, update it.
  127. if ($this->getClientDetails($client_id)) {
  128. $stmt = $this->db->prepare($sql = sprintf('UPDATE %s SET client_secret=:client_secret, redirect_uri=:redirect_uri, grant_types=:grant_types, scope=:scope, user_id=:user_id where client_id=:client_id', $this->config['client_table']));
  129. } else {
  130. $stmt = $this->db->prepare(sprintf('INSERT INTO %s (client_id, client_secret, redirect_uri, grant_types, scope, user_id) VALUES (:client_id, :client_secret, :redirect_uri, :grant_types, :scope, :user_id)', $this->config['client_table']));
  131. }
  132. return $stmt->execute(compact('client_id', 'client_secret', 'redirect_uri', 'grant_types', 'scope', 'user_id'));
  133. }
  134. /**
  135. * @param $client_id
  136. * @param $grant_type
  137. * @return bool
  138. */
  139. public function checkRestrictedGrantType($client_id, $grant_type)
  140. {
  141. $details = $this->getClientDetails($client_id);
  142. if (isset($details['grant_types'])) {
  143. $grant_types = explode(' ', $details['grant_types']);
  144. return in_array($grant_type, (array) $grant_types);
  145. }
  146. // if grant_types are not defined, then none are restricted
  147. return true;
  148. }
  149. /**
  150. * @param string $access_token
  151. * @return array|bool|mixed|null
  152. */
  153. public function getAccessToken($access_token)
  154. {
  155. $stmt = $this->db->prepare(sprintf('SELECT * from %s where access_token = :access_token', $this->config['access_token_table']));
  156. $token = $stmt->execute(compact('access_token'));
  157. if ($token = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  158. // convert date string back to timestamp
  159. $token['expires'] = strtotime($token['expires']);
  160. }
  161. return $token;
  162. }
  163. /**
  164. * @param string $access_token
  165. * @param mixed $client_id
  166. * @param mixed $user_id
  167. * @param int $expires
  168. * @param string $scope
  169. * @return bool
  170. */
  171. public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope = null)
  172. {
  173. // convert expires to datestring
  174. $expires = date('Y-m-d H:i:s', $expires);
  175. // if it exists, update it.
  176. if ($this->getAccessToken($access_token)) {
  177. $stmt = $this->db->prepare(sprintf('UPDATE %s SET client_id=:client_id, expires=:expires, user_id=:user_id, scope=:scope where access_token=:access_token', $this->config['access_token_table']));
  178. } else {
  179. $stmt = $this->db->prepare(sprintf('INSERT INTO %s (access_token, client_id, expires, user_id, scope) VALUES (:access_token, :client_id, :expires, :user_id, :scope)', $this->config['access_token_table']));
  180. }
  181. return $stmt->execute(compact('access_token', 'client_id', 'user_id', 'expires', 'scope'));
  182. }
  183. /**
  184. * @param $access_token
  185. * @return bool
  186. */
  187. public function unsetAccessToken($access_token)
  188. {
  189. $stmt = $this->db->prepare(sprintf('DELETE FROM %s WHERE access_token = :access_token', $this->config['access_token_table']));
  190. $stmt->execute(compact('access_token'));
  191. return $stmt->rowCount() > 0;
  192. }
  193. /* OAuth2\Storage\AuthorizationCodeInterface */
  194. /**
  195. * @param string $code
  196. * @return mixed
  197. */
  198. public function getAuthorizationCode($code)
  199. {
  200. $stmt = $this->db->prepare(sprintf('SELECT * from %s where authorization_code = :code', $this->config['code_table']));
  201. $stmt->execute(compact('code'));
  202. if ($code = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  203. // convert date string back to timestamp
  204. $code['expires'] = strtotime($code['expires']);
  205. }
  206. return $code;
  207. }
  208. /**
  209. * @param string $code
  210. * @param mixed $client_id
  211. * @param mixed $user_id
  212. * @param string $redirect_uri
  213. * @param int $expires
  214. * @param string $scope
  215. * @param string $id_token
  216. * @return bool|mixed
  217. */
  218. public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null, $id_token = null)
  219. {
  220. if (func_num_args() > 6) {
  221. // we are calling with an id token
  222. return call_user_func_array(array($this, 'setAuthorizationCodeWithIdToken'), func_get_args());
  223. }
  224. // convert expires to datestring
  225. $expires = date('Y-m-d H:i:s', $expires);
  226. // if it exists, update it.
  227. if ($this->getAuthorizationCode($code)) {
  228. $stmt = $this->db->prepare($sql = sprintf('UPDATE %s SET client_id=:client_id, user_id=:user_id, redirect_uri=:redirect_uri, expires=:expires, scope=:scope where authorization_code=:code', $this->config['code_table']));
  229. } else {
  230. $stmt = $this->db->prepare(sprintf('INSERT INTO %s (authorization_code, client_id, user_id, redirect_uri, expires, scope) VALUES (:code, :client_id, :user_id, :redirect_uri, :expires, :scope)', $this->config['code_table']));
  231. }
  232. return $stmt->execute(compact('code', 'client_id', 'user_id', 'redirect_uri', 'expires', 'scope'));
  233. }
  234. /**
  235. * @param string $code
  236. * @param mixed $client_id
  237. * @param mixed $user_id
  238. * @param string $redirect_uri
  239. * @param string $expires
  240. * @param string $scope
  241. * @param string $id_token
  242. * @return bool
  243. */
  244. private function setAuthorizationCodeWithIdToken($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null, $id_token = null)
  245. {
  246. // convert expires to datestring
  247. $expires = date('Y-m-d H:i:s', $expires);
  248. // if it exists, update it.
  249. if ($this->getAuthorizationCode($code)) {
  250. $stmt = $this->db->prepare($sql = sprintf('UPDATE %s SET client_id=:client_id, user_id=:user_id, redirect_uri=:redirect_uri, expires=:expires, scope=:scope, id_token =:id_token where authorization_code=:code', $this->config['code_table']));
  251. } else {
  252. $stmt = $this->db->prepare(sprintf('INSERT INTO %s (authorization_code, client_id, user_id, redirect_uri, expires, scope, id_token) VALUES (:code, :client_id, :user_id, :redirect_uri, :expires, :scope, :id_token)', $this->config['code_table']));
  253. }
  254. return $stmt->execute(compact('code', 'client_id', 'user_id', 'redirect_uri', 'expires', 'scope', 'id_token'));
  255. }
  256. /**
  257. * @param string $code
  258. * @return bool
  259. */
  260. public function expireAuthorizationCode($code)
  261. {
  262. $stmt = $this->db->prepare(sprintf('DELETE FROM %s WHERE authorization_code = :code', $this->config['code_table']));
  263. return $stmt->execute(compact('code'));
  264. }
  265. /**
  266. * @param string $username
  267. * @param string $password
  268. * @return bool
  269. */
  270. public function checkUserCredentials($username, $password)
  271. {
  272. if ($user = $this->getUser($username)) {
  273. return $this->checkPassword($user, $password);
  274. }
  275. return false;
  276. }
  277. /**
  278. * @param string $username
  279. * @return array|bool
  280. */
  281. public function getUserDetails($username)
  282. {
  283. return $this->getUser($username);
  284. }
  285. /**
  286. * @param mixed $user_id
  287. * @param string $claims
  288. * @return array|bool
  289. */
  290. public function getUserClaims($user_id, $claims)
  291. {
  292. if (!$userDetails = $this->getUserDetails($user_id)) {
  293. return false;
  294. }
  295. $claims = explode(' ', trim($claims));
  296. $userClaims = array();
  297. // for each requested claim, if the user has the claim, set it in the response
  298. $validClaims = explode(' ', self::VALID_CLAIMS);
  299. foreach ($validClaims as $validClaim) {
  300. if (in_array($validClaim, $claims)) {
  301. if ($validClaim == 'address') {
  302. // address is an object with subfields
  303. $userClaims['address'] = $this->getUserClaim($validClaim, $userDetails['address'] ?: $userDetails);
  304. } else {
  305. $userClaims = array_merge($userClaims, $this->getUserClaim($validClaim, $userDetails));
  306. }
  307. }
  308. }
  309. return $userClaims;
  310. }
  311. /**
  312. * @param string $claim
  313. * @param array $userDetails
  314. * @return array
  315. */
  316. protected function getUserClaim($claim, $userDetails)
  317. {
  318. $userClaims = array();
  319. $claimValuesString = constant(sprintf('self::%s_CLAIM_VALUES', strtoupper($claim)));
  320. $claimValues = explode(' ', $claimValuesString);
  321. foreach ($claimValues as $value) {
  322. $userClaims[$value] = isset($userDetails[$value]) ? $userDetails[$value] : null;
  323. }
  324. return $userClaims;
  325. }
  326. /**
  327. * @param string $refresh_token
  328. * @return bool|mixed
  329. */
  330. public function getRefreshToken($refresh_token)
  331. {
  332. $stmt = $this->db->prepare(sprintf('SELECT * FROM %s WHERE refresh_token = :refresh_token', $this->config['refresh_token_table']));
  333. $token = $stmt->execute(compact('refresh_token'));
  334. if ($token = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  335. // convert expires to epoch time
  336. $token['expires'] = strtotime($token['expires']);
  337. }
  338. return $token;
  339. }
  340. /**
  341. * @param string $refresh_token
  342. * @param mixed $client_id
  343. * @param mixed $user_id
  344. * @param string $expires
  345. * @param string $scope
  346. * @return bool
  347. */
  348. public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = null)
  349. {
  350. // convert expires to datestring
  351. $expires = date('Y-m-d H:i:s', $expires);
  352. $stmt = $this->db->prepare(sprintf('INSERT INTO %s (refresh_token, client_id, user_id, expires, scope) VALUES (:refresh_token, :client_id, :user_id, :expires, :scope)', $this->config['refresh_token_table']));
  353. return $stmt->execute(compact('refresh_token', 'client_id', 'user_id', 'expires', 'scope'));
  354. }
  355. /**
  356. * @param string $refresh_token
  357. * @return bool
  358. */
  359. public function unsetRefreshToken($refresh_token)
  360. {
  361. $stmt = $this->db->prepare(sprintf('DELETE FROM %s WHERE refresh_token = :refresh_token', $this->config['refresh_token_table']));
  362. $stmt->execute(compact('refresh_token'));
  363. return $stmt->rowCount() > 0;
  364. }
  365. /**
  366. * plaintext passwords are bad! Override this for your application
  367. *
  368. * @param array $user
  369. * @param string $password
  370. * @return bool
  371. */
  372. protected function checkPassword($user, $password)
  373. {
  374. return $user['password'] == $this->hashPassword($password);
  375. }
  376. // use a secure hashing algorithm when storing passwords. Override this for your application
  377. protected function hashPassword($password)
  378. {
  379. return sha1($password);
  380. }
  381. /**
  382. * @param string $username
  383. * @return array|bool
  384. */
  385. public function getUser($username)
  386. {
  387. $stmt = $this->db->prepare($sql = sprintf('SELECT * from %s where username=:username', $this->config['user_table']));
  388. $stmt->execute(array('username' => $username));
  389. if (!$userInfo = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  390. return false;
  391. }
  392. // the default behavior is to use "username" as the user_id
  393. return array_merge(array(
  394. 'user_id' => $username
  395. ), $userInfo);
  396. }
  397. /**
  398. * plaintext passwords are bad! Override this for your application
  399. *
  400. * @param string $username
  401. * @param string $password
  402. * @param string $firstName
  403. * @param string $lastName
  404. * @return bool
  405. */
  406. public function setUser($username, $password, $firstName = null, $lastName = null)
  407. {
  408. // do not store in plaintext
  409. $password = $this->hashPassword($password);
  410. // if it exists, update it.
  411. if ($this->getUser($username)) {
  412. $stmt = $this->db->prepare($sql = sprintf('UPDATE %s SET password=:password, first_name=:firstName, last_name=:lastName where username=:username', $this->config['user_table']));
  413. } else {
  414. $stmt = $this->db->prepare(sprintf('INSERT INTO %s (username, password, first_name, last_name) VALUES (:username, :password, :firstName, :lastName)', $this->config['user_table']));
  415. }
  416. return $stmt->execute(compact('username', 'password', 'firstName', 'lastName'));
  417. }
  418. /**
  419. * @param string $scope
  420. * @return bool
  421. */
  422. public function scopeExists($scope)
  423. {
  424. $scope = explode(' ', $scope);
  425. $whereIn = implode(',', array_fill(0, count($scope), '?'));
  426. $stmt = $this->db->prepare(sprintf('SELECT count(scope) as count FROM %s WHERE scope IN (%s)', $this->config['scope_table'], $whereIn));
  427. $stmt->execute($scope);
  428. if ($result = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  429. return $result['count'] == count($scope);
  430. }
  431. return false;
  432. }
  433. /**
  434. * @param mixed $client_id
  435. * @return null|string
  436. */
  437. public function getDefaultScope($client_id = null)
  438. {
  439. $stmt = $this->db->prepare(sprintf('SELECT scope FROM %s WHERE is_default=:is_default', $this->config['scope_table']));
  440. $stmt->execute(array('is_default' => true));
  441. if ($result = $stmt->fetchAll(\PDO::FETCH_ASSOC)) {
  442. $defaultScope = array_map(function ($row) {
  443. return $row['scope'];
  444. }, $result);
  445. return implode(' ', $defaultScope);
  446. }
  447. return null;
  448. }
  449. /**
  450. * @param mixed $client_id
  451. * @param $subject
  452. * @return string
  453. */
  454. public function getClientKey($client_id, $subject)
  455. {
  456. $stmt = $this->db->prepare($sql = sprintf('SELECT public_key from %s where client_id=:client_id AND subject=:subject', $this->config['jwt_table']));
  457. $stmt->execute(array('client_id' => $client_id, 'subject' => $subject));
  458. return $stmt->fetchColumn();
  459. }
  460. /**
  461. * @param mixed $client_id
  462. * @return bool|null
  463. */
  464. public function getClientScope($client_id)
  465. {
  466. if (!$clientDetails = $this->getClientDetails($client_id)) {
  467. return false;
  468. }
  469. if (isset($clientDetails['scope'])) {
  470. return $clientDetails['scope'];
  471. }
  472. return null;
  473. }
  474. /**
  475. * @param mixed $client_id
  476. * @param $subject
  477. * @param $audience
  478. * @param $expires
  479. * @param $jti
  480. * @return array|null
  481. */
  482. public function getJti($client_id, $subject, $audience, $expires, $jti)
  483. {
  484. $stmt = $this->db->prepare($sql = sprintf('SELECT * FROM %s WHERE issuer=:client_id AND subject=:subject AND audience=:audience AND expires=:expires AND jti=:jti', $this->config['jti_table']));
  485. $stmt->execute(compact('client_id', 'subject', 'audience', 'expires', 'jti'));
  486. if ($result = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  487. return array(
  488. 'issuer' => $result['issuer'],
  489. 'subject' => $result['subject'],
  490. 'audience' => $result['audience'],
  491. 'expires' => $result['expires'],
  492. 'jti' => $result['jti'],
  493. );
  494. }
  495. return null;
  496. }
  497. /**
  498. * @param mixed $client_id
  499. * @param $subject
  500. * @param $audience
  501. * @param $expires
  502. * @param $jti
  503. * @return bool
  504. */
  505. public function setJti($client_id, $subject, $audience, $expires, $jti)
  506. {
  507. $stmt = $this->db->prepare(sprintf('INSERT INTO %s (issuer, subject, audience, expires, jti) VALUES (:client_id, :subject, :audience, :expires, :jti)', $this->config['jti_table']));
  508. return $stmt->execute(compact('client_id', 'subject', 'audience', 'expires', 'jti'));
  509. }
  510. /**
  511. * @param mixed $client_id
  512. * @return mixed
  513. */
  514. public function getPublicKey($client_id = null)
  515. {
  516. $stmt = $this->db->prepare($sql = sprintf('SELECT public_key FROM %s WHERE client_id=:client_id OR client_id IS NULL ORDER BY client_id IS NOT NULL DESC', $this->config['public_key_table']));
  517. $stmt->execute(compact('client_id'));
  518. if ($result = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  519. return $result['public_key'];
  520. }
  521. }
  522. /**
  523. * @param mixed $client_id
  524. * @return mixed
  525. */
  526. public function getPrivateKey($client_id = null)
  527. {
  528. $stmt = $this->db->prepare($sql = sprintf('SELECT private_key FROM %s WHERE client_id=:client_id OR client_id IS NULL ORDER BY client_id IS NOT NULL DESC', $this->config['public_key_table']));
  529. $stmt->execute(compact('client_id'));
  530. if ($result = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  531. return $result['private_key'];
  532. }
  533. }
  534. /**
  535. * @param mixed $client_id
  536. * @return string
  537. */
  538. public function getEncryptionAlgorithm($client_id = null)
  539. {
  540. $stmt = $this->db->prepare($sql = sprintf('SELECT encryption_algorithm FROM %s WHERE client_id=:client_id OR client_id IS NULL ORDER BY client_id IS NOT NULL DESC', $this->config['public_key_table']));
  541. $stmt->execute(compact('client_id'));
  542. if ($result = $stmt->fetch(\PDO::FETCH_ASSOC)) {
  543. return $result['encryption_algorithm'];
  544. }
  545. return 'RS256';
  546. }
  547. /**
  548. * DDL to create OAuth2 database and tables for PDO storage
  549. *
  550. * @see https://github.com/dsquier/oauth2-server-php-mysql
  551. *
  552. * @param string $dbName
  553. * @return string
  554. */
  555. public function getBuildSql($dbName = 'oauth2_server_php')
  556. {
  557. $sql = "
  558. CREATE TABLE {$this->config['client_table']} (
  559. client_id VARCHAR(80) NOT NULL,
  560. client_secret VARCHAR(80),
  561. redirect_uri VARCHAR(2000),
  562. grant_types VARCHAR(80),
  563. scope VARCHAR(4000),
  564. user_id VARCHAR(80),
  565. PRIMARY KEY (client_id)
  566. );
  567. CREATE TABLE {$this->config['access_token_table']} (
  568. access_token VARCHAR(40) NOT NULL,
  569. client_id VARCHAR(80) NOT NULL,
  570. user_id VARCHAR(80),
  571. expires TIMESTAMP NOT NULL,
  572. scope VARCHAR(4000),
  573. PRIMARY KEY (access_token)
  574. );
  575. CREATE TABLE {$this->config['code_table']} (
  576. authorization_code VARCHAR(40) NOT NULL,
  577. client_id VARCHAR(80) NOT NULL,
  578. user_id VARCHAR(80),
  579. redirect_uri VARCHAR(2000),
  580. expires TIMESTAMP NOT NULL,
  581. scope VARCHAR(4000),
  582. id_token VARCHAR(1000),
  583. PRIMARY KEY (authorization_code)
  584. );
  585. CREATE TABLE {$this->config['refresh_token_table']} (
  586. refresh_token VARCHAR(40) NOT NULL,
  587. client_id VARCHAR(80) NOT NULL,
  588. user_id VARCHAR(80),
  589. expires TIMESTAMP NOT NULL,
  590. scope VARCHAR(4000),
  591. PRIMARY KEY (refresh_token)
  592. );
  593. CREATE TABLE {$this->config['user_table']} (
  594. username VARCHAR(80),
  595. password VARCHAR(80),
  596. first_name VARCHAR(80),
  597. last_name VARCHAR(80),
  598. email VARCHAR(80),
  599. email_verified BOOLEAN,
  600. scope VARCHAR(4000)
  601. );
  602. CREATE TABLE {$this->config['scope_table']} (
  603. scope VARCHAR(80) NOT NULL,
  604. is_default BOOLEAN,
  605. PRIMARY KEY (scope)
  606. );
  607. CREATE TABLE {$this->config['jwt_table']} (
  608. client_id VARCHAR(80) NOT NULL,
  609. subject VARCHAR(80),
  610. public_key VARCHAR(2000) NOT NULL
  611. );
  612. CREATE TABLE {$this->config['jti_table']} (
  613. issuer VARCHAR(80) NOT NULL,
  614. subject VARCHAR(80),
  615. audiance VARCHAR(80),
  616. expires TIMESTAMP NOT NULL,
  617. jti VARCHAR(2000) NOT NULL
  618. );
  619. CREATE TABLE {$this->config['public_key_table']} (
  620. client_id VARCHAR(80),
  621. public_key VARCHAR(2000),
  622. private_key VARCHAR(2000),
  623. encryption_algorithm VARCHAR(100) DEFAULT 'RS256'
  624. )
  625. ";
  626. return $sql;
  627. }
  628. }