Skip to content

Commit b20f1d9

Browse files
committed
Track session usage when setting the token
1 parent 1c317cd commit b20f1d9

File tree

5 files changed

+69
-1
lines changed

5 files changed

+69
-1
lines changed

Tests/Functional/Bundle/GuardedBundle/AppCustomAuthenticator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AppCustomAuthenticator extends AbstractGuardAuthenticator
2323
{
2424
public function supports(Request $request)
2525
{
26-
return true;
26+
return '/manual_login' !== $request->getPathInfo() && '/profile' !== $request->getPathInfo();
2727
}
2828

2929
public function getCredentials(Request $request)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Bundle\SecurityBundle\Tests\Functional\Bundle\GuardedBundle;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\Response;
16+
use Symfony\Component\Security\Core\User\User;
17+
use Symfony\Component\Security\Core\User\UserInterface;
18+
use Symfony\Component\Security\Guard\GuardAuthenticatorHandler;
19+
use Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken;
20+
21+
class AuthenticationController
22+
{
23+
public function manualLoginAction(GuardAuthenticatorHandler $guardAuthenticatorHandler, Request $request)
24+
{
25+
$guardAuthenticatorHandler->authenticateWithToken(new PostAuthenticationGuardToken(new User('Jane', 'test', ['ROLE_USER']), 'secure', ['ROLE_USER']), $request, 'secure');
26+
27+
return new Response('Logged in.');
28+
}
29+
30+
public function profileAction(UserInterface $user = null)
31+
{
32+
if (null === $user) {
33+
return new Response('Not logged in.');
34+
}
35+
36+
return new Response('Username: '.$user->getUsername());
37+
}
38+
}

Tests/Functional/GuardedTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,14 @@ public function testGuarded()
2121

2222
$this->assertSame(418, $client->getResponse()->getStatusCode());
2323
}
24+
25+
public function testManualLogin()
26+
{
27+
$client = $this->createClient(['debug' => true, 'test_case' => 'Guarded', 'root_config' => 'config.yml']);
28+
29+
$client->request('GET', '/manual_login');
30+
$client->request('GET', '/profile');
31+
32+
$this->assertSame('Username: Jane', $client->getResponse()->getContent());
33+
}
2434
}

Tests/Functional/app/Guarded/config.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,19 @@ framework:
1010
services:
1111
logger: { class: Psr\Log\NullLogger }
1212
Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\GuardedBundle\AppCustomAuthenticator: ~
13+
Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\GuardedBundle\AuthenticationController:
14+
tags: [controller.service_arguments]
1315

1416
security:
17+
encoders:
18+
Symfony\Component\Security\Core\User\User: plaintext
19+
20+
providers:
21+
in_memory:
22+
memory:
23+
users:
24+
Jane: { password: test, roles: [ROLE_USER] }
25+
1526
firewalls:
1627
secure:
1728
pattern: ^/

Tests/Functional/app/Guarded/routing.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ main:
33
defaults:
44
_controller: Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction
55
path: /app
6+
profile:
7+
path: /profile
8+
defaults:
9+
_controller: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\GuardedBundle\AuthenticationController::profileAction
10+
11+
manual_login:
12+
path: /manual_login
13+
defaults:
14+
_controller: Symfony\Bundle\SecurityBundle\Tests\Functional\Bundle\GuardedBundle\AuthenticationController::manualLoginAction

0 commit comments

Comments
 (0)