AccessTokenMapper.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\OAuth2\Db;
  22. use OCA\OAuth2\Exceptions\AccessTokenNotFoundException;
  23. use OCP\AppFramework\Db\Mapper;
  24. use OCP\DB\QueryBuilder\IQueryBuilder;
  25. use OCP\IDBConnection;
  26. class AccessTokenMapper extends Mapper {
  27. /**
  28. * @param IDBConnection $db
  29. */
  30. public function __construct(IDBConnection $db) {
  31. parent::__construct($db, 'oauth2_access_tokens');
  32. }
  33. /**
  34. * @param string $code
  35. * @return AccessToken
  36. * @throws AccessTokenNotFoundException
  37. */
  38. public function getByCode($code) {
  39. $qb = $this->db->getQueryBuilder();
  40. $qb
  41. ->select('*')
  42. ->from($this->tableName)
  43. ->where($qb->expr()->eq('hashed_code', $qb->createNamedParameter(hash('sha512', $code))));
  44. $result = $qb->execute();
  45. $row = $result->fetch();
  46. $result->closeCursor();
  47. if($row === false) {
  48. throw new AccessTokenNotFoundException();
  49. }
  50. return AccessToken::fromRow($row);
  51. }
  52. /**
  53. * delete all access token from a given client
  54. *
  55. * @param int $id
  56. */
  57. public function deleteByClientId($id) {
  58. $qb = $this->db->getQueryBuilder();
  59. $qb
  60. ->delete($this->tableName)
  61. ->where($qb->expr()->eq('client_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
  62. $qb->execute();
  63. }
  64. }