Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lucasmezencio/3990974 to your computer and use it in GitHub Desktop.
Save lucasmezencio/3990974 to your computer and use it in GitHub Desktop.
Simple Basic Auth Controller for Silex.
<?php
namespace Silex\Provider;
use Silex\Application;
use Silex\SilexEvents;
use Silex\ControllerProviderInterface;
use Silex\ControllerCollection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
class BasicAuthControllerProvider implements ControllerProviderInterface
{
public function connect(Application $app)
{
// init
$app['login.username'] = (isset($app['login.username']))? $app['login.username']: "demo";
$app['login.password'] = (isset($app['login.password']))? $app['login.password']: "123456";
$app['login.redirect'] = (isset($app['login.redirect']))? $app['login.redirect']: "home";
$app['login.basic_login_response'] = function() {
$response = new Response();
$response->headers->set('WWW-Authenticate', sprintf('Basic realm="%s"', 'Basic Login'));
$response->setStatusCode(401, 'Please sign in.');
return $response;
};
// controllers
$controllers = new ControllerCollection();
// login
$controllers->get('/', function (Request $request, Application $app) {
$username = $request->server->get('PHP_AUTH_USER', false);
$password = $request->server->get('PHP_AUTH_PW');
if ($app['login.username'] === $username && $app['login.password'] === $password) {
$app['session']->set('isAuthenticated', true);
return $app->redirect($app['url_generator']->generate($app['login.redirect']));
}
return $app['login.basic_login_response'];
})->bind('login');
// logout
$controllers->get('/logout', function (Request $request, Application $app) {
$app['session']->set('isAuthenticated', false);
return $app['login.basic_login_response'];
})->bind('logout');
// add befre event
$this->addCheckAuthEvent($app);
return $controllers;
}
private function addCheckAuthEvent($app)
{
// check login
$app['dispatcher']->addListener(SilexEvents::BEFORE, function (GetResponseEvent $event) use ($app){
$request = $event->getRequest();
if ($request->getRequestUri() === $app['url_generator']->generate('login')) {
return;
}
$app['session']->get('isAuthenticated');
if (!$app['session']->get('isAuthenticated')) {
$ret = $app->redirect($app['url_generator']->generate('login'));
} else {
$ret = null;
}
if ($ret instanceof Response) {
$event->setResponse($ret);
}
}, 0);
}
}
<?php
require_once __DIR__ . '/silex.phar';
$app = new Silex\Application();
$app['autoloader']->registerNamespace('Silex', __DIR__.'/src');
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app->register(new Silex\Provider\SessionServiceProvider());
// if you want to logout, access to 'auth/logout'
$app->mount('/auth', new Silex\Provider\BasicAuthControllerProvider());
$app->get('/', function () use ($app) {
return $app->escape('This page is sample page');
})->bind('home');
$app->run();
$app['login.username'] = 'admin';
$app['login.password'] = 'password';
$app['login.redirect'] = 'member_top';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment