Skip to content

Commit

Permalink
add method to query all user auth tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristophWurst committed May 18, 2016
1 parent 6231b72 commit 946d330
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/private/Authentication/Token/DefaultTokenMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use OCP\AppFramework\Db\Mapper;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IUser;

class DefaultTokenMapper extends Mapper {

Expand Down Expand Up @@ -83,4 +84,31 @@ public function getToken($token) {
return DefaultToken::fromRow($data);
}

/**
* 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();
$qb->select('id', 'uid', 'password', 'name', 'type', 'token', 'last_activity')
->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;
}

}
14 changes: 14 additions & 0 deletions lib/private/Authentication/Token/DefaultTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IUser;
use OCP\Security\ICrypto;

class DefaultTokenProvider implements IProvider {
Expand Down Expand Up @@ -102,6 +103,19 @@ public function updateToken(IToken $token) {
$this->mapper->update($token);
}

/**
* 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 IToken[]
*/
public function getTokenByUser(IUser $user) {
return $this->mapper->getTokenByUser($user);
}

/**
* Get a token by token id
*
Expand Down
12 changes: 12 additions & 0 deletions lib/private/Authentication/Token/IProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace OC\Authentication\Token;

use OC\Authentication\Exceptions\InvalidTokenException;
use OCP\IUser;

interface IProvider {

Expand Down Expand Up @@ -68,6 +69,17 @@ public function invalidateToken($token);
*/
public function updateToken(IToken $token);

/**
* 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 IToken[]
*/
public function getTokenByUser(IUser $user);

/**
* Get the (unencrypted) password of the given token
*
Expand Down
18 changes: 18 additions & 0 deletions tests/lib/authentication/token/defaulttokenmappertest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,22 @@ public function testGetInvalidToken() {
$this->mapper->getToken($token);
}

public function testGetTokenByUser() {
$user = $this->getMock('\OCP\IUser');
$user->expects($this->once())
->method('getUID')
->will($this->returnValue('user1'));

$this->assertCount(2, $this->mapper->getTokenByUser($user));
}

public function testGetTokenByUserNotFound() {
$user = $this->getMock('\OCP\IUser');
$user->expects($this->once())
->method('getUID')
->will($this->returnValue('user1000'));

$this->assertCount(0, $this->mapper->getTokenByUser($user));
}

}
10 changes: 10 additions & 0 deletions tests/lib/authentication/token/defaulttokenprovidertest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ public function testUpdateToken() {

$this->assertEquals($this->time, $tk->getLastActivity());
}

public function testGetTokenByUser() {
$user = $this->getMock('\OCP\IUser');
$this->mapper->expects($this->once())
->method('getTokenByUser')
->with($user)
->will($this->returnValue(['token']));

$this->assertEquals(['token'], $this->tokenProvider->getTokenByUser($user));
}

public function testGetPassword() {
$token = 'token1234';
Expand Down

0 comments on commit 946d330

Please sign in to comment.