2016-04-25 15:10:55 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-04-25 15:10:55 +03: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 17:40:41 +03:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
2016-04-25 15:10:55 +03:00
|
|
|
use OCP\AppFramework\Db\Mapper;
|
2016-05-08 18:41:37 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2016-04-25 15:10:55 +03:00
|
|
|
use OCP\IDBConnection;
|
2016-05-18 12:33:56 +03:00
|
|
|
use OCP\IUser;
|
2016-04-25 15:10:55 +03:00
|
|
|
|
|
|
|
class DefaultTokenMapper extends Mapper {
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function invalidate($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')
|
2016-09-06 22:41:15 +03:00
|
|
|
->where($qb->expr()->eq('token', $qb->createParameter('token')))
|
2016-05-08 18:41:37 +03:00
|
|
|
->setParameter('token', $token)
|
|
|
|
->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
|
|
|
*/
|
2016-09-06 22:41:15 +03:00
|
|
|
public function invalidateOld($olderThan, $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)))
|
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
|
|
|
*/
|
2016-04-26 12:32:35 +03:00
|
|
|
public function getToken($token) {
|
2016-05-08 18:41:37 +03:00
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2016-08-01 20:06:54 +03:00
|
|
|
$result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope')
|
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)))
|
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');
|
|
|
|
}
|
2016-08-03 13:03:18 +03:00
|
|
|
;
|
2016-05-08 18:41:37 +03:00
|
|
|
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
|
|
|
*
|
2016-11-11 11:25:42 +03:00
|
|
|
* @param string $id
|
2016-08-03 16:57:06 +03:00
|
|
|
* @throws DoesNotExistException
|
|
|
|
* @return DefaultToken
|
|
|
|
*/
|
2016-11-11 11:25:42 +03:00
|
|
|
public function getTokenById($id) {
|
2016-08-03 16:57:06 +03: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 17:36:50 +03:00
|
|
|
->from('authtoken')
|
2016-11-11 11:25:42 +03:00
|
|
|
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
2016-08-03 16:57:06 +03:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$data = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
if ($data === false) {
|
|
|
|
throw new DoesNotExistException('token does not exist');
|
|
|
|
};
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param IUser $user
|
|
|
|
* @return DefaultToken[]
|
|
|
|
*/
|
|
|
|
public function getTokenByUser(IUser $user) {
|
|
|
|
/* @var $qb IQueryBuilder */
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2016-08-01 20:06:54 +03:00
|
|
|
$qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope')
|
2017-05-18 17:36:50 +03:00
|
|
|
->from('authtoken')
|
2016-05-18 12:33:56 +03: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 12:20:22 +03:00
|
|
|
/**
|
|
|
|
* @param IUser $user
|
|
|
|
* @param int $id
|
|
|
|
*/
|
|
|
|
public function deleteById(IUser $user, $id) {
|
|
|
|
/* @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)))
|
|
|
|
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())));
|
|
|
|
$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
|
|
|
|
*/
|
|
|
|
public function deleteByName($name) {
|
|
|
|
$qb = $this->db->getQueryBuilder();
|
2017-05-18 17:36:50 +03:00
|
|
|
$qb->delete('authtoken')
|
2017-05-12 17:14:32 +03:00
|
|
|
->where($qb->expr()->eq('name', $qb->createNamedParameter($name)));
|
|
|
|
$qb->execute();
|
|
|
|
}
|
|
|
|
|
2016-04-25 15:10:55 +03:00
|
|
|
}
|