Skip to content

Commit

Permalink
minor #46637 [Security] Fix tests (chalasr)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.2 branch.

Discussion
----------

[Security] Fix tests

| Q             | A
| ------------- | ---
| Branch?       | 6.2
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Same as #46624 for FormLoginAuthenticator et al.

Commits
-------

c884399 [Security] Fix tests
  • Loading branch information
nicolas-grekas committed Jun 10, 2022
2 parents ce227a1 + c884399 commit 013857a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,9 @@ public function supports(Request $request): ?bool

public function authenticate(Request $request): Passport
{
return new SelfValidatingPassport(
new UserBadge($request->attributes->get('_pre_authenticated_username'), $this->userProvider->loadUserByIdentifier(...)),
[new PreAuthenticatedUserBadge()]
);
$userBadge = new UserBadge($request->attributes->get('_pre_authenticated_username'), $this->userProvider->loadUserByIdentifier(...));

return new SelfValidatingPassport($userBadge, [new PreAuthenticatedUserBadge()]);
}

public function createToken(Passport $passport, string $firewallName): TokenInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,9 @@ public function authenticate(Request $request): Passport
{
$credentials = $this->getCredentials($request);

$passport = new Passport(
new UserBadge($credentials['username'], $this->userProvider->loadUserByIdentifier(...)),
new PasswordCredentials($credentials['password']),
[new RememberMeBadge()]
);
$userBadge = new UserBadge($credentials['username'], $this->userProvider->loadUserByIdentifier(...));
$passport = new Passport($userBadge, new PasswordCredentials($credentials['password']), [new RememberMeBadge()]);

if ($this->options['enable_csrf']) {
$passport->addBadge(new CsrfTokenBadge($this->options['csrf_token_id'], $credentials['csrf_token']));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ public function authenticate(Request $request): Passport
$username = $request->headers->get('PHP_AUTH_USER');
$password = $request->headers->get('PHP_AUTH_PW', '');

$passport = new Passport(
new UserBadge($username, $this->userProvider->loadUserByIdentifier(...)),
new PasswordCredentials($password)
);
$userBadge = new UserBadge($username, $this->userProvider->loadUserByIdentifier(...));
$passport = new Passport($userBadge, new PasswordCredentials($password));

if ($this->userProvider instanceof PasswordUpgraderInterface) {
$passport->addBadge(new PasswordUpgradeBadge($password, $this->userProvider));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,8 @@ public function authenticate(Request $request): Passport
}

$userBadge = new UserBadge($credentials['username'], $this->userProvider->loadUserByIdentifier(...));
$passport = new Passport($userBadge, new PasswordCredentials($credentials['password']));

$passport = new Passport(
$userBadge,
new PasswordCredentials($credentials['password'])
);
if ($this->userProvider instanceof PasswordUpgraderInterface) {
$passport->addBadge(new PasswordUpgradeBadge($credentials['password'], $this->userProvider));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,23 +54,21 @@ public function supports(Request $request): ?bool

public function authenticate(Request $request): Passport
{
$username = $request->get('user');
if (!$username) {
if (!$username = $request->get('user')) {
throw new InvalidLoginLinkAuthenticationException('Missing user from link.');
}

return new SelfValidatingPassport(
new UserBadge($username, function () use ($request) {
try {
$user = $this->loginLinkHandler->consumeLoginLink($request);
} catch (InvalidLoginLinkExceptionInterface $e) {
throw new InvalidLoginLinkAuthenticationException('Login link could not be validated.', 0, $e);
}
$userBadge = new UserBadge($username, function () use ($request) {
try {
$user = $this->loginLinkHandler->consumeLoginLink($request);
} catch (InvalidLoginLinkExceptionInterface $e) {
throw new InvalidLoginLinkAuthenticationException('Login link could not be validated.', 0, $e);
}

return $user;
}),
[new RememberMeBadge()]
);
return $user;
});

return new SelfValidatingPassport($userBadge, [new RememberMeBadge()]);
}

public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,17 @@ public function supports(Request $request): ?bool

public function authenticate(Request $request): Passport
{
$rawCookie = $request->cookies->get($this->cookieName);
if (!$rawCookie) {
if (!$rawCookie = $request->cookies->get($this->cookieName)) {
throw new \LogicException('No remember-me cookie is found.');
}

$rememberMeCookie = RememberMeDetails::fromRawCookie($rawCookie);

return new SelfValidatingPassport(new UserBadge($rememberMeCookie->getUserIdentifier(), function () use ($rememberMeCookie) {
$userBadge = new UserBadge($rememberMeCookie->getUserIdentifier(), function () use ($rememberMeCookie) {
return $this->rememberMeHandler->consumeRememberMeCookie($rememberMeCookie);
}));
});

return new SelfValidatingPassport($userBadge);
}

public function createToken(Passport $passport, string $firewallName): TokenInterface
Expand Down

0 comments on commit 013857a

Please sign in to comment.