2016-04-25 15:10:55 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-05-15 11:24:46 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2016-04-25 15:10:55 +03:00
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
* @author Marcel Waldvogel <marcel.waldvogel@uni-konstanz.de>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-04-25 15:10:55 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-04-25 15:10:55 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Authentication\Token;
|
|
|
|
|
2016-04-25 17:40:41 +03:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
2018-05-10 15:47:26 +03:00
|
|
|
use OCP\AppFramework\Db\QBMapper;
|
2016-05-08 18:41:37 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2016-04-25 15:10:55 +03:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
|
2018-05-10 15:47:26 +03:00
|
|
|
class DefaultTokenMapper extends QBMapper {
|
2016-04-25 15:10:55 +03:00
|
|
|
|
|
|
|
public function __construct(IDBConnection $db) {
|
2017-05-18 17:36:50 +03:00
|
|
|
parent::__construct($db, 'authtoken');
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
|
|
|
|
2016-04-25 17:40:41 +03:00
|
|
|
/**
|
|
|
|
* Invalidate (delete) a given token
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
*/
|
2018-05-15 11:24:46 +03:00
|
|
|
public function invalidate(string $token) {
|
2016-09-06 22:41:15 +03:00
|
|
|
/* @var $qb IQueryBuilder */
|
2016-05-08 18:41:37 +03:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
2017-05-18 17:36:50 +03:00
|
|
|
$qb->delete('authtoken')
|
2018-05-18 20:48:08 +03:00
|
|
|
->where($qb->expr()->eq('token', $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR)))
|
2018-06-04 10:51:13 +03:00
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
|
2016-05-08 18:41:37 +03:00
|
|
|
->execute();
|
2016-04-25 17:40:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $olderThan
|
2016-09-06 22:41:15 +03:00
|
|
|
* @param int $remember
|
2016-04-25 17:40:41 +03:00
|
|
|
*/
|
2018-05-15 11:24:46 +03:00
|
|
|
public function invalidateOld(int $olderThan, int $remember = IToken::DO_NOT_REMEMBER) {
|
2016-05-08 18:41:37 +03:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2017-05-18 17:36:50 +03:00
|
|
|
$qb->delete('authtoken')
|
2016-09-06 22:41:15 +03:00
|
|
|
->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT)))
|
|
|
|
->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT)))
|
2018-06-04 10:51:13 +03:00
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
|
2016-05-08 18:41:37 +03:00
|
|
|
->execute();
|
2016-04-25 17:40:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user UID for the given token
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
* @throws DoesNotExistException
|
2016-04-26 12:32:35 +03:00
|
|
|
* @return DefaultToken
|
2016-04-25 17:40:41 +03:00
|
|
|
*/
|
2018-05-15 11:24:46 +03:00
|
|
|
public function getToken(string $token): DefaultToken {
|
2016-05-08 18:41:37 +03:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2018-05-18 20:48:08 +03:00
|
|
|
$result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version')
|
2017-05-18 17:36:50 +03:00
|
|
|
->from('authtoken')
|
2016-11-11 11:25:42 +03:00
|
|
|
->where($qb->expr()->eq('token', $qb->createNamedParameter($token)))
|
2018-06-04 10:51:13 +03:00
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
|
2016-05-08 18:41:37 +03:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$data = $result->fetch();
|
2016-06-17 17:13:11 +03:00
|
|
|
$result->closeCursor();
|
2016-05-08 18:41:37 +03:00
|
|
|
if ($data === false) {
|
|
|
|
throw new DoesNotExistException('token does not exist');
|
|
|
|
}
|
|
|
|
return DefaultToken::fromRow($data);
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|
|
|
|
|
2016-08-03 16:57:06 +03:00
|
|
|
/**
|
2016-11-11 11:25:42 +03:00
|
|
|
* Get the token for $id
|
2016-08-03 16:57:06 +03:00
|
|
|
*
|
2018-05-15 11:24:46 +03:00
|
|
|
* @param int $id
|
2016-08-03 16:57:06 +03:00
|
|
|
* @throws DoesNotExistException
|
|
|
|
* @return DefaultToken
|
|
|
|
*/
|
2018-05-15 11:24:46 +03:00
|
|
|
public function getTokenById(int $id): DefaultToken {
|
2016-08-03 16:57:06 +03:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2018-05-18 20:48:08 +03:00
|
|
|
$result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version')
|
2017-05-18 17:36:50 +03:00
|
|
|
->from('authtoken')
|
2016-11-11 11:25:42 +03:00
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
2018-06-04 10:51:13 +03:00
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
|
2016-08-03 16:57:06 +03:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$data = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
if ($data === false) {
|
|
|
|
throw new DoesNotExistException('token does not exist');
|
2017-07-23 22:03:26 +03:00
|
|
|
}
|
2016-08-03 16:57:06 +03:00
|
|
|
return DefaultToken::fromRow($data);
|
|
|
|
}
|
|
|
|
|
2016-05-18 12:33:56 +03:00
|
|
|
/**
|
2017-07-21 10:50:44 +03:00
|
|
|
* Get all tokens of a user
|
2016-05-18 12:33:56 +03:00
|
|
|
*
|
|
|
|
* The provider may limit the number of result rows in case of an abuse
|
|
|
|
* where a high number of (session) tokens is generated
|
|
|
|
*
|
2018-05-29 10:29:29 +03:00
|
|
|
* @param string $uid
|
2016-05-18 12:33:56 +03:00
|
|
|
* @return DefaultToken[]
|
|
|
|
*/
|
2018-05-29 10:29:29 +03:00
|
|
|
public function getTokenByUser(string $uid): array {
|
2016-05-18 12:33:56 +03:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2018-05-18 20:48:08 +03:00
|
|
|
$qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version')
|
2017-05-18 17:36:50 +03:00
|
|
|
->from('authtoken')
|
2018-05-29 10:29:29 +03:00
|
|
|
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
|
2018-06-04 10:51:13 +03:00
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
|
2016-05-18 12:33:56 +03:00
|
|
|
->setMaxResults(1000);
|
|
|
|
$result = $qb->execute();
|
|
|
|
$data = $result->fetchAll();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
$entities = array_map(function ($row) {
|
|
|
|
return DefaultToken::fromRow($row);
|
|
|
|
}, $data);
|
|
|
|
|
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
|
2018-05-29 10:29:29 +03:00
|
|
|
public function deleteById(string $uid, int $id) {
|
2016-05-19 12:20:22 +03:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2017-05-18 17:36:50 +03:00
|
|
|
$qb->delete('authtoken')
|
2016-05-19 12:20:22 +03:00
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
2018-05-29 10:29:29 +03:00
|
|
|
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
|
2018-06-04 10:51:13 +03:00
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)));
|
2016-05-19 12:20:22 +03:00
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
2017-05-12 17:14:32 +03:00
|
|
|
/**
|
|
|
|
* delete all auth token which belong to a specific client if the client was deleted
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2018-05-15 11:24:46 +03:00
|
|
|
public function deleteByName(string $name) {
|
2017-05-12 17:14:32 +03:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
2017-05-18 17:36:50 +03:00
|
|
|
$qb->delete('authtoken')
|
2018-05-18 20:48:08 +03:00
|
|
|
->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR))
|
2018-06-04 10:51:13 +03:00
|
|
|
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)));
|
2017-05-12 17:14:32 +03:00
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|