Merge pull request #12886 from nextcloud/td/oauth2/strict_db

Move OAuth2 db code to QBMapper and make it strict
This commit is contained in:
Roeland Jago Douma 2018-12-07 13:37:34 +01:00 committed by GitHub
commit 4f47dff086
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 30 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch> * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
* *
@ -22,11 +23,12 @@
namespace OCA\OAuth2\Db; namespace OCA\OAuth2\Db;
use OCA\OAuth2\Exceptions\AccessTokenNotFoundException; use OCA\OAuth2\Exceptions\AccessTokenNotFoundException;
use OCP\AppFramework\Db\Mapper; use OCP\AppFramework\Db\IMapperException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection; use OCP\IDBConnection;
class AccessTokenMapper extends Mapper { class AccessTokenMapper extends QBMapper {
/** /**
* @param IDBConnection $db * @param IDBConnection $db
@ -40,19 +42,20 @@ class AccessTokenMapper extends Mapper {
* @return AccessToken * @return AccessToken
* @throws AccessTokenNotFoundException * @throws AccessTokenNotFoundException
*/ */
public function getByCode($code) { public function getByCode(string $code): AccessToken {
$qb = $this->db->getQueryBuilder(); $qb = $this->db->getQueryBuilder();
$qb $qb
->select('*') ->select('*')
->from($this->tableName) ->from($this->tableName)
->where($qb->expr()->eq('hashed_code', $qb->createNamedParameter(hash('sha512', $code)))); ->where($qb->expr()->eq('hashed_code', $qb->createNamedParameter(hash('sha512', $code))));
$result = $qb->execute();
$row = $result->fetch(); try {
$result->closeCursor(); $token = $this->findEntity($qb);
if($row === false) { } catch (IMapperException $e) {
throw new AccessTokenNotFoundException(); throw new AccessTokenNotFoundException('Could not find access token', 0, $e);
} }
return AccessToken::fromRow($row);
return $token;
} }
/** /**
@ -60,7 +63,7 @@ class AccessTokenMapper extends Mapper {
* *
* @param int $id * @param int $id
*/ */
public function deleteByClientId($id) { public function deleteByClientId(int $id) {
$qb = $this->db->getQueryBuilder(); $qb = $this->db->getQueryBuilder();
$qb $qb
->delete($this->tableName) ->delete($this->tableName)

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
/** /**
* @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch> * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
* *
@ -22,11 +23,12 @@
namespace OCA\OAuth2\Db; namespace OCA\OAuth2\Db;
use OCA\OAuth2\Exceptions\ClientNotFoundException; use OCA\OAuth2\Exceptions\ClientNotFoundException;
use OCP\AppFramework\Db\Mapper; use OCP\AppFramework\Db\IMapperException;
use OCP\AppFramework\Db\QBMapper;
use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection; use OCP\IDBConnection;
class ClientMapper extends Mapper { class ClientMapper extends QBMapper {
/** /**
* @param IDBConnection $db * @param IDBConnection $db
@ -40,50 +42,50 @@ class ClientMapper extends Mapper {
* @return Client * @return Client
* @throws ClientNotFoundException * @throws ClientNotFoundException
*/ */
public function getByIdentifier($clientIdentifier) { public function getByIdentifier(string $clientIdentifier): Client {
$qb = $this->db->getQueryBuilder(); $qb = $this->db->getQueryBuilder();
$qb $qb
->select('*') ->select('*')
->from($this->tableName) ->from($this->tableName)
->where($qb->expr()->eq('client_identifier', $qb->createNamedParameter($clientIdentifier))); ->where($qb->expr()->eq('client_identifier', $qb->createNamedParameter($clientIdentifier)));
$result = $qb->execute();
$row = $result->fetch(); try {
$result->closeCursor(); $client = $this->findEntity($qb);
if($row === false) { } catch (IMapperException $e) {
throw new ClientNotFoundException(); throw new ClientNotFoundException('could not find client '.$clientIdentifier, 0, $e);
} }
return Client::fromRow($row); return $client;
} }
/** /**
* @param string $uid internal uid of the client * @param int $id internal id of the client
* @return Client * @return Client
* @throws ClientNotFoundException * @throws ClientNotFoundException
*/ */
public function getByUid($uid) { public function getByUid(int $id): Client {
$qb = $this->db->getQueryBuilder(); $qb = $this->db->getQueryBuilder();
$qb $qb
->select('*') ->select('*')
->from($this->tableName) ->from($this->tableName)
->where($qb->expr()->eq('id', $qb->createNamedParameter($uid, IQueryBuilder::PARAM_INT))); ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, IQueryBuilder::PARAM_INT)));
$result = $qb->execute();
$row = $result->fetch(); try {
$result->closeCursor(); $client = $this->findEntity($qb);
if($row === false) { } catch (IMapperException $e) {
throw new ClientNotFoundException(); throw new ClientNotFoundException('could not find client with id '.$id, 0, $e);
} }
return Client::fromRow($row); return $client;
} }
/** /**
* @return Client[] * @return Client[]
*/ */
public function getClients() { public function getClients(): array {
$qb = $this->db->getQueryBuilder(); $qb = $this->db->getQueryBuilder();
$qb $qb
->select('*') ->select('*')
->from($this->tableName); ->from($this->tableName);
return $this->findEntities($qb->getSQL()); return $this->findEntities($qb);
} }
} }