-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #187 from toniperic/revoke-tokens-and-codes
Revoking credentials
- Loading branch information
Showing
10 changed files
with
270 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Trikoder\Bundle\OAuth2Bundle\Service; | ||
|
||
use Trikoder\Bundle\OAuth2Bundle\Model\Client; | ||
|
||
/** | ||
* @api | ||
*/ | ||
interface ClientFinderInterface | ||
{ | ||
public function find(string $identifier): ?Client; | ||
} |
101 changes: 101 additions & 0 deletions
101
Service/CredentialsRevoker/DoctrineCredentialsRevoker.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Trikoder\Bundle\OAuth2Bundle\Service\CredentialsRevoker; | ||
|
||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Trikoder\Bundle\OAuth2Bundle\Model\AccessToken; | ||
use Trikoder\Bundle\OAuth2Bundle\Model\AuthorizationCode; | ||
use Trikoder\Bundle\OAuth2Bundle\Model\Client; | ||
use Trikoder\Bundle\OAuth2Bundle\Model\RefreshToken; | ||
use Trikoder\Bundle\OAuth2Bundle\Service\CredentialsRevokerInterface; | ||
|
||
final class DoctrineCredentialsRevoker implements CredentialsRevokerInterface | ||
{ | ||
/** | ||
* @var EntityManagerInterface | ||
*/ | ||
private $entityManager; | ||
|
||
public function __construct(EntityManagerInterface $entityManager) | ||
{ | ||
$this->entityManager = $entityManager; | ||
} | ||
|
||
public function revokeCredentialsForUser(UserInterface $user): void | ||
{ | ||
$userIdentifier = $user->getUsername(); | ||
|
||
$this->entityManager->createQueryBuilder() | ||
->update(AccessToken::class, 'at') | ||
->set('at.revoked', true) | ||
->where('at.userIdentifier = :userIdentifier') | ||
->setParameter('userIdentifier', $userIdentifier) | ||
->getQuery() | ||
->execute(); | ||
|
||
$queryBuilder = $this->entityManager->createQueryBuilder(); | ||
$queryBuilder | ||
->update(RefreshToken::class, 'rt') | ||
->set('rt.revoked', true) | ||
->where($queryBuilder->expr()->in( | ||
'rt.accessToken', | ||
$this->entityManager->createQueryBuilder() | ||
->select('at.identifier') | ||
->from(AccessToken::class, 'at') | ||
->where('at.userIdentifier = :userIdentifier') | ||
->getDQL() | ||
)) | ||
->setParameter('userIdentifier', $userIdentifier) | ||
->getQuery() | ||
->execute(); | ||
|
||
$this->entityManager->createQueryBuilder() | ||
->update(AuthorizationCode::class, 'ac') | ||
->set('ac.revoked', true) | ||
->where('ac.userIdentifier = :userIdentifier') | ||
->setParameter('userIdentifier', $userIdentifier) | ||
->getQuery() | ||
->execute(); | ||
} | ||
|
||
public function revokeCredentialsForClient(Client $client): void | ||
{ | ||
$doctrineClient = $this->entityManager | ||
->getRepository(Client::class) | ||
->findOneBy(['identifier' => $client->getIdentifier()]); | ||
|
||
$this->entityManager->createQueryBuilder() | ||
->update(AccessToken::class, 'at') | ||
->set('at.revoked', true) | ||
->where('at.client = :client') | ||
->setParameter('client', $doctrineClient) | ||
->getQuery() | ||
->execute(); | ||
|
||
$queryBuilder = $this->entityManager->createQueryBuilder(); | ||
$queryBuilder->update(RefreshToken::class, 'rt') | ||
->set('rt.revoked', true) | ||
->where($queryBuilder->expr()->in( | ||
'rt.accessToken', | ||
$this->entityManager->createQueryBuilder() | ||
->select('at.identifier') | ||
->from(AccessToken::class, 'at') | ||
->where('at.client = :client') | ||
->getDQL() | ||
)) | ||
->setParameter('client', $doctrineClient) | ||
->getQuery() | ||
->execute(); | ||
|
||
$this->entityManager->createQueryBuilder() | ||
->update(AuthorizationCode::class, 'ac') | ||
->set('ac.revoked', true) | ||
->where('ac.client = :client') | ||
->setParameter('client', $doctrineClient) | ||
->getQuery() | ||
->execute(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Trikoder\Bundle\OAuth2Bundle\Service; | ||
|
||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Trikoder\Bundle\OAuth2Bundle\Model\Client; | ||
|
||
/** | ||
* Service responsible for revoking credentials on client-level and user-level. | ||
* Credentials = access tokens, refresh tokens and authorization codes. | ||
* | ||
* @api | ||
*/ | ||
interface CredentialsRevokerInterface | ||
{ | ||
public function revokeCredentialsForUser(UserInterface $user): void; | ||
|
||
public function revokeCredentialsForClient(Client $client): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Trikoder\Bundle\OAuth2Bundle\Tests\Acceptance; | ||
|
||
use DateTimeImmutable; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Trikoder\Bundle\OAuth2Bundle\Model\AccessToken; | ||
use Trikoder\Bundle\OAuth2Bundle\Model\AuthorizationCode; | ||
use Trikoder\Bundle\OAuth2Bundle\Model\Client; | ||
use Trikoder\Bundle\OAuth2Bundle\Model\RefreshToken; | ||
use Trikoder\Bundle\OAuth2Bundle\Service\CredentialsRevoker\DoctrineCredentialsRevoker; | ||
use Trikoder\Bundle\OAuth2Bundle\Tests\Fixtures\FixtureFactory; | ||
|
||
/** | ||
* @TODO This should be in the Integration tests folder but the current tests infrastructure would need improvements first. | ||
* @covers \Trikoder\Bundle\OAuth2Bundle\Service\CredentialsRevoker\DoctrineCredentialsRevoker | ||
*/ | ||
final class DoctrineCredentialsRevokerTest extends AbstractAcceptanceTest | ||
{ | ||
public function testRevokesAllCredentialsForUser(): void | ||
{ | ||
$userIdentifier = FixtureFactory::FIXTURE_USER; | ||
|
||
/** @var EntityManagerInterface $em */ | ||
$em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
|
||
$em->persist($client = new Client('client', 'secret')); | ||
|
||
$authCode = $this->buildAuthCode('foo', '+1 minute', $client, $userIdentifier); | ||
$accessToken = $this->buildAccessToken('bar', '+1 minute', $client, $userIdentifier); | ||
$refreshToken = $this->buildRefreshToken('baz', '+1 minute', $accessToken); | ||
|
||
$em->persist($authCode); | ||
$em->persist($accessToken); | ||
$em->persist($refreshToken); | ||
$em->flush(); | ||
|
||
$revoker = new DoctrineCredentialsRevoker($em); | ||
|
||
$revoker->revokeCredentialsForUser(FixtureFactory::createUser()); | ||
|
||
$em->refresh($authCode); | ||
$em->refresh($accessToken); | ||
$em->refresh($refreshToken); | ||
|
||
$this->assertTrue($authCode->isRevoked()); | ||
$this->assertTrue($accessToken->isRevoked()); | ||
$this->assertTrue($refreshToken->isRevoked()); | ||
} | ||
|
||
public function testRevokesAllCredentialsForClient(): void | ||
{ | ||
/** @var EntityManagerInterface $em */ | ||
$em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
|
||
$em->persist($client = new Client('acme', 'secret')); | ||
|
||
$authCode = $this->buildAuthCode('foo', '+1 minute', $client, 'john'); | ||
$accessToken = $this->buildAccessToken('bar', '+1 minute', $client); | ||
$refreshToken = $this->buildRefreshToken('baz', '+1 minute', $accessToken); | ||
|
||
$em->persist($authCode); | ||
$em->persist($accessToken); | ||
$em->persist($refreshToken); | ||
$em->flush(); | ||
|
||
$revoker = new DoctrineCredentialsRevoker($em); | ||
|
||
$revoker->revokeCredentialsForClient($client); | ||
|
||
$em->refresh($authCode); | ||
$em->refresh($accessToken); | ||
$em->refresh($refreshToken); | ||
|
||
$this->assertTrue($authCode->isRevoked()); | ||
$this->assertTrue($accessToken->isRevoked()); | ||
$this->assertTrue($refreshToken->isRevoked()); | ||
} | ||
|
||
private function buildRefreshToken(string $identifier, string $modify, AccessToken $accessToken): RefreshToken | ||
{ | ||
return new RefreshToken( | ||
$identifier, | ||
new DateTimeImmutable($modify), | ||
$accessToken | ||
); | ||
} | ||
|
||
private function buildAccessToken(string $identifier, string $modify, Client $client, ?string $userIdentifier = null): AccessToken | ||
{ | ||
return new AccessToken( | ||
$identifier, | ||
new DateTimeImmutable($modify), | ||
$client, | ||
$userIdentifier, | ||
[] | ||
); | ||
} | ||
|
||
private function buildAuthCode(string $identifier, string $modify, Client $client, ?string $userIdentifier = null): AuthorizationCode | ||
{ | ||
return new AuthorizationCode( | ||
$identifier, | ||
new DateTimeImmutable($modify), | ||
$client, | ||
$userIdentifier, | ||
[] | ||
); | ||
} | ||
} |