Skip to content

Commit

Permalink
[Security][SecurityBundle] Allow passing attributes to passport via `…
Browse files Browse the repository at this point in the history
…Security::login()`
  • Loading branch information
alexandre-daubois committed Sep 18, 2024
1 parent 7776b9c commit 3e3b964
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
11 changes: 9 additions & 2 deletions Authentication/AuthenticatorManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,19 @@ public function __construct(
}

/**
* @param BadgeInterface[] $badges Optionally, pass some Passport badges to use for the manual login
* @param BadgeInterface[] $badges Optionally, pass some Passport badges to use for the manual login
* @param array<string, mixed> $attributes Optionally, pass some Passport attributes to use for the manual login
*/
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = []): ?Response
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = [] /* , array $attributes = [] */): ?Response
{
$attributes = 4 < \func_num_args() ? func_get_arg(4) : [];

// create an authentication token for the User
$passport = new SelfValidatingPassport(new UserBadge($user->getUserIdentifier(), fn () => $user), $badges);
foreach ($attributes as $k => $v) {
$passport->setAttribute($k, $v);
}

$token = $authenticator->createToken($passport, $this->firewallName);

// announce the authentication token
Expand Down
5 changes: 3 additions & 2 deletions Authentication/UserAuthenticatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ interface UserAuthenticatorInterface
* Convenience method to programmatically login a user and return a
* Response *if any* for success.
*
* @param BadgeInterface[] $badges Optionally, pass some Passport badges to use for the manual login
* @param BadgeInterface[] $badges Optionally, pass some Passport badges to use for the manual login
* @param array<string, mixed> $attributes Optionally, pass some Passport attributes to use for the manual login
*/
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = []): ?Response;
public function authenticateUser(UserInterface $user, AuthenticatorInterface $authenticator, Request $request, array $badges = [] /* , array $attributes = [] */): ?Response;
}
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CHANGELOG
* Pass the current token to the `checkPostAuth()` method of user checkers
* Deprecate argument `$secret` of `RememberMeAuthenticator`
* Deprecate passing an empty string as `$userIdentifier` argument to `UserBadge` constructor
* Allow passing passport attributes to the `UserAuthenticatorInterface::authenticateUser()` method

7.1
---
Expand Down
15 changes: 13 additions & 2 deletions Tests/Authentication/AuthenticatorManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,24 @@ public function testAuthenticateRequestCanModifyTokenFromEvent()
public function testAuthenticateUser()
{
$authenticator = $this->createAuthenticator();
$authenticator->expects($this->any())->method('createToken')->willReturn($this->token);
$authenticator->expects($this->any())->method('onAuthenticationSuccess')->willReturn($this->response);

$badge = new UserBadge('alex');

$authenticator
->expects($this->any())
->method('createToken')
->willReturnCallback(function (Passport $passport) use ($badge) {
$this->assertSame(['attr' => 'foo', 'attr2' => 'bar'], $passport->getAttributes());
$this->assertSame([UserBadge::class => $badge], $passport->getBadges());

return $this->token;
});

$this->tokenStorage->expects($this->once())->method('setToken')->with($this->token);

$manager = $this->createManager([$authenticator]);
$manager->authenticateUser($this->user, $authenticator, $this->request);
$manager->authenticateUser($this->user, $authenticator, $this->request, [$badge], ['attr' => 'foo', 'attr2' => 'bar']);
}

public function testAuthenticateUserCanModifyTokenFromEvent()
Expand Down

0 comments on commit 3e3b964

Please sign in to comment.