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

allow creation of multiple access tokens per client id #65

Merged
merged 2 commits into from
Aug 2, 2017
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
15 changes: 15 additions & 0 deletions appinfo/Migrations/Version20170724162518.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace OCA\oauth2\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Type;
use OCP\Migration\ISchemaMigration;

class Version20170724162518 implements ISchemaMigration {

public function changeSchema(Schema $schema, array $options) {
$prefix = $options['tablePrefix'];
$table = $schema->getTable("{$prefix}oauth2_refresh_tokens");
$table->addColumn('access_token_id', Type::INTEGER, ['notNull' => false]);
}
}
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<name>OAuth 2.0</name>
<description>Application for using OAuth 2.0 in ownCloud</description>
<licence>AGPL</licence>
<author>Project Seminar "sciebo@Learnweb" of the University of Münster</author>
<author>Project Seminar "sciebo@Learnweb" of the University of Münster, Thomas Müller</author>
<version>0.1.1</version>
<namespace>OAuth2</namespace>
<category>integration</category>
Expand Down
11 changes: 7 additions & 4 deletions lib/Controller/OAuthApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public function generateToken($grant_type, $code = null,
$this->logger->info('An authorization code has been used by the client "' . $client->getName() . '" to request an access token.', ['app' => $this->appName]);

$userId = $authorizationCode->getUserId();
$this->authorizationCodeMapper->delete($authorizationCode);

break;
case 'refresh_token':
if (!is_string($refresh_token)) {
Expand All @@ -168,15 +170,15 @@ public function generateToken($grant_type, $code = null,
$this->logger->info('A refresh token has been used by the client "' . $client->getName() . '" to request an access token.', ['app' => $this->appName]);

$userId = $refreshToken->getUserId();
$relatedAccessToken = new AccessToken();
$relatedAccessToken->setId($refreshToken->getAccessTokenId());
$this->accessTokenMapper->delete($relatedAccessToken);
$this->refreshTokenMapper->delete($refreshToken);
break;
default:
return new JSONResponse(['error' => 'invalid_grant'], Http::STATUS_BAD_REQUEST);
}

$this->authorizationCodeMapper->deleteByClientUser($client->getId(), $userId);
$this->accessTokenMapper->deleteByClientUser($client->getId(), $userId);
$this->refreshTokenMapper->deleteByClientUser($client->getId(), $userId);

$token = Utilities::generateRandom();
$accessToken = new AccessToken();
$accessToken->setToken($token);
Expand All @@ -190,6 +192,7 @@ public function generateToken($grant_type, $code = null,
$refreshToken->setToken($token);
$refreshToken->setClientId($client->getId());
$refreshToken->setUserId($userId);
$refreshToken->setAccessTokenId($accessToken->getId());
$this->refreshTokenMapper->insert($refreshToken);

return new JSONResponse(
Expand Down
4 changes: 0 additions & 4 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,6 @@ public function generateAuthorizationCode($response_type, $client_id, $redirect_
return new RedirectResponse(OC_Util::getDefaultPageUrl());
}

$this->authorizationCodeMapper->deleteByClientUser($client->getId(), $this->userId);
$this->accessTokenMapper->deleteByClientUser($client->getId(), $this->userId);
$this->refreshTokenMapper->deleteByClientUser($client->getId(), $this->userId);

$code = Utilities::generateRandom();
$authorizationCode = new AuthorizationCode();
$authorizationCode->setCode($code);
Expand Down
4 changes: 4 additions & 0 deletions lib/Db/RefreshToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,22 @@
* @method void setClientId(int $clientId)
* @method string getUserId()
* @method void setUserId(string $userId)
* @method int getAccessTokenId()
* @method void setAccessTokenId(int $accessTokenId)
*/
class RefreshToken extends Entity {

protected $token;
protected $clientId;
protected $userId;
protected $accessTokenId;

public function __construct() {
$this->addType('id', 'int');
$this->addType('token', 'string');
$this->addType('client_id', 'int');
$this->addType('user_id', 'string');
$this->addType('access_token_id', 'int');
}

}
7 changes: 5 additions & 2 deletions tests/Unit/Controller/OAuthApiControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use OCA\OAuth2\Db\RefreshToken;
use OCA\OAuth2\Db\RefreshTokenMapper;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IURLGenerator;
use PHPUnit_Framework_TestCase;

class OAuthApiControllerTest extends PHPUnit_Framework_TestCase {
Expand Down Expand Up @@ -133,8 +134,10 @@ public function setUp() {
$refreshToken->setToken('GF62kYz7us4yr4Uf1v2IzvsFZaNQZyUZuMIkAJVJaCfz6FM9pecVZXCy3M3amqVV');
$refreshToken->setClientId($this->client1->getId());
$refreshToken->setUserId($this->userId);
$refreshToken->setAccessTokenId($accessToken->getId());
$this->refreshToken = $this->refreshTokenMapper->insert($refreshToken);

/** @var IURLGenerator $urlGenerator */
$urlGenerator = $container->query('ServerContainer')->getURLGenerator();
$this->authorizationSuccessfulMessageUrl = $urlGenerator->linkToRouteAbsolute(
$container->query('AppName') . '.page.authorizationSuccessful'
Expand Down Expand Up @@ -273,8 +276,8 @@ public function testGenerateTokenWithAuthorizationCode() {
$this->assertEquals($this->authorizationSuccessfulMessageUrl, $json->message_url);
$this->assertEquals(200, $result->getStatus());
$this->assertEquals(0, count($this->authorizationCodeMapper->findAll()));
$this->assertEquals(1, count($this->accessTokenMapper->findAll()));
$this->assertEquals(1, count($this->refreshTokenMapper->findAll()));
$this->assertEquals(2, count($this->accessTokenMapper->findAll()));
$this->assertEquals(2, count($this->refreshTokenMapper->findAll()));
}

public function testGenerateTokenWithRefreshToken() {
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Controller/SettingsControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function setUp() {
$refreshToken->setToken('3M3amqVGF62kYz7us4yr4QZyUZuMIAZUf1v2IzvsFJVJaCfz6FM9pecVkVZaNXCy');
$refreshToken->setClientId($this->client->getId());
$refreshToken->setUserId($this->userId);
$refreshToken->setAccessTokenId($accessToken->getId());
$this->refreshTokenMapper->insert($refreshToken);

$this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
Expand Down
26 changes: 23 additions & 3 deletions tests/Unit/Db/RefreshTokenMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

namespace OCA\OAuth2\Tests\Unit\Db;

use Doctrine\DBAL\Platforms\SqlitePlatform;
use OCA\OAuth2\AppInfo\Application;
use OCA\OAuth2\Db\AccessToken;
use OCA\OAuth2\Db\AccessTokenMapper;
use OCA\OAuth2\Db\RefreshToken;
use OCA\OAuth2\Db\RefreshTokenMapper;
use PHPUnit_Framework_TestCase;
Expand All @@ -38,9 +41,6 @@ class RefreshTokenMapperTest extends PHPUnit_Framework_TestCase {
/** @var int $clientId */
private $clientId = 1;

/** @var int $expires */
private $expires = 12;

/** @var RefreshToken $refreshToken1 */
private $refreshToken1;

Expand All @@ -49,6 +49,8 @@ class RefreshTokenMapperTest extends PHPUnit_Framework_TestCase {

/** @var RefreshToken $refreshToken2 */
private $refreshToken2;
/** @var AccessTokenMapper */
private $accessTokenMapper;

public function setUp() {
parent::setUp();
Expand All @@ -59,18 +61,36 @@ public function setUp() {
$this->refreshTokenMapper = $container->query('OCA\OAuth2\Db\RefreshTokenMapper');
$this->refreshTokenMapper->deleteAll();

$this->accessTokenMapper = $container->query(AccessTokenMapper::class);

$accessToken = new AccessToken();
$accessToken->setToken('3M3amqVGF62kYz7us4yr4QZyUZuMIAZUf1v2IzvsFJVJaCfz6FM9pecVkVZaNXCy');
$accessToken->setClientId($this->clientId);
$accessToken->setUserId($this->userId);
$accessToken->resetExpires();
$this->accessTokenMapper->insert($accessToken);

$refreshToken = new RefreshToken();
$refreshToken->setToken($this->token);
$refreshToken->setClientId($this->clientId);
$refreshToken->setUserId($this->userId);
$refreshToken->setAccessTokenId($accessToken->getId());

$this->refreshToken1 = $this->refreshTokenMapper->insert($refreshToken);
$this->id = $this->refreshToken1->getId();

$accessToken = new AccessToken();
$accessToken->setToken('3M3amqVGF62kYz7us4yr4QZyUZuMIAZUf1v2IzvsFJVJaCfz6FM9pecVkVZaNXCy');
$accessToken->setClientId($this->clientId);
$accessToken->setUserId($this->userId);
$accessToken->resetExpires();
$this->accessTokenMapper->insert($accessToken);

$refreshToken = new RefreshToken();
$refreshToken->setToken('XCy4QZI7s4yr3MmkcVv2IzvkVZUf1asFZaYzuGF6uyUZ6FM9pef2AqVzMJ3VJaCN');
$refreshToken->setClientId(1);
$refreshToken->setUserId('max');
$refreshToken->setAccessTokenId($accessToken->getId());
$this->refreshToken2 = $this->refreshTokenMapper->insert($refreshToken);
}

Expand Down