Skip to content

Instantly share code, notes, and snippets.

@hallboav
Created July 23, 2017 05:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hallboav/162920720076f871f04622ce78fe6ec9 to your computer and use it in GitHub Desktop.
Save hallboav/162920720076f871f04622ce78fe6ec9 to your computer and use it in GitHub Desktop.
The user must authenticate to two consecutive firewalls to reach the resource.
<?php
// composer require silex/silex symfony/security && php -S localhost:8888 -t $(pwd)
require 'vendor/autoload.php';
use Silex\Application;
use Silex\Provider\SessionServiceProvider;
use Silex\Provider\SecurityServiceProvider;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
$app = new Application();
$app->register(new SessionServiceProvider());
$app->register(new SecurityServiceProvider(), [
'security.firewalls' => [
'fw1' => [
'pattern' => '/fw0/fw1',
'users' => [
'fw1' => [
['ROLE_USER'],
'$2y$13$l3VH4WG.qslkCBw5pB1scOxKXzBdaGwxr4w68B2TwCGIf.bSDNfo6' // fw1
]
],
'form' => [
'default_target_path' => '/fw0/fw1',
'login_path' => '/fw0/login_fw1',
'check_path' => '/fw0/fw1/login_check',
],
'logout' => [
'logout_path' => '/fw0/fw1/logout_fw1',
'target_url' => '/fw0/login_fw1',
'invalidate_session' => false
]
],
'fw0' => [
'pattern' => '/fw0',
'users' => [
'fw0' => [
['ROLE_USER'],
'$2y$13$j9tSz8zI9KN95GHErFfOC.1cb8.wQ0hixzAPnlMe.UvDimACtEKOq' // fw0
]
],
'form' => [
'default_target_path' => '/fw0',
'login_path' => '/login_fw0',
'check_path' => '/fw0/login_check',
],
'logout' => [
'logout_path' => '/fw0/logout_fw0',
'target_url' => '/login_fw0'
]
]
]
]);
$app->get('/', function () use ($app) {
return new RedirectResponse('/login_fw0');
});
$app->get('/login_fw0', function () use ($app) {
return new Response(<<<EOT
<h4>fw0_login</h4>
<form method="post" action="/fw0/login_check">
<label for="username">Username</label><br><input id="username" name="_username" value="fw0"><br>
<label for="password">Password</label><br><input id="password" name="_password" value="fw0"><br>
<button type="submit">Submit</button>
</form>
EOT
);
});
$fw1 = function () use ($app) {
$user = $app['user'];
return new Response(<<<EOT
Welcome {$user->getUsername()}. <a href="/fw0/logout_fw0">Logout</a>.
<h4>fw1_login</h4>
<form method="post" action="/fw0/fw1/login_check">
<label for="username">Username</label><br><input id="username" name="_username" value="fw1"><br>
<label for="password">Password</label><br><input id="password" name="_password" value="fw1"><br>
<button type="submit">Submit</button>
</form>
EOT
);
};
$app->get('/fw0', $fw1);
$app->get('/fw0/login_fw1', $fw1);
$app->get('/fw0/fw1', function () use ($app) {
$user = $app['user'];
return new Response(<<<EOT
Welcome {$user->getUsername()}. <a href="/fw0/fw1/logout_fw1">Logout</a>.
<h2>Congratulations!</h2>
EOT
);
});
$app->run();
@spelcaster
Copy link

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment