123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- declare(strict_types=1);
- namespace OCA\OAuth2\Db;
- use OCA\OAuth2\Controller\OauthApiController;
- use OCA\OAuth2\Exceptions\AccessTokenNotFoundException;
- use OCP\AppFramework\Db\IMapperException;
- use OCP\AppFramework\Db\QBMapper;
- use OCP\AppFramework\Utility\ITimeFactory;
- use OCP\DB\Exception;
- use OCP\DB\QueryBuilder\IQueryBuilder;
- use OCP\IDBConnection;
- class AccessTokenMapper extends QBMapper {
- public function __construct(
- IDBConnection $db,
- private ITimeFactory $timeFactory,
- ) {
- parent::__construct($db, 'oauth2_access_tokens');
- }
-
- public function getByCode(string $code): AccessToken {
- $qb = $this->db->getQueryBuilder();
- $qb
- ->select('*')
- ->from($this->tableName)
- ->where($qb->expr()->eq('hashed_code', $qb->createNamedParameter(hash('sha512', $code))));
- try {
- $token = $this->findEntity($qb);
- } catch (IMapperException $e) {
- throw new AccessTokenNotFoundException('Could not find access token', 0, $e);
- }
- return $token;
- }
-
- public function deleteByClientId(int $id) {
- $qb = $this->db->getQueryBuilder();
- $qb
- ->delete($this->tableName)
- ->where($qb->expr()->eq('client_id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
- $qb->executeStatement();
- }
-
- public function cleanupExpiredAuthorizationCode(): void {
- $now = $this->timeFactory->now()->getTimestamp();
- $maxTokenCreationTs = $now - OauthApiController::AUTHORIZATION_CODE_EXPIRES_AFTER;
- $qb = $this->db->getQueryBuilder();
- $qb
- ->delete($this->tableName)
- ->where($qb->expr()->eq('token_count', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)))
- ->andWhere($qb->expr()->lt('code_created_at', $qb->createNamedParameter($maxTokenCreationTs, IQueryBuilder::PARAM_INT)));
- $qb->executeStatement();
- }
- }
|