Skip to content

Commit

Permalink
Add access token revoked event (#1776)
Browse files Browse the repository at this point in the history
  • Loading branch information
axlon committed Aug 5, 2024
1 parent ead5067 commit ca63a86
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Bridge/AccessTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DateTime;
use Illuminate\Contracts\Events\Dispatcher;
use Laravel\Passport\Events\AccessTokenCreated;
use Laravel\Passport\Events\AccessTokenRevoked;
use Laravel\Passport\Passport;
use Laravel\Passport\TokenRepository;
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
Expand Down Expand Up @@ -78,7 +79,9 @@ public function persistNewAccessToken(AccessTokenEntityInterface $accessTokenEnt
*/
public function revokeAccessToken($tokenId)
{
$this->tokenRepository->revokeAccessToken($tokenId);
if ($this->tokenRepository->revokeAccessToken($tokenId)) {
$this->events->dispatch(new AccessTokenRevoked($tokenId));
}
}

/**
Expand Down
17 changes: 17 additions & 0 deletions src/Events/AccessTokenRevoked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Laravel\Passport\Events;

class AccessTokenRevoked
{
/**
* Create a new event instance.
*
* @param string $tokenId
* @return void
*/
public function __construct(
public string $tokenId,
) {
}
}
28 changes: 28 additions & 0 deletions tests/Unit/BridgeAccessTokenRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,34 @@ public function test_access_tokens_can_be_persisted()
$repository->persistNewAccessToken($accessToken);
}

public function test_access_tokens_can_be_revoked()
{
$tokenRepository = m::mock(TokenRepository::class);
$events = m::mock(Dispatcher::class);

$tokenRepository->shouldReceive('revokeAccessToken')->with('token-id')->once()->andReturn(1);
$events->shouldReceive('dispatch')->once();

$repository = new AccessTokenRepository($tokenRepository, $events);
$repository->revokeAccessToken('token-id');

$this->expectNotToPerformAssertions();
}

public function test_access_token_revoke_event_is_not_dispatched_when_nothing_happened()
{
$tokenRepository = m::mock(TokenRepository::class);
$events = m::mock(Dispatcher::class);

$tokenRepository->shouldReceive('revokeAccessToken')->with('token-id')->once()->andReturn(0);
$events->shouldNotReceive('dispatch');

$repository = new AccessTokenRepository($tokenRepository, $events);
$repository->revokeAccessToken('token-id');

$this->expectNotToPerformAssertions();
}

public function test_can_get_new_access_token()
{
$tokenRepository = m::mock(TokenRepository::class);
Expand Down

0 comments on commit ca63a86

Please sign in to comment.