Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add method to query all user auth tokens #24686

Merged
merged 1 commit into from
May 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does not match interface

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which interface?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its missing the warning

 * The provider may limit the number of result rows in case of an abuse
 * where a high number of (session) tokens is generated

If you don't want to repeat yourself, use

/**
 * {@inheritdoc}
 */

https://manual.phpdoc.org/HTMLSmartyConverter/HandS/phpDocumentor/tutorial_tags.inlineinheritdoc.pkg.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please check again, there is no interface the mapper implements. DefaultTokenProvider implements IProvider and has the right doc block 😉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I ment lib/private/Authentication/Token/DefaultTokenProvider.php well then copy the doc lines from there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added…

*
* 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