Authenticating a user is done by the firewall. An application may have multiple secured areas, so the firewall is configured using a map of these secured areas. For each of these areas, the map contains a request matcher and a collection of listeners. The request matcher gives the firewall the ability to find out if the current request points to a secured area. The listeners are then asked if the current request can be used to authenticate the user:
The firewall map will be given to the firewall as its first argument, together with the event dispatcher that is used by the HttpKernel
:
Firewall Listeners¶
When the firewall gets notified of the kernel.request
event, it asks the firewall map if the request matches one of the secured areas. The first secured area that matches the request will return a set of corresponding firewall listeners (which each implement ListenerInterface
). These listeners will all be asked to handle the current request. This basically means: find out if the current request contains any information by which the user might be authenticated (for instance the Basic HTTP authentication listener checks if the request has a header called PHP_AUTH_USER
).
Exception Listener¶
If any of the listeners throws an AuthenticationException
, the exception listener that was provided when adding secured areas to the firewall map will jump in.
The exception listener determines what happens next, based on the arguments it received when it was created. It may start the authentication procedure, perhaps ask the user to supply their credentials again (when they have only been authenticated based on a “remember-me” cookie), or transform the exception into an AccessDeniedHttpException
, which will eventually result in an “HTTP/1.1 403: Access Denied” response.
Entry Points¶
When the user is not authenticated at all (i.e. when the token storage has no token yet), the firewall’s entry point will be called to “start” the authentication process. An entry point should implement AuthenticationEntryPointInterface
, which has only one method: start()
. This method receives the current Request
object and the exception by which the exception listener was triggered. The method should return a Response
object. This could be, for instance, the page containing the login form or, in the case of Basic HTTP authentication, a response with a WWW-Authenticate
header, which will prompt the user to supply their username and password.