Skip to content

Commit

Permalink
Track session usage when setting the token
Browse files Browse the repository at this point in the history
  • Loading branch information
wouterj committed Apr 3, 2020
1 parent 15edfd3 commit 20b8804
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 3 deletions.
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 @@ -49,6 +49,10 @@ public function setToken(TokenInterface $token = null)
@trigger_error(sprintf('Not implementing the "%s::getRoleNames()" method in "%s" is deprecated since Symfony 4.3.', TokenInterface::class, \get_class($token)), E_USER_DEPRECATED);
}

if ($token) {
$this->getToken();
}

$this->initializer = null;
$this->token = $token;
}
Expand Down
Expand Up @@ -52,6 +52,11 @@ public function getToken(): ?TokenInterface
public function setToken(TokenInterface $token = null): void
{
$this->storage->setToken($token);

if ($token && $this->enableUsageTracking) {
// increments the internal session usage index
$this->sessionLocator->get('session')->getMetadataBag();
}
}

public function enableUsageTracking(): void
Expand Down
Expand Up @@ -411,9 +411,9 @@ protected function runSessionOnKernelResponse($newToken, $original = null)

private function handleEventWithPreviousSession($userProviders, UserInterface $user = null, RememberMeServicesInterface $rememberMeServices = null)
{
$user = $user ?: new User('foo', 'bar');
$tokenUser = $user ?: new User('foo', 'bar');
$session = new Session(new MockArraySessionStorage());
$session->set('_security_context_key', serialize(new UsernamePasswordToken($user, '', 'context_key', ['ROLE_USER'])));
$session->set('_security_context_key', serialize(new UsernamePasswordToken($tokenUser, '', 'context_key', ['ROLE_USER'])));

$request = new Request();
$request->setSession($session);
Expand Down Expand Up @@ -442,6 +442,10 @@ private function handleEventWithPreviousSession($userProviders, UserInterface $u
$listener(new RequestEvent($this->getMockBuilder(HttpKernelInterface::class)->getMock(), $request, HttpKernelInterface::MASTER_REQUEST));

if (null !== $usageIndex) {
if (null !== $user) {
++$usageIndex;
}

$this->assertSame($usageIndex, $session->getUsageIndex());
$tokenStorage->getToken();
$this->assertSame(1 + $usageIndex, $session->getUsageIndex());
Expand Down

0 comments on commit 20b8804

Please sign in to comment.