2016-04-25 12:10:55 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-04-25 12:10:55 +00:00
|
|
|
* @author Christoph Wurst <christoph@owncloud.com>
|
|
|
|
*
|
|
|
|
* @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;
|
2016-04-25 12:10:55 +00:00
|
|
|
use OCP\AppFramework\Db\Mapper;
|
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
|
|
|
|
|
|
|
class DefaultTokenMapper extends Mapper {
|
|
|
|
|
|
|
|
public function __construct(IDBConnection $db) {
|
|
|
|
parent::__construct($db, 'authtoken');
|
|
|
|
}
|
|
|
|
|
2016-04-25 14:40:41 +00:00
|
|
|
/**
|
|
|
|
* Invalidate (delete) a given token
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
*/
|
|
|
|
public function invalidate($token) {
|
2016-09-06 19:41:15 +00:00
|
|
|
/* @var $qb IQueryBuilder */
|
2016-05-08 15:41:37 +00:00
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$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
|
|
|
*/
|
2016-09-06 19:41:15 +00:00
|
|
|
public function invalidateOld($olderThan, $remember = IToken::DO_NOT_REMEMBER) {
|
2016-05-08 15:41:37 +00:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$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
|
|
|
*/
|
2016-04-26 09:32:35 +00:00
|
|
|
public function getToken($token) {
|
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')
|
2016-05-08 15:41:37 +00:00
|
|
|
->from('authtoken')
|
|
|
|
->where($qb->expr()->eq('token', $qb->createParameter('token')))
|
|
|
|
->setParameter('token', $token)
|
|
|
|
->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');
|
|
|
|
}
|
2016-08-03 10:03:18 +00:00
|
|
|
;
|
2016-05-08 15:41:37 +00:00
|
|
|
return DefaultToken::fromRow($data);
|
2016-04-25 12:10:55 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 09:33:56 +00:00
|
|
|
/**
|
|
|
|
* Get all token of a user
|
|
|
|
*
|
|
|
|
* 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[]
|
|
|
|
*/
|
|
|
|
public function getTokenByUser(IUser $user) {
|
|
|
|
/* @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')
|
2016-05-18 09:33:56 +00:00
|
|
|
->from('authtoken')
|
|
|
|
->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
|
|
|
|
*/
|
|
|
|
public function deleteById(IUser $user, $id) {
|
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
|
|
|
$qb->delete('authtoken')
|
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
|
|
|
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())));
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
2016-04-25 12:10:55 +00:00
|
|
|
}
|