21

I built a Silex project with an login mechanism.

Not being a Symfony expert, I strictly followed the guidelines here for the authentication process : http://silex.sensiolabs.org/doc/providers/security.html

... and it works fine on my development environment

However, when I pushed my project on my production server, I get the following error each time I try to log into my web app

[2012-12-18 16:35:33] CRITICAL: Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException:
A Token was not found in the SecurityContext. (uncaught exception) at
/my/app/path/vendor/symfony/security/Symfony/Component/Security/Http/Firewall/AccessListener.php line 53 [] []

which means that the following code in AccessListener.php

$this->context->getToken());

throws an expection

Given the fact that the same code works perfectly fine on my development environment, I assume it has something to do with my production server configuration.

I found this thread http://groups.google.com/forum/#!msg/symfony-devs/jKphNy_0Q2Y/vYfkAuyjSHEJ that suggests to add the following line to my project's .htaccess

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

with no result. I still get the "A Token was not found in the SecurityContext" exception.

Does anybody have an idea ?

Edit The content of $app['security.firewalls'] is the following

$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'login' => array(
  'pattern' => '^/login$'
),
'admin' => array(
  'pattern' => '^/',
  'form'    => array('login_path' => '/login', 'check_path' => '/admin/login_check'),
  'logout'  => array('logout_path' => '/admin/logout'), // url to call for logging out
  'users' => array(
  'admin' => array('ROLE_ADMIN', 'SOMEPASSWORD'),
  ),
)
)
));
benoit
  • 891
  • 12
  • 22
  • can you post settings of `$app['security.firewalls']` array? – Mun Mun Das Dec 19 '12 at 14:16
  • @m2mdas : I've included it above – benoit Dec 20 '12 at 12:10
  • 1
    Try adding `'anonymous' => true,` in the `login` firewall array. – Mun Mun Das Dec 20 '12 at 12:39
  • What webserver are your running? – mosch Dec 20 '12 at 22:15
  • It could be that the security context of symfony2 could be written in a SQLite database (don't know about silex, but could be similar). Anyway, make 100% sure that all file permissions on your prod server are set correctly. – ducin Dec 22 '12 at 22:51
  • @m2mdas : I've added the setting, with no changes – benoit Dec 28 '12 at 11:19
  • @tkoomzaaskz : thanks for your reply but what kind of file permissions should I check ? – benoit Dec 28 '12 at 11:20
  • Please post the complete .htaccess in your question – Michel Feldheim Jan 13 '13 at 10:13
  • Do sessions work for you? The error may be coming from not being able to save session data (though you should get an error saying this). Also: can you provide a minimal silex project as a github gist that replicates your problem? I have both apache and nginx available to experiment. – Maerlyn Jan 13 '13 at 10:26
  • 1
    @benoit Can you please provide the data that [Michel Feldheim](http://stackoverflow.com/users/1032504/michel-feldheim) asked for in [his answer](http://stackoverflow.com/a/14302533/1339429). We are just trying to help :) – JeanValjean Jan 14 '13 at 09:04

2 Answers2

1

It seems it has nothing to do with HTTP Basic Auth, because you don't use it in any of your firewalls. What you use is a firewall with a form entry point, which then uses session to store the security token.

I would suggest you to look at how sessions (and cookies) are managed on prod server compared to your dev environment.

Florian Klein
  • 8,692
  • 1
  • 32
  • 42
0

Most likely the reason why this works on your local machine but not in your productive environment is that .htaccess is supported by Apache, while nginx does not bother wasting I/O and CPU time parsing this files

If you post your .htaccess I will show you how to translate this to performant nginx-readable configuration.

EDIT

Silex even has a configuration example for nginx

http://silex.sensiolabs.org/doc/web_servers.html

Michel Feldheim
  • 17,625
  • 5
  • 60
  • 77