1
0

AccessTokenMapper.php 2.3 KB

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