Skip to content

Fixed throwing an error with anonymous users #5

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

Merged
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
2 changes: 1 addition & 1 deletion Security/Firewall/OAuthListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function __invoke(RequestEvent $event)
public function handle(RequestEvent $event): void
{
if (null === $oauthToken = $this->serverService->getBearerToken($event->getRequest(), true)) {
throw new LogicException('Token for event was null');
return;
}
Copy link
Owner

Choose a reason for hiding this comment

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

Just to keep things clean, do you think that you could avoid having the "return" as the "handle" method's return type is "void" so it's technically not supposed to "return". It looks like when the token is null you just want to "do nothing", in which case you might be able to "invert the if" such that the new token gets created only of the oauthToken is not null.

@patrickbussmann what do you think?

And I really like that you made a test to capture this use-case. Thank you for this! :)


$token = new OAuthToken();
Expand Down
26 changes: 26 additions & 0 deletions Tests/Security/Firewall/OAuthListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,30 @@ public function testHandleResponse(): void
// no return, trigger the expectations
$listener->handle($this->event);
}

public function testHandleAnonymousAuthentication(): void
{
$listener = new OAuthListener($this->tokenStorage, $this->authManager, $this->serverService);

$this->serverService
->expects($this->once())
->method('getBearerToken')
->willReturn(null)
;

$this->tokenStorage
->expects($this->never())
->method('setToken')
;

$this->event
->expects($this->never())
->method('setResponse')
;

// no return, trigger the expectations
$listener->handle($this->event);

$this->assertNull($this->tokenStorage->getToken());
}
}