8000 [DX] Ability to authentication a User directly by norberttech · Pull Request #11320 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
8000 File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Checks token authentication.
Checks user preauth.
Improve functional test.
  • Loading branch information
ajgarlag committed Dec 19, 2014
commit 38608152432778108df9833e8f0bca6c78c733dd
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@

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');
$user = $this->container->get('security.user.provider.concrete.in_memory')->loadUserByUsername('norzechowicz');
$this->container->get('security.login_manager')->loginUser('secured_area', $user);

return new Response();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ class LoginManagerTestCase extends WebTestCase
public function testLoginUserInController()
{
$client = $this->createClient(array('test_case' => 'LoginManager'));
$client->insulate();

// Avoid to follow redirects. If we follow this redirect, the user
// will be logged in automatically
$client->setMaxRedirects(-1);
$client->request('GET', '/secured/index');
$this->assertRedirect($client->getResponse(), '/login');

// Access to '/login' route to login the user automatically
$client->request('GET', '/login');
$client->request('GET', '/secured/index');
$this->assertEquals('Secured area', $client->getResponse()->getContent());
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Security/Http/Login/LoginManager.php
< 8000 td id="diff-06ccf69bb79f82e6aed54fddbb3d2287e49dd9e7b8ca8e0d4218488121c771feR79" data-line-number="79" class="blob-num blob-num-addition js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesResolverInterface;
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class LoginManager
{
Expand Down Expand Up @@ -70,9 +71,14 @@ public function __construct(SecurityContextInterface $securityContext, UserCheck
*/
public function loginUser($firewallName, UserInterface $user, Response $response = null)
{
$this->userChecker->checkPreAuth($user);
$this->userChecker->checkPostAuth($user);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the method checkPreAuth be called previously?

$token = $this->createToken($firewallName, $user);
Copy link
Contributor

Choose a reason for hiding this comment

The 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.");
}


if (!$token->isAuthenticated()) {
throw new AuthenticationException("Unauthenticated token");
}

$request = $this->requestStack->getMasterRequest();
if (null !== $request) {
$this->sessionAuthenticationStrategy->onAuthentication($request, $token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function setUp()
public function testLoginWithoutRequest()
{
$loginManager = $this->createLoginManager();
$user = new User('norzechowicz', 'password123');
$user = new User('norzechowicz', 'password123', array('ROLE_USER'));

$this->userChecker->expects($this->once())
->method('checkPostAuth')
Expand All @@ -83,7 +83,7 @@ public function testLoginWithoutRequest()
public function testLoginWithRequest()
{
$loginManager = $this->createLoginManager();
$user = new User('norzechowicz', 'password123');
$user = new User('norzechowicz', 'password123', array('ROLE_USER'));

$this->userChecker->expects($this->once())
->method('checkPostAuth')
Expand All @@ -110,7 +110,7 @@ public function testLoginWithRequest()
public function testLoginWithRequestResponseAndRememberMeServices()
{
$loginManager = $this->createLoginManager();
$user = new User('norzechowicz', 'password123');
$user = new User('norzechowicz', 'password123', array('ROLE_USER'));

$this->userChecker->expects($this->once())
->method('checkPostAuth')
Expand Down Expand Up @@ -144,6 +144,17 @@ public function testLoginWithRequestResponseAndRememberMeServices()
$loginManager->loginUser(self::FIREWALL_NAME, $user, Response::create());
}

/**
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationException
*/
public function testLoginShouldFailWithoutAuthenticatedToken()
{
$loginManager = $this->createLoginManager();
$user = new User('norzechowicz', 'password123');

$loginManager->loginUser(self::FIREWALL_NAME, $user);
}

/**
* @return LoginManager
*/
Expand Down
0