Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b14a5e8

Browse filesBrowse files
committed
Moved new authenticator to the HTTP namespace
This removes the introduced dependency on Guard from core. It also allows an easier migration path, as the complete Guard subcomponent can now be deprecated later in the 5.x life.
1 parent b923e4c commit b14a5e8
Copy full SHA for b14a5e8

23 files changed

+273
-209
lines changed

‎src/Symfony/Bundle/SecurityBundle/Resources/config/authenticators.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Resources/config/authenticators.xml
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
</service>
2121

2222
<service id="security.authenticator.http_basic"
23-
class="Symfony\Component\Security\Core\Authentication\Authenticator\HttpBasicAuthenticator"
23+
class="Symfony\Component\Security\Http\Authentication\Authenticator\HttpBasicAuthenticator"
2424
abstract="true">
2525
<argument type="abstract">realm name</argument>
2626
<argument type="abstract">user provider</argument>
@@ -29,7 +29,7 @@
2929
</service>
3030

3131
<service id="security.authenticator.form_login"
32-
class="Symfony\Component\Security\Core\Authentication\Authenticator\FormLoginAuthenticator"
32+
class="Symfony\Component\Security\Http\Authentication\Authenticator\FormLoginAuthenticator"
3333
abstract="true">
3434
<argument type="service" id="security.http_utils" />
3535
<argument /> <!-- csrf token generator -->
@@ -39,7 +39,7 @@
3939
</service>
4040

4141
<service id="security.authenticator.anonymous"
42-
class="Symfony\Component\Security\Core\Authentication\Authenticator\AnonymousAuthenticator"
42+
class="Symfony\Component\Security\Http\Authentication\Authenticator\AnonymousAuthenticator"
4343
abstract="true">
4444
<argument /> <!-- secret -->
4545
<argument type="service" id="security.token_storage" />

‎src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/SecurityBundle/Resources/config/security.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
<argument type="service" id="event_dispatcher" />
5353
</call>
5454
</service>
55-
<service id="security.authentication.manager.guard" class="Symfony\Component\Security\Core\Authentication\GuardAuthenticationManager">
55+
<service id="security.authentication.manager.guard" class="Symfony\Component\Security\Http\Authentication\GuardAuthenticationManager">
5656
<argument /> <!-- guard authenticators -->
5757
<argument type="service" id="Symfony\Component\Security\Core\User\UserCheckerInterface" /> <!-- User Checker -->
5858
<argument>%security.authentication.manager.erase_credentials%</argument>
+71Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Authentication\Token;
13+
14+
/**
15+
* The token used by the guard auth system before authentication.
16+
*
17+
* The GuardAuthenticationListener creates this, which is then consumed
18+
* immediately by the GuardAuthenticationProvider. If authentication is
19+
* successful, a different authenticated token is returned
20+
*
21+
* @author Ryan Weaver <ryan@knpuniversity.com>
22+
*/
23+
class PreAuthenticationGuardToken extends AbstractToken
24+
{
25+
private $credentials;
26+
private $guardProviderKey;
27+
private $providerKey;
28+
29+
/**
30+
* @param mixed $credentials
31+
* @param string $guardProviderKey Unique key that bind this token to a specific AuthenticatorInterface
32+
* @param string|null $providerKey The general provider key (when using with HTTP, this is the firewall name)
33+
*/
34+
public function __construct($credentials, string $guardProviderKey, ?string $providerKey = null)
35+
{
36+
$this->credentials = $credentials;
37+
$this->guardProviderKey = $guardProviderKey;
38+
$this->providerKey = $providerKey;
39+
40+
parent::__construct([]);
41+
42+
// never authenticated
43+
parent::setAuthenticated(false);
44+
}
45+
46+
public function getProviderKey(): ?string
47+
{
48+
return $this->providerKey;
49+
}
50+
51+
public function getGuardProviderKey()
52+
{
53+
return $this->guardProviderKey;
54+
}
55+
56+
/**
57+
* Returns the user credentials, which might be an array of anything you
58+
* wanted to put in there (e.g. username, password, favoriteColor).
59+
*
60+
* @return mixed The user credentials
61+
*/
62+
public function getCredentials()
63+
{
64+
return $this->credentials;
65+
}
66+
67+
public function setAuthenticated(bool $authenticated)
68+
{
69+
throw new \LogicException('The PreAuthenticationGuardToken is *never* authenticated.');
70+
}
71+
}

