2016-04-25 12:10:55 +00:00
|
|
|
<?php
|
2018-05-15 08:24:46 +00:00
|
|
|
declare(strict_types=1);
|
2016-04-25 12:10:55 +00:00
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-04-25 12:10:55 +00:00
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @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 12:10:55 +00: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,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Authentication\Token;
|
|
|
|
|
2016-04-25 14:40:41 +00:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
2018-05-10 12:47:26 +00:00
|
|
|
use OCP\AppFramework\Db\QBMapper;
|
2016-05-08 15:41:37 +00:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2016-04-25 12:10:55 +00:00
|
|
|
use OCP\IDBConnection;
|
2016-05-18 09:33:56 +00:00
|
|
|
use OCP\IUser;
|
2016-04-25 12:10:55 +00:00
|
|
|
|
2018-05-10 12:47:26 +00:00
|
|
|
class DefaultTokenMapper extends QBMapper {
|
2016-04-25 12:10:55 +00:00
|
|
|
|
|
|
|
public function __construct(IDBConnection $db) {
|
2017-05-18 14:36:50 +00:00
|
|
|
parent::__construct($db, 'authtoken');
|
2016-04-25 12:10:55 +00:00
|
|
|
}
|
|
|
|
|
2016-04-25 14:40:41 +00:00
|
|
|
/**
|
|
|
|
* Invalidate (delete) a given token
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
*/
|
2018-05-15 08:24:46 +00:00
|
|
|
public function invalidate(string $token) {
|
2016-09-06 19:41:15 +00:00
|
|
|
/* @var $qb IQueryBuilder */
|
2016-05-08 15:41:37 +00:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
2017-05-18 14:36:50 +00:00
|
|
|
$qb->delete('authtoken')
|
2016-09-06 19:41:15 +00:00
|
|
|
->where($qb->expr()->eq('token', $qb->createParameter('token')))
|
2016-05-08 15:41:37 +00:00
|
|
|
->setParameter('token', $token)
|
|
|
|
->execute();
|
2016-04-25 14:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $olderThan
|
2016-09-06 19:41:15 +00:00
|
|
|
* @param int $remember
|
2016-04-25 14:40:41 +00:00
|
|
|
*/
|
2018-05-15 08:24:46 +00:00
|
|
|
public function invalidateOld(int $olderThan, int $remember = IToken::DO_NOT_REMEMBER) {
|
2016-05-08 15:41:37 +00:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2017-05-18 14:36:50 +00:00
|
|
|
$qb->delete('authtoken')
|
2016-09-06 19:41:15 +00: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)))
|
2016-05-08 15:41:37 +00:00
|
|
|
->execute();
|
2016-04-25 14:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user UID for the given token
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
* @throws DoesNotExistException
|
2016-04-26 09:32:35 +00:00
|
|
|
* @return DefaultToken
|
2016-04-25 14:40:41 +00:00
|
|
|
*/
|
2018-05-15 08:24:46 +00:00
|
|
|
public function getToken(string $token): DefaultToken {
|
2016-05-08 15:41:37 +00:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2016-08-01 17:06:54 +00:00
|
|
|
$result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope')
|
2017-05-18 14:36:50 +00:00
|
|
|
->from('authtoken')
|
2016-11-11 08:25:42 +00:00
|
|
|
->where($qb->expr()->eq('token', $qb->createNamedParameter($token)))
|
2016-05-08 15:41:37 +00:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$data = $result->fetch();
|
2016-06-17 14:13:11 +00:00
|
|
|
$result->closeCursor();
|
2016-05-08 15:41:37 +00:00
|
|
|
if ($data === false) {
|
|
|
|
throw new DoesNotExistException('token does not exist');
|
|
|
|
}
|
|
|
|
return DefaultToken::fromRow($data);
|
2016-04-25 12:10:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-03 13:57:06 +00:00
|
|
|
/**
|
2016-11-11 08:25:42 +00:00
|
|
|
* Get the token for $id
|
2016-08-03 13:57:06 +00:00
|
|
|
*
|
2018-05-15 08:24:46 +00:00
|
|
|
* @param int $id
|
2016-08-03 13:57:06 +00:00
|
|
|
* @throws DoesNotExistException
|
|
|
|
* @return DefaultToken
|
|
|
|
*/
|
2018-05-15 08:24:46 +00:00
|
|
|
public function getTokenById(int $id): DefaultToken {
|
2016-08-03 13:57:06 +00:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check', 'scope')
|
2017-05-18 14:36:50 +00:00
|
|
|
->from('authtoken')
|
2016-11-11 08:25:42 +00:00
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
2016-08-03 13:57:06 +00:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$data = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
if ($data === false) {
|
|
|
|
throw new DoesNotExistException('token does not exist');
|
2017-07-23 19:03:26 +00:00
|
|
|
}
|
2016-08-03 13:57:06 +00:00
|
|
|
return DefaultToken::fromRow($data);
|
|
|
|
}
|
|
|
|
|
2016-05-18 09:33:56 +00:00
|
|
|
/**
|
2017-07-21 07:50:44 +00:00
|
|
|
* Get all tokens of a user
|
2016-05-18 09:33:56 +00:00
|
|
|
*
|
|
|
|
* The provider may limit the number of result rows in case of an abuse
|
|
|
|
* where a high number of (session) tokens is generated
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
* @return DefaultToken[]
|
|
|
|
*/
|
2018-05-15 08:24:46 +00:00
|
|
|
public function getTokenByUser(IUser $user): array {
|
2016-05-18 09:33:56 +00:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2016-08-01 17:06:54 +00:00
|
|
|
$qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope')
|
2017-05-18 14:36:50 +00:00
|
|
|
->from('authtoken')
|
2016-05-18 09:33:56 +00:00
|
|
|
->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
|
|
|
|
->setMaxResults(1000);
|
|
|
|
$result = $qb->execute();
|
|
|
|
$data = $result->fetchAll();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
$entities = array_map(function ($row) {
|
|
|
|
return DefaultToken::fromRow($row);
|
|
|
|
}, $data);
|
|
|
|
|
|
|
|
return $entities;
|
|
|
|
}
|
|
|
|
|
2016-05-19 09:20:22 +00:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @param int $id
|
|
|
|
*/
|
2018-05-15 08:24:46 +00:00
|
|
|
public function deleteById(IUser $user, int $id) {
|
2016-05-19 09:20:22 +00:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2017-05-18 14:36:50 +00:00
|
|
|
$qb->delete('authtoken')
|
2016-05-19 09:20:22 +00:00
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
|
|
|
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())));
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
2017-05-12 14:14:32 +00:00
|
|
|
/**
|
|
|
|
* delete all auth token which belong to a specific client if the client was deleted
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*/
|
2018-05-15 08:24:46 +00:00
|
|
|
public function deleteByName(string $name) {
|
2017-05-12 14:14:32 +00:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
2017-05-18 14:36:50 +00:00
|
|
|
$qb->delete('authtoken')
|
2017-07-24 09:18:46 +00:00
|
|
|
->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR));
|
2017-05-12 14:14:32 +00:00
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
2016-04-25 12:10:55 +00:00
|
|
|
}
|