Skip to content

Commit

Permalink
Force incrementing session usage index upon programatic login
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Apr 3, 2020
1 parent 15edfd3 commit c1e0d9e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 1 deletion.
Expand Up @@ -23,7 +23,7 @@ class AppCustomAuthenticator extends AbstractGuardAuthenticator
{
public function supports(Request $request)
{
return true;
return '/manual_login' !== $request->getPathInfo() && '/profile' !== $request->getPathInfo();
}

public function getCredentials(Request $request)
Expand Down
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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\GuardedBundle;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;

class AuthenticationController
{
public function manualLoginAction(GuardAuthenticatorHandler $guardAuthenticatorHandler, Request $request)
{
$guardAuthenticatorHandler->authenticateWithToken(new PostAuthenticationGuardToken(new User('Jane', 'test', ['ROLE_USER']), 'secure', ['ROLE_USER']), $request, 'secure');

return new Response('Logged in.');
}

public function profileAction(UserInterface $user = null)
{
if (null === $user) {
return new Response('Not logged in.');
}

return new Response('Username: '.$user->getUsername());
}
}
10 changes: 10 additions & 0 deletions src/Symfony/Bundle/SecurityBundle/Tests/Functional/GuardedTest.php
Expand Up @@ -21,4 +21,14 @@ public function testGuarded()

$this->assertSame(418, $client->getResponse()->getStatusCode());
}

public function testManualLogin()
{
$client = $this->createClient(['debug' => true, 'test_case' => 'Guarded', 'root_config' => 'config.yml']);

$client->request('GET', '/manual_login');
$client->request('GET', '/profile');

$this->assertSame('Username: Jane', $client->getResponse()->getContent());
}
}
Expand Up @@ -10,8 +10,19 @@ framework:
services:
logger: { class: Psr\Log\NullLogger }
Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\GuardedBundle\AppCustomAuthenticator: ~
Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\GuardedBundle\AuthenticationController:
tags: [controller.service_arguments]

security:
encoders:
Symfony\Component\Security\Core\User\User: plaintext

providers:
in_memory:
memory:
users:
Jane: { password: test, roles: [ROLE_USER] }

firewalls:
secure:
pattern: ^/
Expand Down
Expand Up @@ -3,3 +3,12 @@ main:
defaults:
_controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction
path: /app
profile:
path: /profile
defaults:
_controller: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\GuardedBundle\AuthenticationController::profileAction

manual_login:
path: /manual_login
defaults:
_controller: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\GuardedBundle\AuthenticationController::manualLoginAction
Expand Up @@ -62,6 +62,8 @@ public function __construct(TokenStorageInterface $tokenStorage, EventDispatcher
public function authenticateWithToken(TokenInterface $token, Request $request, string $providerKey = null)
{
$this->migrateSession($request, $token, $providerKey);
// force incrementing the internal session usage index
$this->tokenStorage->getToken();
$this->tokenStorage->setToken($token);

if (null !== $this->dispatcher) {
Expand Down

0 comments on commit c1e0d9e

Please sign in to comment.