-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[DX] Ability to authentication a User directly #11320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
af1f962
230bbb2
de986c4
ea862a9
3af3221
1ce60d7
33f7b63
3860815
b6d23f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Security\Http\RememberMe; | ||
|
||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface; | ||
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesResolverInterface; | ||
|
||
class RememberMeServicesResolver implements RememberMeServicesResolverInterface | ||
{ | ||
/** | ||
* @var \Symfony\Component\DependencyInjection\ContainerInterface | ||
*/ | ||
private $serviceContainer; | ||
|
||
/** | ||
* @param ContainerInterface $serviceContainer | ||
*/ | ||
public function __construct(ContainerInterface $serviceContainer) | ||
{ | ||
$this->serviceContainer = $serviceContainer; | ||
} | ||
|
||
/** | ||
* @param $providerKey | ||
* @return null|RememberMeServicesInterface | ||
*/ | ||
public function resolve($providerKey) | ||
{ | ||
$rememberMeServices = null; | ||
if ($this->serviceContainer->has('security.authentication.rememberme.services.persistent.'.$providerKey)) { | ||
$rememberMeServices = $this->serviceContainer->get('security.authentication.rememberme.services.persistent.'.$providerKey); | ||
} elseif ($this->serviceContainer->has('security.authentication.rememberme.services.simplehash.'.$providerKey)) { | ||
$rememberMeServices = $this->serviceContainer->get('security.authentication.rememberme.services.simplehash.'.$providerKey); | ||
} | ||
|
||
if ($rememberMeServices instanceof RememberMeServicesInterface) { | ||
return $rememberMeServices; | ||
} | ||
|
||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\LoginManagerBundle\Controller; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\DependencyInjection\ContainerAware; | ||
use Symfony\Component\Security\Core\User\User; | ||
|
||
class LoginController extends ContainerAware | ||
{ | ||
public function loginAction() | ||
{ | ||
$user = new User('norzechowicz', 'password123'); | ||
$this->container->get('security.login_manager')->loginUser('secured_area', $user); | ||
|
||
return new Response(); | ||
} | ||
|
||
public function loginCheckAction() | ||
{ | ||
return new Response('', 400); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\LoginManagerBundle\Controller; | ||
|
||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\DependencyInjection\ContainerAware; | ||
use Symfony\Component\Security\Core\User\User; | ||
|
||
class SecuredController extends ContainerAware | ||
{ | ||
public function indexAction() | ||
{ | ||
return new Response("Secured area"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\LoginManagerBundle; | ||
|
||
use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
||
class LoginManagerBundle extends Bundle | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
login: | ||
path: /login | ||
defaults: { _controller: LoginManagerBundle:Login:login } | ||
|
||
login_check: | ||
path: /login_check | ||
defaults: { _controller: LoginManagerBundle:Login:loginCheck } | ||
|
||
secured_index: | ||
path: /secured/index | ||
defaults: { _controller: LoginManagerBundle:Secured:index } | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\SecurityBundle\Tests\Functional; | ||
|
||
/** | ||
* @group functional | ||
*/ | ||
class LoginManagerTestCase extends WebTestCase | ||
{ | ||
public function testLoginUserInController() | ||
{ | ||
$client = $this->createClient(array('test_case' => 'LoginManager')); | ||
$client->insulate(); | ||
$client->request('GET', '/login'); | ||
$client->request('GET', '/secured/index'); | ||
$this->assertEquals('Secured area', $client->getResponse()->getContent()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
return array( | ||
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), | ||
new Symfony\Bundle\SecurityBundle\SecurityBundle(), | ||
new Symfony\Bundle\TwigBundle\TwigBundle(), | ||
new Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\LoginManagerBundle\LoginManagerBundle(), | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
imports: | ||
- { resource: ./../config/default.yml } | ||
|
||
security: | ||
encoders: | ||
Symfony\Component\Security\Core\User\User: plaintext | ||
|
||
providers: | ||
in_memory: | ||
memory: | ||
users: | ||
norzechowicz: { password: test, roles: [ROLE_USER] } | ||
|
||
firewalls: | ||
secured_area: | ||
pattern: ^/ | ||
form_login: | ||
check_path: /login_check | ||
default_target_path: /secured/index | ||
anonymous: ~ | ||
|
||
access_control: | ||
- { path: ^/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY } | ||
- { path: .*, roles: ROLE_USER } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
_csrf_form_login_bundle: | ||
resource: @LoginManagerBundle/Resources/config/routing.yml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Login; | ||
|
||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | ||
use Symfony\Component\Security\Core\SecurityContextInterface; | ||
use Symfony\Component\Security\Core\User\UserCheckerInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesResolverInterface; | ||
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface; | ||
|
||
class LoginManager implements LoginManagerInterface | ||
{ | ||
/** | ||
* @var SecurityContextInterface | ||
*/ | ||
private $securityContext; | ||
|
||
/** | ||
* @var UserCheckerInterface | ||
*/ | ||
private $userChecker; | ||
|
||
/** | ||
* @var RequestStack | ||
*/ | ||
private $requestStack; | ||
|
||
/** | ||
* @var SessionAuthenticationStrategyInterface | ||
*/ | ||
private $sessionAuthenticationStrategy; | ||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] Empty line missing |
||
* @var RememberMeServicesResolverInterface | ||
*/ | ||
private $rememberMeServicesResolver; | ||
|
||
/** | ||
* @param SecurityContextInterface $securityContext | ||
* @param UserCheckerInterface $userChecker | ||
* @param RequestStack $requestStack | ||
* @param SessionAuthenticationStrategyInterface $sessionAuthenticationStrategy | ||
* @param RememberMeServicesResolverInterface $rememberMeServicesResolver | ||
*/ | ||
public function __construct( | ||
SecurityContextInterface $securityContext, | ||
UserCheckerInterface $userChecker, | ||
RequestStack $requestStack, | ||
SessionAuthenticationStrategyInterface $sessionAuthenticationStrategy, | ||
RememberMeServicesResolverInterface $rememberMeServicesResolver | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you move everything on one line? |
||
) { | ||
$this->securityContext = $securityContext; | ||
$this->userChecker = $userChecker; | ||
$this->requestStack = $requestStack; | ||
$this->sessionAuthenticationStrategy = $sessionAuthenticationStrategy; | ||
$this->rememberMeServicesResolver = $rememberMeServicesResolver; | ||
} | ||
|
||
/** | ||
* @param $firewallName | ||
* @param UserInterface $user | ||
* @param Response $response | ||
*/ | ||
public function loginUser($firewallName, UserInterface $user, Response $response = null) | ||
{ | ||
$this->userChecker->checkPostAuth($user); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't the method |
||
$token = $this->createToken($firewallName, $user); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we check if the token has been authenticated? if (!$token->isAuthenticated()) {
throw new AuthenticationException("Unauthenticated token.");
} |
||
|
||
$request = $this->requestStack->getMasterRequest(); | ||
if (!is_null($request)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The core always uses this instead of if (null !== $request) { There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh sorry, going to fix that now! |
||
$this->sessionAuthenticationStrategy->onAuthentication($request, $token); | ||
|
||
if (!is_null($response)) { | ||
$rememberMeServices = $this->rememberMeServicesResolver->resolve($firewallName); | ||
|
||
if (!is_null($rememberMeServices)) { | ||
$rememberMeServices->loginSuccess($request, $response, $token); | ||
} | ||
} | ||
} | ||
|
||
$this->securityContext->setToken($token); | ||
} | ||
|
||
/** | ||
* @param $firewall | ||
* @param UserInterface $user | ||
* @return UsernamePasswordToken | ||
*/ | ||
protected function createToken($firewall, UserInterface $user) | ||
{ | ||
return new UsernamePasswordToken($user, null, $firewall, $user->getRoles()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Http\Login; | ||
|
||
use Symfony\Component\Security\Core\User\UserInterface; | ||
|
||
interface LoginManagerInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the need for an interface. |
||
{ | ||
public function loginUser($firewallName, UserInterface $user); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remember correctly these services are marked private. We also have (had?) this problem in the FOSUserBundle.