Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Captcha not validating in Symfony 2.5.3 #99

Closed
SvetlinStaev opened this issue Aug 12, 2014 · 6 comments
Closed

Captcha not validating in Symfony 2.5.3 #99

SvetlinStaev opened this issue Aug 12, 2014 · 6 comments

Comments

@SvetlinStaev
Copy link

Hi,

I just installed the Captcha Bundle via composer and enabled the bundle in the Kernel, configured it in config.yml and into a FormType form as follow:

$builder->add('username', 'text', array('attr'=>array('placeholder'=>'Admin username', 'autofocus'=>'')))
->add('password', 'password', array('attr'=>array('placeholder'=>'Password')))
->add('captcha', 'captcha', array('background_color'=>array(234,234,236)))
->add('remember_me', 'checkbox', array('required'=>false));

It renders fine on the login page of my site but when I submit the form it just doesn't validate the captcha in any way. I checked in the Profiler and it shows the captcha sessions details:

gcb_captcha [
phrase => 6cbzz,
width => 130,
height => 35,
distortion => true,
length => 5,
quality => 100,
background_color => [
0 => 234,
1 => 234,
2 => 236
],
text_color => []
]

but it doesn't validate against that. I am using it in a standard form to authenticate the user against a firewall. Do I need to attach any listeners or something else to the login_check to ensure the captcha is validated or am I missing something in the configuration?

Thanks,

Svetlin

@Gregwar
Copy link
Owner

Gregwar commented Aug 12, 2014

Hello
What happen?
I dont understand if your code is not checker or if it is always checked wrong

@SvetlinStaev
Copy link
Author

It is not being checked at all. I tried to debug it with XDebug to see if the captcha is validated at all or it is something I am missing in the setup but it seems it doesn't even go through the validate method in the CaptchaValidator class. I set a breakpoint on line 83:

if (!($code && is_string($code) && ($this->compare($code, $expectedCode) || $this->compare($code, $this->bypassCode)))) {

But it doesn't execute it and I tried entering invalid and valid values of the captcha.

@Gregwar
Copy link
Owner

Gregwar commented Aug 13, 2014

Actually, if you're talking about the logging form I guess this is normal

This form is not checked by the usual validation process but intercepted by
security services...

I'm affraid you'll have to do trickier things like creating events or
authentication provider to do this

Keep us on touch if you find any more information
Le 13 août 2014 07:24, "Svetlin Staev" [email protected] a écrit :

It is not being checked at all. I tried to debug it with XDebug to see if
the captcha is validated at all or it is something I am missing in the
setup but it seems it doesn't even go through the validate method in the
CaptchaValidator class. I set a breakpoint on line 83:

if (!($code && is_string($code) && ($this->compare($code, $expectedCode)
|| $this->compare($code, $this->bypassCode)))) {

But it doesn't execute it and I tried entering invalid and valid values of
the captcha.


Reply to this email directly or view it on GitHub
#99 (comment)
.

@SvetlinStaev
Copy link
Author

I managed the validation via a custom authentication provider:

encoderFactory = $encoderFactory; $this->session = $session; } public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) { $request = Request::createFromGlobals()->request->get('admin_login'); $session = $this->session->get('gcb_captcha'); // Check for valid captcha if (isset($request['captcha']) && isset($session['phrase']) && !$this->compare($request['captcha'], $session['phrase'])) { throw new AuthenticationException('Invalid Access Code!'); } try { $user = $userProvider->loadUserByUsername($token->getUsername()); } catch (UsernameNotFoundException $e) { throw new AuthenticationException('Admin User Not Found!'); } $encoder = $this->encoderFactory->getEncoder($user); $passwordValid = $encoder->isPasswordValid( $user->getPassword(), $token->getCredentials(), $user->getSalt() ); if ($passwordValid) { return new UsernamePasswordToken( $user, $user->getPassword(), $providerKey, $user->getRoles() ); } throw new AuthenticationException('Invalid password for administrator - '.$token->getUsername().'!'); } public function supportsToken(TokenInterface $token, $providerKey) { return $token instanceof UsernamePasswordToken && $token->getProviderKey() === $providerKey; } public function createToken(Request $request, $username, $password, $providerKey) { return new UsernamePasswordToken($username, $password, $providerKey); } ``` } in security.yml: security: firewalls: admin_area: simple_form: authenticator: admin_login_authenticator in services.yml: admin_login_authenticator: class: Icepique\AdminBundle\Security\AdminLoginAuthenticator arguments: ["@security.encoder_factory", "@session"] I am not really great in Symfony 2 development, there might be a better or more elegant way of doing it. In case the author wants to include it as a new feature in the bundle maybe he can implement it.

@vchabot
Copy link

vchabot commented Aug 14, 2014

Hello,

First, thanks for your bundle, which is a great time saving!

I have the same problem, on a v2.5.3 Symfony2 version, I added as you mentionned on the documentation the following code to get the captcha:

<?php
    // ...
    $builder->add('captcha', 'captcha'); // That's all !
    // ...

I already installed the bundle using composer, and added the following line to my AppKernel.php file:

<?php
// app/appKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Gregwar\CaptchaBundle\GregwarCaptchaBundle(),
    );
}

The thing is, when I submit my form (basic contact form), my form is not valid and I have no error, even when I dump the following method:

$form->getErrorsAsString();

I tried to see in the sources if we go through the validate method in the Gregwar\CaptchaBundle\Validator\CaptchaValidator class, but it seems not to go through...

Did I miss something?

Vincent

@vchabot
Copy link

vchabot commented Aug 18, 2014

Hello,

I come back to tell you that I understood why my captcha was not validated.

As you use the event listener FormEvents::POST_BIND to add your validator, you must bind the form to the request in your controller. As I did not bind my form with my request, I could not validate the captcha. So, you must add this line if not already done:

$form->bind($this->getRequest());

Mistake from me, I apologize for this double post.

Vincent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants