ASP4x-Captcha-Imager

 view release on metacpan or  search on metacpan

inc/Module/Install/Metadata.pm  view on Meta::CPAN

	$self->provides( %{ $build->find_dist_packages || {} } );
}

sub feature {
	my $self     = shift;
	my $name     = shift;
	my $features = ( $self->{values}{features} ||= [] );
	my $mods;

	if ( @_ == 1 and ref( $_[0] ) ) {
		# The user used ->feature like ->features by passing in the second
		# argument as a reference.  Accomodate for that.
		$mods = $_[0];
	} else {
		$mods = \@_;
	}

	my $count = 0;
	push @$features, (
		$name => [
			map {

lib/ASP4x/Captcha/Imager.pm  view on Meta::CPAN

  
  IMG {
    border: dotted 1px #AAA;
  }
  </style>
  </head>
  <body>
    <form action="/handlers/dev.validate" method="post">
      <p>
        <label>Enter the code you see below:</label>
        <input type="text" name="security_code" />
      </p>
      <p>
        <label>&nbsp;</label>
        <img id="captcha" src="/handlers/dev.captcha?r=<%= rand() %>" alt="Security Code" />
        <a href="" onclick="document.getElementById('captcha').src = '/handlers/dev.captcha?r=' + Math.random(); return false">
          (Click for a new Image)
        </a>
      </p>
      <p>
        <label>&nbsp;</label>

lib/ASP4x/Captcha/Imager.pm  view on Meta::CPAN


  use strict;
  use warnings 'all';
  use base 'ASP4::FormHandler';
  use vars __PACKAGE__->VARS;

  sub run
  {
    my ($s, $context) = @_;
    
    my $secret = $Config->system->settings->captcha_key;
    my $code = lc($Form->{security_code});
    
    # It should exist in the session and have the correct value:
    if( exists($Session->{asp4captcha}->{$code}) )
    {
      # Ding ding ding ding ding!
      $Response->Write("CORRECT");
    }
    else
    {
      # Bzzzzzzzzzzt: WRONG!
      $Response->Write("WRONG");
    }# end if()
  }# end run()

  1;# return true:

=head1 DESCRIPTION

"CAPTCHA" is the little security image containing a hard-to-read code that you may
have seen on some websites.  They are common on sign-up forms and email forms.  The
idea is that a bot or script can't read the image and can't guess the code.

C<ASP4x::Captcha::Imager> uses L<Imager> to generate an image of a random string
of numbers and letters.

=head2 What Does the Captcha Image Look Like?

You can see an example in the example/example.png file included with this distribution.

t/010-basic/020-basic.t  view on Meta::CPAN

);

is(
  $res->header('content-type') => "image/png",
  "content-type is image/png"
);

my $Session = $api->context->session;

my $wordLength = $api->context->config->system->settings->captcha_length;
my $secret = $api->context->config->system->settings->captcha_key;

my ($word) = grep {
  length($_) == $wordLength &&
  md5_hex($_ . $secret) eq $Session->{asp4captcha}->{$_};
} keys %{ $Session->{asp4captcha} };

ok( $word, "Found word '$word'");

my $hashed = md5_hex($word . $secret);
is($hashed => $Session->{asp4captcha}->{$word}, "Hashing is correct" );


t/010-basic/030-form.t  view on Meta::CPAN

  ok(
    $api->ua->get('/handlers/dev.captcha?r=' . rand()),
    "Got the captcha image"
  );
  ok(
    my ($form) = HTML::Form->parse( $res->content, '/' ),
    "Got the form"
  );


  # Figure out what the security code is:
  my $Session = $api->context->session;
  my $wordLength = $api->context->config->system->settings->captcha_length;
  my $secret = $api->context->config->system->settings->captcha_key;
  my ($word) = grep {
    length($_) == $wordLength &&
    md5_hex($_ . $secret) eq $Session->{asp4captcha}->{$_};
  } keys %{ $Session->{asp4captcha} };


  # Fill out the form - lowercase:
  $form->find_input('security_code')->value( lc($word) );
  $res = $api->ua->submit_form( $form );
  is( $res->content => "CORRECT", "Validated correctly (A)" );


  # Fill out the form - uppercase:
  $form->find_input('security_code')->value( uc($word) );
  $res = $api->ua->submit_form( $form );
  is( $res->content => "CORRECT", "Validated correctly (B)" );

  # Fill out the form - WRONG:
  $form->find_input('security_code')->value( 'no-way-jose' );
  $res = $api->ua->submit_form( $form );
  is( $res->content => "WRONG", "Validated correctly (C)" );
}# end for()

t/handlers/dev/validate.pm  view on Meta::CPAN

use strict;
use warnings 'all';
use base 'ASP4::FormHandler';
use vars __PACKAGE__->VARS;
use Digest::MD5 'md5_hex';

sub run
{
  my ($s, $context) = @_;
  
  my $secret = $Config->system->settings->captcha_key;
  my $code = lc($Form->{security_code});
  
  # It should exist in the session and have the correct value:
  if( exists($Session->{asp4captcha}->{$code}) && md5_hex($code . $secret) eq $Session->{asp4captcha}->{$code} )
  {
    $Response->Write("CORRECT");
  }
  else
  {
    # Bzzzzzzzzzzt: WRONG!
    $Response->Write("WRONG");
  }# end if()
}# end run()

t/htdocs/form.asp  view on Meta::CPAN

<html>
<body>

  <form action="/handlers/dev.validate" method="post">
    <p>
      <label>Enter the code you see below:</label>
      <input type="text" name="security_code" />
    </p>
    <p>
      <label>&nbsp;</label>
      <img id="captcha" src="/handlers/dev.captcha?r=<%= rand() %>" alt="Security Code" />
      <br/>
      <a href="" onclick="document.getElementById('captcha').src = '/handlers/dev.captcha?r=' + Math.random(); return false">
        (Click for a new Image)
      </a>
    </p>
    <p>



( run in 1.126 second using v1.01-cache-2.11-cpan-39bf76dae61 )