Captcha Module

General Presentation

The Captcha module provides APIs for generating captchas. As seen in the #displayCapcha() macro from the "xwiki/templates/commentsinline.vm" Velocity template, the captcha images are retrieved from "/xwiki/bin/imagecaptcha":

  <div id='commentCaptcha'>
  ## We only want to hide the captcha if we're sure the user has javascript, if this is an ajax call then it's a safe bet.
    <script type='text/javascript'>
    $('commentCaptcha').style.display = "none";
    var handler = function() {
      var captchaDiv = $('commentCaptcha');
      var image = new Element('img', {'src' : '$doc.getURL("imagecaptcha")?nounce=$datetool.getSystemTime()', 'alt' : '$escapetool.javascript($services.localization.render("core.captcha.image.alternateText"))'});
      captchaDiv.insertBefore(image, captchaDiv.firstChild);
      Event.observe(image, 'load', function() {
        Effect.SlideDown('commentCaptcha', { duration: 0.7 });
      Event.stopObserving($('${xCommentClass}_comment'), 'focus', handler);
    Event.observe($('${xCommentClass}_comment'), 'focus', handler);
    <img src='$doc.getURL("imagecaptcha")?nounce=$datetool.getSystemTime()' alt='$services.localization.render("core.captcha.image.alternateText")' />
  <input type='hidden' name='captcha_type' value='image'/>
  <label for='captcha_answer'>$services.localization.render('core.captcha.image.instruction')</label>
  <input id='captcha_answer' name='captcha_answer' type='text'>
    <span style="color:#C00; font-weight:bold; margin:0 0 0 5px;">$services.localization.render('core.captcha.captchaAnswerIsWrong')</span>
  <div class="wikimodel-emptyline"></div>

The extra forward slash character is required by "xwiki/WEB-INF/struts-config.xml":

<action path="/imagecaptcha/"

However, it is possible to put anything before or after the trailing "/".

The source code of the Captcha Module is available on GitHub.

Create a Captcha Type

The JCaptcha documentation lists the supported types, not only image but also sound captchas. To add a new captcha type to XWiki, just copy the DefaultImageCaptchaAction class and use a different "ImageCaptchaService" implementation. An explanatory example of application integration is available here

Verify the Answer

The answer verification is handled by the CaptchaVerifier Interface. JCaptcha uses a unique string identifier to differentiate users apart and know which user should be solving which captcha. The string is retrieved from the Request object when the user's browser loads the captcha image and supplied to the isAnswerCorrect() method in order to check the supplied answer against the stored answer. 

If you don't want to use JCapcha, you can implement the "CaptchaVerifier" Interface as explained in the XWiki Components tutorial.

Related Pages

Trying to decide between Cloud and On Premise? See the comparison on the XWiki Help Center.

Search this space