Captcha Module

Version 4.5 by Vincent Massol on 2010/10/26 09:49

The captcha module supplies captchas and checks answers for correctness. It uses JCaptcha 2.0 to get captchas and supplies them through a struts action.

captchaEx2.png

Getting the captcha image

The captcha images are served from yourwiki.com/xwiki/bin/imagecaptcha// 
The extra trailing / is required by struts but you can put anything before or after it so:

xwiki.getURL(doc.getFullName(), "imagecaptcha")

will output yourwiki.com/xwiki/bin/imagecaptcha/SomeSpace/SomePage which will work.

Verifying the answer

To verify the captcha answer in Groovy, you simply get the CaptchaVerifier using Utils.getComponent
then call isAnswerCorrect

import com.xpn.xwiki.web.Utils;
import org.xwiki.captcha.CaptchaVerifier;
CaptchaVerifier verifier = Utils.getComponent(CaptchaVerifier.class, "image");
verifier.isAnswerCorrect(verifier.getUserId(request), userSuppliedAnswer);

JCaptcha uses a unique String to tell different users apart so it knows who should be solving which captcha. This String is gotten from the Request object when the user's browser loads the captcha image and it must be supplied to isAnswerCorrect in order to check the supplied answer against the right stored answer. You can get this String by calling getUserId with the Request object.

For more information, you can read the code for the interface CaptchaVerifier.

What about sound captchas?

Sound captcha support will be added when JCaptcha 2.0 is officially released.

How about implementing your own captcha type?

JCaptcha supports a variety of different types of captchas, you can add a new captcha type to XWiki by copying DefaultImageCaptchaAction but using a different JCaptcha ImageCaptchaService implementation.

You can also add a different captcha type which doesn't use JCaptcha at all, all you have to do is implement CaptchaVerifier. Remember that you will be writing a component so it's a good idea to take a look at the ComponentModule.

Tags: development
    

Get Connected