Implement moar CAPTCHA, need to revisit the tests...

This commit is contained in:
swagg boi 2023-08-05 13:19:33 -04:00
parent ec82ffae25
commit a0312d24f8
3 changed files with 44 additions and 5 deletions

View file

@ -135,7 +135,7 @@ sub startup($self) {
$r->get('/rules')->to('page#rules')->name('rules_page'); $r->get('/rules')->to('page#rules')->name('rules_page');
$r->get('/captcha/:return_url') $r->any('/captcha/:return_url')
->to('page#captcha') ->to('page#captcha')
->name('captcha_page'); ->name('captcha_page');

View file

@ -1,11 +1,50 @@
package PostText::Controller::Page; package PostText::Controller::Page;
use Mojo::Base 'Mojolicious::Controller', -signatures; use Mojo::Base 'Mojolicious::Controller', -signatures;
use Mojo::Util qw{b64_decode gunzip};
use Roman::Unicode qw{to_roman to_perl};
sub about($self) { $self->render } sub about($self) { $self->render }
sub rules($self) { $self->render } sub rules($self) { $self->render }
sub captcha($self) { $self->render } sub captcha($self) {
my $v;
'false'; $v = $self->validation if $self->req->method eq 'POST';
if ($v && $v->has_data) {
$v->required('answer')->num(1, 9);
$v->required('number')->size(1, 4);
if ($v->has_error) {
$self->stash(status => 400)
}
else {
my $answer = $v->param('answer');
my $roman_numeral = $v->param('number');
my $return_url =
gunzip b64_decode $self->param('return_url');
if ($answer == to_perl $roman_numeral) {
$self->session(is_human => 1);
return $self->redirect_to($return_url);
}
else {
$self->stash(
error => 'Sounds like something a robot would say...'
)
}
}
}
my $random_int = 1 + int rand 9;
my $roman_numeral = to_roman $random_int;
$self->stash(roman_numeral => $roman_numeral);
$self->render;
}
1;

View file

@ -16,7 +16,7 @@
<p class="field-with-error">Must be between <%= $error->[2] %> <p class="field-with-error">Must be between <%= $error->[2] %>
and <%= $error->[3] %> characters.</p> and <%= $error->[3] %> characters.</p>
<% } =%> <% } =%>
<%#= label_for answer => "What number is this?: $roman_number" %> <%= label_for answer => "What roman numeral is this?: $roman_numeral" %>
<%= text_field 'answer', ( <%= text_field 'answer', (
id => 'answer', id => 'answer',
maxlength => 1, maxlength => 1,
@ -24,6 +24,6 @@
required => undef required => undef
) %> ) %>
</div> </div>
<%= hidden_field url => $return_url, id => 'url' %> <%= hidden_field number => $roman_numeral, id => 'number' %>
<button type="submit" class="form-button">Answer</button> <button type="submit" class="form-button">Answer</button>
</form> </form>