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":

#macro(displayCaptcha)
  <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.
  #if($hideCaptcha)
    <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);
    }.bind(this);
    Event.observe($('${xCommentClass}_comment'), 'focus', handler);
    </script>
  #else
    <img src='$doc.getURL("imagecaptcha")?nounce=$datetool.getSystemTime()' alt='$services.localization.render("core.captcha.image.alternateText")' />
  #end
  <input type='hidden' name='captcha_type' value='image'/>
  <br/>
  <label for='captcha_answer'>$services.localization.render('core.captcha.image.instruction')</label>
  <br/>
  <input id='captcha_answer' name='captcha_answer' type='text'>
  #if($captchaAnswerWrong)
    <span style="color:#C00; font-weight:bold; margin:0 0 0 5px;">$services.localization.render('core.captcha.captchaAnswerIsWrong')</span>
  #end
  <div class="wikimodel-emptyline"></div>
  </div>

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

<action path="/imagecaptcha/"
 type="org.xwiki.captcha.internal.DefaultImageCaptchaAction">
</action>

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

Search this space

 

Most popular tags

Failed to execute the [groovy] macro
  1. access rights
  2. activity stream
  3. annotation
  4. attachment
  5. comment
  6. Document Tree Macro
  7. export
  8. Extension Manager
  9. Flamingo skin
  10. global user
  11. Groovy event listener
  12. group
  13. nested page
  14. search
  15. skin
  16. syntax
  17. user
  18. user profile
  19. velocity macros
  20. wiki
  21. wysiwyg
  22. XWiki Applications
  23. xwikiattachment_archive table
  24. xwikiattachment table
  25. xwikiattrecyclebin table
  26. xwikiproperties table

[Display all tags from this space]