‎src/Symfony/Component/Security/Core/Tests/Authentication/Authenticator/HttpBasicAuthenticatorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Core/Tests/Authentication/Authenticator/HttpBasicAuthenticatorTest.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use PHPUnit\Framework\MockObject\MockObject;
66
use PHPUnit\Framework\TestCase;
77
use Symfony\Component\HttpFoundation\Request;
8-
use Symfony\Component\Security\Core\Authentication\Authenticator\HttpBasicAuthenticator;
8+
use Symfony\Component\Security\Http\Authentication\Authenticator\HttpBasicAuthenticator;
99
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
1010
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
1111
use Symfony\Component\Security\Core\Exception\BadCredentialsException;

‎src/Symfony/Component/Security/Core/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Core/composer.json
-1Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"symfony/event-dispatcher-contracts": "^1.1|^2",
2121
"symfony/polyfill-php80": "^1.15",
2222
"symfony/service-contracts": "^1.1.6|^2",
23-
"symfony/security-guard": "^4.4",
2423
"symfony/deprecation-contracts": "^2.1"
2524
},
2625
"require-dev": {

‎src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticationListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Guard/Firewall/GuardAuthenticationListener.php
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpKernel\Event\RequestEvent;
1717
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
18+
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticationGuardToken;
1819
use Symfony\Component\Security\Guard\AuthenticatorInterface;
1920
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
21+
use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken as GuardPreAuthenticationGuardToken;
2022
use Symfony\Component\Security\Http\Firewall\AbstractListener;
23+
use Symfony\Component\Security\Http\Firewall\GuardManagerListenerTrait;
2124
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
2225

2326
/**
@@ -30,7 +33,7 @@
3033
*/
3134
class GuardAuthenticationListener extends AbstractListener
3235
{
33-
use GuardAuthenticatorListenerTrait;
36+
use GuardManagerListenerTrait;
3437

3538
private $guardHandler;
3639
private $authenticationManager;
@@ -101,6 +104,11 @@ public function setRememberMeServices(RememberMeServicesInterface $rememberMeSer
101104
$this->rememberMeServices = $rememberMeServices;
102105
}
103106

107+
protected function createPreAuthenticatedToken($credentials, string $uniqueGuardKey, string $providerKey): PreAuthenticationGuardToken
108+
{
109+
return new GuardPreAuthenticationGuardToken($credentials, $uniqueGuardKey, $providerKey);
110+
}
111+
104112
protected function getGuardKey(string $key): string
105113
{
106114
// get a key that's unique to *this* guard authenticator

‎src/Symfony/Component/Security/Guard/GuardAuthenticatorHandler.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Guard/GuardAuthenticatorHandler.php
+2-122Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Guard;
1313

14-
use Symfony\Component\HttpFoundation\Request;
15-
use Symfony\Component\HttpFoundation\Response;
16-
use Symfony\Component\Security\Core\Authentication\Authenticator\AuthenticatorInterface as CoreAuthenticatorInterface;
17-
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18-
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19-
use Symfony\Component\Security\Core\Exception\AuthenticationException;
20-
use Symfony\Component\Security\Core\User\UserInterface;
21-
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
22-
use Symfony\Component\Security\Http\SecurityEvents;
23-
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
24-
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
14+
use Symfony\Component\Security\Http\Authentication\GuardAuthenticatorHandler as CoreAuthenticatorHandlerAlias;
2515

2616
/**
2717
* A utility class that does much of the *work* during the guard authentication process.
@@ -33,116 +23,6 @@
3323
*
3424
* @final
3525
*/
36-
class GuardAuthenticatorHandler
26+
class GuardAuthenticatorHandler extends CoreAuthenticatorHandlerAlias
3727
{
38-
private $tokenStorage;
39-
private $dispatcher;
40-
private $sessionStrategy;
41-
private $statelessProviderKeys;
42-
43-
/**
44-
* @param array $statelessProviderKeys An array of provider/firewall keys that are "stateless" and so do not need the session migrated on success
45-
*/
46-
public function __construct(TokenStorageInterface $tokenStorage, EventDispatcherInterface $eventDispatcher = null, array $statelessProviderKeys = [])
47-
{
48-
$this->tokenStorage = $tokenStorage;
49-
$this->dispatcher = $eventDispatcher;
50-
$this->statelessProviderKeys = $statelessProviderKeys;
51-
}
52-
53-
/**
54-
* Authenticates the given token in the system.
55-
*/
56-
public function authenticateWithToken(TokenInterface $token, Request $request, string $providerKey = null)
57-
{
58-
$this->migrateSession($request, $token, $providerKey);
59-
$this->tokenStorage->setToken($token);
60-
61-
if (null !== $this->dispatcher) {
62-
$loginEvent = new InteractiveLoginEvent($request, $token);
63-
$this->dispatcher->dispatch($loginEvent, SecurityEvents::INTERACTIVE_LOGIN);
64-
}
65-
}
66-
67-
/**
68-
* Returns the "on success" response for the given GuardAuthenticator.
69-
*
70-
* @param CoreAuthenticatorInterface|AuthenticatorInterface $guardAuthenticator
71-
*/
72-
public function handleAuthenticationSuccess(TokenInterface $token, Request $request, $guardAuthenticator, string $providerKey): ?Response
73-
{
74-
if (!$guardAuthenticator instanceof AuthenticatorInterface && !$guardAuthenticator instanceof CoreAuthenticatorInterface) {
75-
throw new \UnexpectedValueException('Invalid guard authenticator passed to '.__METHOD__.'. Expected AuthenticatorInterface of either Security Core or Security Guard.');
76-
}
77-
78-
$response = $guardAuthenticator->onAuthenticationSuccess($request, $token, $providerKey);
79-
80-
// check that it's a Response or null
81-
if ($response instanceof Response || null === $response) {
82-
return $response;
83-
}
84-
85-
throw new \UnexpectedValueException(sprintf('The "%s::onAuthenticationSuccess()" method must return null or a Response object. You returned "%s".', \get_class($guardAuthenticator), get_debug_type($response)));
86-
}
87-
88-
/**
89-
* Convenience method for authenticating the user and returning the
90-
* Response *if any* for success.
91-
*
92-
* @param CoreAuthenticatorInterface|AuthenticatorInterface $authenticator
93-
*/
94-
public function authenticateUserAndHandleSuccess(UserInterface $user, Request $request, $authenticator, string $providerKey): ?Response
95-
{
96-
if (!$authenticator instanceof AuthenticatorInterface && !$authenticator instanceof CoreAuthenticatorInterface) {
97-
throw new \UnexpectedValueException('Invalid guard authenticator passed to '.__METHOD__.'. Expected AuthenticatorInterface of either Security Core or Security Guard.');
98-
}
99-
100-
// create an authenticated token for the User
101-
$token = $authenticator->createAuthenticatedToken($user, $providerKey);
102-
// authenticate this in the system
103-
$this->authenticateWithToken($token, $request, $providerKey);
104-
105-
// return the success metric
106-
return $this->handleAuthenticationSuccess($token, $request, $authenticator, $providerKey);
107-
}
108-
109-
/**
110-
* Handles an authentication failure and returns the Response for the
111-
* GuardAuthenticator.
112-
*
113-
* @param CoreAuthenticatorInterface|AuthenticatorInterface $guardAuthenticator
114-
*/
115-
public function handleAuthenticationFailure(AuthenticationException $authenticationException, Request $request, $guardAuthenticator, string $providerKey): ?Response
116-
{
117-
if (!$guardAuthenticator instanceof AuthenticatorInterface && !$guardAuthenticator instanceof CoreAuthenticatorInterface) {
118-
throw new \UnexpectedValueException('Invalid guard authenticator passed to '.__METHOD__.'. Expected AuthenticatorInterface of either Security Core or Security Guard.');
119-
}
120-
121-
$response = $guardAuthenticator->onAuthenticationFailure($request, $authenticationException);
122-
if ($response instanceof Response || null === $response) {
123-
// returning null is ok, it means they want the request to continue
124-
return $response;
125-
}
126-
127-
throw new \UnexpectedValueException(sprintf('The "%s::onAuthenticationFailure()" method must return null or a Response object. You returned "%s".', \get_class($guardAuthenticator), get_debug_type($response)));
128-
}
129-
130-
/**
131-
* Call this method if your authentication token is stored to a session.
132-
*
133-
* @final
134-
*/
135-
public function setSessionAuthenticationStrategy(SessionAuthenticationStrategyInterface $sessionStrategy)
136-
{
137-
$this->sessionStrategy = $sessionStrategy;
138-
}
139-
140-
private function migrateSession(Request $request, TokenInterface $token, ?string $providerKey)
141-
{
142-
if (\in_array($providerKey, $this->statelessProviderKeys, true) || !$this->sessionStrategy || !$request->hasSession() || !$request->hasPreviousSession()) {
143-
return;
144-
}
145-
146-
$this->sessionStrategy->onAuthentication($request, $token);
147-
}
14828
}

‎src/Symfony/Component/Security/Guard/Provider/GuardAuthenticationProvider.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Guard/Provider/GuardAuthenticationProvider.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Guard\Provider;
1313

14+
use Symfony\Component\Security\Http\Authentication\GuardAuthenticationManagerTrait;
1415
use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
1516
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1617
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
@@ -30,7 +31,7 @@
3031
*/
3132
class GuardAuthenticationProvider implements AuthenticationProviderInterface
3233
{
33-
use GuardAuthenticationProviderTrait;
34+
use GuardAuthenticationManagerTrait;
3435

3536
/**
3637
* @var AuthenticatorInterface[]

‎src/Symfony/Component/Security/Guard/Token/PreAuthenticationGuardToken.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Guard/Token/PreAuthenticationGuardToken.php
+2-48Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Guard\Token;
1313

14-
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
14+
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticationGuardToken as CorePreAuthenticationGuardToken;
1515

1616
/**
1717
* The token used by the guard auth system before authentication.
@@ -22,52 +22,6 @@
2222
*
2323
* @author Ryan Weaver <ryan@knpuniversity.com>
2424
*/
25-
class PreAuthenticationGuardToken extends AbstractToken implements GuardTokenInterface
25+
class PreAuthenticationGuardToken extends CorePreAuthenticationGuardToken implements GuardTokenInterface
2626
{
27-
private $credentials;
28-
private $guardProviderKey;
29-
private $providerKey;
30-
31-
/**
32-
* @param mixed $credentials
33-
* @param string $guardProviderKey Unique key that bind this token to a specific AuthenticatorInterface
34-
* @param string|null $providerKey The general provider key (when using with HTTP, this is the firewall name)
35-
*/
36-
public function __construct($credentials, string $guardProviderKey, ?string $providerKey = null)
37-
{
38-
$this->credentials = $credentials;
39-
$this->guardProviderKey = $guardProviderKey;
40-
$this->providerKey = $providerKey;
41-
42-
parent::__construct([]);
43-
44-
// never authenticated
45-
parent::setAuthenticated(false);
46-
}
47-
48-
public function getProviderKey(): ?string
49-
{
50-
return $this->providerKey;
51-
}
52-
53-
public function getGuardProviderKey()
54-
{
55-
return $this->guardProviderKey;
56-
}
57-
58-
/**
59-
* Returns the user credentials, which might be an array of anything you
60-
* wanted to put in there (e.g. username, password, favoriteColor).
61-
*
62-
* @return mixed The user credentials
63-
*/
64-
public function getCredentials()
65-
{
66-
return $this->credentials;
67-
}
68-
69-
public function setAuthenticated(bool $authenticated)
70-
{
71-
throw new \LogicException('The PreAuthenticationGuardToken is *never* authenticated.');
72-
}
7327
}

‎src/Symfony/Component/Security/Guard/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Guard/composer.json
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.2.5",
20-
"symfony/security-core": "^5.0",
20+
"symfony/security-core": "^5.1",
2121
"symfony/security-http": "^4.4.1|^5.0.1",
2222
"symfony/polyfill-php80": "^1.15"
2323
},

‎src/Symfony/Component/Security/Core/Authentication/Authenticator/AbstractAuthenticator.php renamed to ‎src/Symfony/Component/Security/Http/Authentication/Authenticator/AbstractAuthenticator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Security/Http/Authentication/Authenticator/AbstractAuthenticator.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Security\Core\Authentication\Authenticator;
12+
namespace Symfony\Component\Security\Http\Authentication\Authenticator;
1313

1414
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1515
use Symfony\Component\Security\Core\User\UserInterface;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.