Symfony2: How To Perform Custom Validation Before Authentication? - symfony

I am using Symfony login system which is symfony will take care of the entire login process for me as long as I got the configuration (firewall) correctly.
How can I perform some custom validation before it performs the authentication process? For example, check the length of username, validate captcha code and etc. If one of the requirement does not meet, I need to stop further action (authentication) and return error message.
I know there is some authentication handlers like authentication success handler or authentication failure handler which allow you to do some action upon authentication successful or failure. Is there any handler like this that allow me put my custom code before performing authentication action?

You can create a custom authenticator, this is all you need:
http://symfony.com/doc/current/cookbook/security/custom_password_authenticator.html#the-password-authenticator

Related

FOSUser bundle: Is it possible to stop user registration inside REGISTRATION_SUCCESS or REGISTRATION_INITIALIZE events?

I am implementing recaptcha with fosuserbundle.
Is it possible to stop user registration or password reset with an error message through event listeners?
Or should I just override the controllers?
I would prefer to avoid using a third bundle to integrate recaptcha
Thanks a lot for your time.
The registration_success occure just after sucessful submitting and perform the result in database so, just before performing you can send a httresponse (inside the response event) that provide redirection (or otherelse)

Logging a user's login and logout activities

I am new to Spring. I have a requirement to log all successful login and logout attempts by a user using logback.
I am using one HandleInterceptor(org.springframework.web.servlet.HandlerInterceptor) to intercept all requests made to handlers.
Can I use the same interceptors to handle authentication requests too?
If not which interceptors to be used. Or is there any way around?
As you are only interested in login and logout operation, you should not use a handler interceptor on all requests.
There is a simple way to be notified all all login events : use an ApplicationListener that will listen to AuthenticationSuccessEvent or InteractiveAuthenticationSuccessEvent - you will find more details and example code on this other post Spring security: adding “On successful login event listener”
To be notified on logout, you can use a custom SuccessfullLogoutHandler or add a LogoutHandler to the LogoutFilter. You can configure it with the <logout> attribute in xml configuration or with a SecurityContextLogoutHandler if you are using java configuration in spring security 3.2

Asp.net mvc Authentication filter overide default reditect

In my MVC 5 application I have applied some Role filters such as:
[Authorize(Roles = "ManageRoles")]
Now if a user does not have permission to access this role, it redirects me to the login page. This is incorrect in my application as I want to rather display an error message saying that you donot have permission to access.
Where do I change what happens if a user is not authorised to access a filter?
Do I have to implement custom filters? I would like to try and use the redirectTo action if possible so that I can have different error pages in different situations.
Use Custom Authorize Attribure - Ben Scheirman or Ben Cull's answer in this thread.
Also Check Mark's response to similar question, where he used HandleUnauthorizedRequest to redirect unauthorized users.
In the both the above approaches, you can redirect to any Route or Action of your interest and use HttpContent.Items[] or TempData to hold the specific error messages or values to be display on the destination page.
MVC5 has actually started to address this issue. They now include Authentication Filters in addition to Authorization Filters. These are pretty lightly documented, but my gut feeling is that this a first stab at separating authentication from authorization (up until now, ASP.NET has confused the two)
What i'm thinking is that Authentication filters will be used to control whether a user is logged in or not, and Authorization filters will be used to control what you have access to. However, it seems that this isn't yet fully realized.
In your Login view, you can add logic for:
Checking if the request is not authenticated
1.1. Display login form
Checking if the user is authenticated but not in the required role
2.1. Display error message
Since you'll be automatically redirected to the login page by your Web.config settings, you can take advantage of this mechanism.
if (!Request.IsAuthenticated)
{
//render login form
}
else
{
<p>Error: you do not have the necessary credentials to access this resource.</p>
}
Another option would be to create your own AuthorizationAttribute. This question is similar to yours. You might find it useful.

Execution order against authorization filter

Using Fluent Security, I have configured website access using DenyAnonymousAccess, DenyAuthenticationAccess and RequireRole. However, since HandleSecurityAttribute is not an authorization filter, all logic to handle DenyAnonymousAccessPolicyViolation only get executed after authorization filter, such as ValidateAntiForgeryToken.
This means that if current user's authentication ticket is timeout, any action decorated with ValidateAntiForgeryToken will throw exception since the token is not created for anonymous user.
So is there a way to work around this? I am currently looking at two solutions:
reate my own authorization filter to perform exact what HandleSecurityAttribute security does.
Use the global Authorize filer to handle authentication and leave role / other access policy to FluentSecurity.
Update:
I checked in a demo at github, the username and password for this is dev1 and devdev. I set the timeout forms authentication to 1 minute. So if you login, and wait for one minute, then click on logoff, you will get the token is not created for anonymous user. In normal MVC, this won't happen since [Authorize] runs before [ValidateAntiForgeryToken], which redirect user back to login screen.
I haven't had any problems with this. Make sure that you put the HandleSecurityAttribute first in the filters list!
GlobalFilters.Filters.Add(new HandleSecurityAttribute(), 0);

ASP.NET Event OnAuthenticationFailed?

Is there an event that gets fired right after ASP.NET authentication has failed to identify the user? I need to trigger an alarm every time that happens. Don't offer to implement custom membership provider nor to do it from the Login form's controller. I'm looking for native ASP.NET pipeline event. HttpApplication has two events: one for before Authentication and one after, but nothing for failure.
As far as I remeber, yes:
Have a close look at the logging capabilites of the HealthMonitoring section. It is not only possible to log errors, but also events such as successfull logins or failed login.
Have a look at http://www.asp.net/hosting/tutorials/logging-error-details-with-asp-net-health-monitoring-cs
I guess eventName="Failure Audits" is the way to go

Resources