Skip to content

Commit eb7cf73

Browse files
committed
minor #40672 [Security] [Passport] improve dx and document AuthenticationException (jrushlow)
This PR was merged into the 5.2 branch. Discussion ---------- [Security] [Passport] improve dx and document AuthenticationException | Q | A | ------------- | --- | Branch? | 5.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | tbd `Passport::getUser()` (Instance of `UserPassportInterface::class`) throws an `AuthenticationException::class` if a user does not exist. Let's document that for better DX and visibility. Use case: - User login w/ a `username` that does not exist (custom json authenticator) - Attempt Authentication... - Auth failed `LoginFailureEvent` dispatched - snippet below: ```php // Userland\LoginFailureEventSubscriber::class public function dispatchFailure(LoginFailureEvent $event): void { $user = $event->getPassport()->getUser(); $message = new UserlandMessage($user); $this->messageBus->dispatch($message); } ``` - `401` status is returned. The above subscriber fails silently because a `UsernameNotFoundException` was ultimately thrown from `UserBadge::getUser()`. Commits ------- 97ceba0f5d improve dx and document auth exception
2 parents 3626606 + b9b14e0 commit eb7cf73

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

Authenticator/Passport/Badge/UserBadge.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Http\Authenticator\Passport\Badge;
1313

14+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1415
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
1516
use Symfony\Component\Security\Core\User\UserInterface;
1617
use Symfony\Component\Security\Http\EventListener\UserProviderListener;
@@ -55,6 +56,9 @@ public function getUserIdentifier(): string
5556
return $this->userIdentifier;
5657
}
5758

59+
/**
60+
* @throws AuthenticationException when the user cannot be found
61+
*/
5862
public function getUser(): UserInterface
5963
{
6064
if (null === $this->user) {

Authenticator/Passport/Passport.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ public function __construct($userBadge, CredentialsInterface $credentials, array
5555
}
5656
}
5757

58+
/**
59+
* {@inheritdoc}
60+
*/
5861
public function getUser(): UserInterface
5962
{
6063
if (null === $this->user) {

Authenticator/Passport/UserPassportInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Http\Authenticator\Passport;
1313

14+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1415
use Symfony\Component\Security\Core\User\UserInterface;
1516

1617
/**
@@ -22,5 +23,8 @@
2223
*/
2324
interface UserPassportInterface extends PassportInterface
2425
{
26+
/**
27+
* @throws AuthenticationException when the user cannot be found
28+
*/
2529
public function getUser(): UserInterface;
2630
}

0 commit comments

Comments
 (0)