Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ protected function extractUsername(Request $request): ?string
throw new BadCredentialsException(sprintf('User key was not found: "%s".', $this->userKey));
}

return $request->server->get($this->userKey);
return $request->server->get($this->userKey) ?: null;
Copy link
Author

@ghost ghost Jan 28, 2025

Choose a reason for hiding this comment

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

Looking at how the method is used I wonder if we shouldn’t throw a BadCredentialsException here then.
(xabbuh's comment on previous PR)

Copy link
Author

@ghost ghost Jan 28, 2025

Choose a reason for hiding this comment

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

Throwing an exception would be consistent with the FormLoginAuthenticator's behaviour :

if ('' === $credentials['username']) {
throw new BadCredentialsException(\sprintf('The key "%s" must be a non-empty string.', $this->options['username_parameter']));

OTOH the base class' abstract extractUsername() returns a nullable string and the null check in supports() indicates this is due to being unable to extract a username. Other users who've extended the base class may expect that behaviour, so throwing an exception (in a reference implementation) may be confusing.

abstract protected function extractUsername(Request $request): ?string;

if (null === $username) {
$this->logger?->debug('Skipping pre-authenticated authenticator no username could be extracted.', ['authenticator' => static::class]);

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense to me

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function testSupportNoUser()
$authenticator = new RemoteUserAuthenticator(new InMemoryUserProvider(), new TokenStorage(), 'main');

$this->assertFalse($authenticator->supports($this->createRequest([])));
$this->assertFalse($authenticator->supports($this->createRequest(['REMOTE_USER' => ''])));
}

public function testSupportTokenStorageWithToken()
Expand Down