Use constant for token version
And don't set the version in the constructor. That would possible cause to many updates. Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
parent
9e7a95fe58
commit
df34571d1d
9 changed files with 27 additions and 20 deletions
|
@ -41,6 +41,8 @@ use OCP\AppFramework\Db\Entity;
|
|||
*/
|
||||
class DefaultToken extends Entity implements IToken {
|
||||
|
||||
const VERSION = 1;
|
||||
|
||||
/** @var string user UID */
|
||||
protected $uid;
|
||||
|
||||
|
@ -90,8 +92,6 @@ class DefaultToken extends Entity implements IToken {
|
|||
$this->addType('scope', 'string');
|
||||
$this->addType('expires', 'int');
|
||||
$this->addType('version', 'int');
|
||||
|
||||
$this->setVersion(1);
|
||||
}
|
||||
|
||||
public function getId(): int {
|
||||
|
|
|
@ -50,7 +50,7 @@ class DefaultTokenMapper extends QBMapper {
|
|||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->delete('authtoken')
|
||||
->where($qb->expr()->eq('token', $qb->createNamedParameter($token, IQueryBuilder::PARAM_STR)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
|
||||
->execute();
|
||||
}
|
||||
|
||||
|
@ -65,7 +65,7 @@ class DefaultTokenMapper extends QBMapper {
|
|||
->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)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
|
||||
->execute();
|
||||
}
|
||||
|
||||
|
@ -82,7 +82,7 @@ class DefaultTokenMapper extends QBMapper {
|
|||
$result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version')
|
||||
->from('authtoken')
|
||||
->where($qb->expr()->eq('token', $qb->createNamedParameter($token)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
|
||||
->execute();
|
||||
|
||||
$data = $result->fetch();
|
||||
|
@ -106,7 +106,7 @@ class DefaultTokenMapper extends QBMapper {
|
|||
$result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version')
|
||||
->from('authtoken')
|
||||
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
|
||||
->execute();
|
||||
|
||||
$data = $result->fetch();
|
||||
|
@ -132,7 +132,7 @@ class DefaultTokenMapper extends QBMapper {
|
|||
$qb->select('id', 'uid', 'login_name', 'password', 'name', 'token', 'type', 'remember', 'last_activity', 'last_check', 'scope', 'expires', 'version')
|
||||
->from('authtoken')
|
||||
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)))
|
||||
->setMaxResults(1000);
|
||||
$result = $qb->execute();
|
||||
$data = $result->fetchAll();
|
||||
|
@ -151,7 +151,7 @@ class DefaultTokenMapper extends QBMapper {
|
|||
$qb->delete('authtoken')
|
||||
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
||||
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)));
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)));
|
||||
$qb->execute();
|
||||
}
|
||||
|
||||
|
@ -164,7 +164,7 @@ class DefaultTokenMapper extends QBMapper {
|
|||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->delete('authtoken')
|
||||
->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(1, IQueryBuilder::PARAM_INT)));
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(DefaultToken::VERSION, IQueryBuilder::PARAM_INT)));
|
||||
$qb->execute();
|
||||
}
|
||||
|
||||
|
|
|
@ -105,6 +105,7 @@ class DefaultTokenProvider implements IProvider {
|
|||
$dbToken->setRemember($remember);
|
||||
$dbToken->setLastActivity($this->time->getTime());
|
||||
$dbToken->setLastCheck($this->time->getTime());
|
||||
$dbToken->setVersion(DefaultToken::VERSION);
|
||||
|
||||
$this->mapper->insert($dbToken);
|
||||
|
||||
|
|
|
@ -45,6 +45,8 @@ use OCP\AppFramework\Db\Entity;
|
|||
*/
|
||||
class PublicKeyToken extends Entity implements IToken {
|
||||
|
||||
const VERSION = 2;
|
||||
|
||||
/** @var string user UID */
|
||||
protected $uid;
|
||||
|
||||
|
@ -102,8 +104,6 @@ class PublicKeyToken extends Entity implements IToken {
|
|||
$this->addType('publicKey', 'string');
|
||||
$this->addType('privateKey', 'string');
|
||||
$this->addType('version', 'int');
|
||||
|
||||
$this->setVersion(2);
|
||||
}
|
||||
|
||||
public function getId(): int {
|
||||
|
|
|
@ -45,7 +45,7 @@ class PublicKeyTokenMapper extends QBMapper {
|
|||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->delete('authtoken')
|
||||
->where($qb->expr()->eq('token', $qb->createNamedParameter($token)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)))
|
||||
->execute();
|
||||
}
|
||||
|
||||
|
@ -60,7 +60,7 @@ class PublicKeyTokenMapper extends QBMapper {
|
|||
->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)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)))
|
||||
->execute();
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,7 @@ class PublicKeyTokenMapper extends QBMapper {
|
|||
$result = $qb->select('*')
|
||||
->from('authtoken')
|
||||
->where($qb->expr()->eq('token', $qb->createNamedParameter($token)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)))
|
||||
->execute();
|
||||
|
||||
$data = $result->fetch();
|
||||
|
@ -97,7 +97,7 @@ class PublicKeyTokenMapper extends QBMapper {
|
|||
$result = $qb->select('*')
|
||||
->from('authtoken')
|
||||
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)))
|
||||
->execute();
|
||||
|
||||
$data = $result->fetch();
|
||||
|
@ -123,7 +123,7 @@ class PublicKeyTokenMapper extends QBMapper {
|
|||
$qb->select('*')
|
||||
->from('authtoken')
|
||||
->where($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)))
|
||||
->setMaxResults(1000);
|
||||
$result = $qb->execute();
|
||||
$data = $result->fetchAll();
|
||||
|
@ -142,7 +142,7 @@ class PublicKeyTokenMapper extends QBMapper {
|
|||
$qb->delete('authtoken')
|
||||
->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
|
||||
->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($uid)))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT)));
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)));
|
||||
$qb->execute();
|
||||
}
|
||||
|
||||
|
@ -155,7 +155,7 @@ class PublicKeyTokenMapper extends QBMapper {
|
|||
$qb = $this->db->getQueryBuilder();
|
||||
$qb->delete('authtoken')
|
||||
->where($qb->expr()->eq('name', $qb->createNamedParameter($name), IQueryBuilder::PARAM_STR))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT)));
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)));
|
||||
$qb->execute();
|
||||
}
|
||||
|
||||
|
@ -165,7 +165,7 @@ class PublicKeyTokenMapper extends QBMapper {
|
|||
$qb->delete('authtoken')
|
||||
->where($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN)))
|
||||
->andWhere($qb->expr()->neq('id', $qb->createNamedParameter($except->getId())))
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(2, IQueryBuilder::PARAM_INT)));
|
||||
->andWhere($qb->expr()->eq('version', $qb->createNamedParameter(PublicKeyToken::VERSION, IQueryBuilder::PARAM_INT)));
|
||||
|
||||
$qb->execute();
|
||||
}
|
||||
|
|
|
@ -190,7 +190,7 @@ class PublicKeyTokenProvider implements IProvider {
|
|||
throw new InvalidTokenException();
|
||||
}
|
||||
|
||||
// When changeing passwords all temp tokens are deleted
|
||||
// When changing passwords all temp tokens are deleted
|
||||
$this->mapper->deleteTempToken($token);
|
||||
|
||||
// Update the password for all tokens
|
||||
|
@ -314,6 +314,7 @@ class PublicKeyTokenProvider implements IProvider {
|
|||
$dbToken->setRemember($remember);
|
||||
$dbToken->setLastActivity($this->time->getTime());
|
||||
$dbToken->setLastCheck($this->time->getTime());
|
||||
$dbToken->setVersion(PublicKeyToken::VERSION);
|
||||
|
||||
return $dbToken;
|
||||
}
|
||||
|
|
|
@ -135,6 +135,7 @@ class DefaultTokenMapperTest extends TestCase {
|
|||
$token->setRemember(IToken::DO_NOT_REMEMBER);
|
||||
$token->setLastActivity($this->time - 60 * 60 * 24 * 3);
|
||||
$token->setLastCheck($this->time - 10);
|
||||
$token->setVersion(DefaultToken::VERSION);
|
||||
|
||||
$dbToken = $this->mapper->getToken($token->getToken());
|
||||
|
||||
|
@ -164,6 +165,7 @@ class DefaultTokenMapperTest extends TestCase {
|
|||
$token->setRemember(IToken::DO_NOT_REMEMBER);
|
||||
$token->setLastActivity($this->time - 60 * 60 * 24 * 3);
|
||||
$token->setLastCheck($this->time - 10);
|
||||
$token->setVersion(DefaultToken::VERSION);
|
||||
|
||||
$dbToken = $this->mapper->getToken($token->getToken());
|
||||
$token->setId($dbToken->getId()); // We don't know the ID
|
||||
|
|
|
@ -91,6 +91,7 @@ class DefaultTokenProviderTest extends TestCase {
|
|||
$toInsert->setRemember(IToken::DO_NOT_REMEMBER);
|
||||
$toInsert->setLastActivity($this->time);
|
||||
$toInsert->setLastCheck($this->time);
|
||||
$toInsert->setVersion(DefaultToken::VERSION);
|
||||
|
||||
$this->config->expects($this->any())
|
||||
->method('getSystemValue')
|
||||
|
|
|
@ -147,6 +147,7 @@ class PublicKeyTokenMapperTest extends TestCase {
|
|||
$token->setLastCheck($this->time - 10);
|
||||
$token->setPublicKey('public key');
|
||||
$token->setPrivateKey('private key');
|
||||
$token->setVersion(PublicKeyToken::VERSION);
|
||||
|
||||
$dbToken = $this->mapper->getToken($token->getToken());
|
||||
|
||||
|
@ -178,6 +179,7 @@ class PublicKeyTokenMapperTest extends TestCase {
|
|||
$token->setLastCheck($this->time - 10);
|
||||
$token->setPublicKey('public key');
|
||||
$token->setPrivateKey('private key');
|
||||
$token->setVersion(PublicKeyToken::VERSION);
|
||||
|
||||
$dbToken = $this->mapper->getToken($token->getToken());
|
||||
$token->setId($dbToken->getId()); // We don't know the ID
|
||||
|
|
Loading…
Reference in a new issue