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

[12.x] Add access token revoked event #1776

Merged
merged 1 commit into from
Aug 5, 2024
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
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