I’m currently developing a new set of Restfull APIs on Symfony2.1, and they are for the moment under no firewall in my security.yml
api:
pattern: ^/api
security: false
I have a RequestListener that “protect” them by checking if users give an Auth token or use Basic Auth. After correct login, we populate the security context with the user. (Maybe we could even make a firewall of that using a Factory?)
This works perfectly for external devs / organisations who want to use our API in their apps.
Now, I’d like us to rely on these same APIs inside our project (controllers, ajax calls..) and I was wondering now if we have to implement ourselves the API get-token or Basic Auth process to populate the security context of the API or if they could, in a way or another, retrieve magically the current security context of the main firewall. (it would save me the embarrassment of geting a token, saving it somewhere and passing it through my Backbonejs ajax calls all the way in my views).
Thanks for your thoughts on that! 🙂
First of all, for basic auth, you could rely on the
http_basicauthentication provider, provided by Symfony2. No need to use a request listener. If you want to use a token-based authentication, write a Token authentication provider.Security contexts are separated, and you can’t interact with another context. They are completely partitioned. However, you can add as many authentication provider as you want.
Using Backbone.js, you can keep the token-based strategy. For instance, pass the token to Backbone using a HTML attribute:
Then, just use it in your JavaScript app:
See the $.ajaxSetup doc for more information.