Bug 36197: (QA follow-up): Add public form captcha

Feedback provided by David Cook + Matthias Meusburger at KohaCon24

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
This commit is contained in:
Pedro Amorim 2025-04-11 10:32:36 +00:00 committed by Katrin Fischer
parent 3c54da2095
commit e677ba112f
No known key found for this signature in database
GPG Key ID: 0EF6E2C03357A834
4 changed files with 51 additions and 12 deletions

View File

@ -296,8 +296,10 @@ sub create {
C4::Context->preference("ILLOpacUnauthenticatedRequest") && !$other->{'cardnumber'};
if ($unauthenticated_request) {
( $failed, $result ) = _validate_form_params( $other, $result, $params );
if ( !Koha::ILL::Request::unauth_request_data_check($other) ) {
$result->{status} = "missing_unauth_data";
return $result if $failed;
my $unauth_request_error = Koha::ILL::Request::unauth_request_data_error($other);
if ($unauth_request_error) {
$result->{status} = $unauth_request_error;
$result->{value} = $params;
$failed = 1;
}

View File

@ -27,6 +27,8 @@
<div class="alert alert-warning">
<strong>Please note:</strong> The library you chose is invalid.
</div>
[% ELSIF whole.status == 'failed_captcha' %]
<div class="alert alert-warning">You typed in the wrong characters in the box before submitting. Please try again.</div>
[% ELSE %]
<p>Unhandled error</p>
[% END %]
@ -119,6 +121,22 @@
[% INCLUDE "${cwd}/shared-includes/forms/${type}.inc" %]
[% END %]
[% INCLUDE "${cwd}/shared-includes/custom_fields.inc" %]
[% IF whole.value.other.type && unauthenticated_ill && !logged_in_user%]
<fieldset class="rows" id="illrequest_captcha">
<legend>Verification</legend>
<ol>
<li>
<label for="captcha" class="required">Verification:</label>
<input type="text" name="captcha" id="captcha" class="form-control input-fluid" style="text-transform: uppercase;" />
<div class="required_label required">Required</div>
<input type="hidden" name="captcha_digest" value="[% captcha_digest | html %]" />
<span class="hint">Please type the following characters into the preceding box: <strong>[% captcha | html %]</strong></span>
</li>
</ol>
</fieldset>
[% END %]
<fieldset class="action">
<input id="ill-submit" class="btn btn-primary" type="submit" value="Create"/>
<a class="cancel" href="/cgi-bin/koha/opac-illrequests.pl">Cancel</a>

View File

@ -1968,23 +1968,25 @@ sub add_unauthenticated_data {
);
}
=head3 unauth_request_data_check
=head3 unauth_request_data_error
unauth_request_data_check($metadata);
unauth_request_data_error($metadata);
Checks if unauthenticated request data is present
Checks if unauthenticated request data errored, returns error_code if so, else 0
=cut
sub unauth_request_data_check {
sub unauth_request_data_error {
my ($metadata) = @_;
return 1 unless C4::Context->preference("ILLOpacUnauthenticatedRequest");
return
$metadata->{unauthenticated_first_name}
return 0 unless C4::Context->preference("ILLOpacUnauthenticatedRequest");
return 'missing_unauth_data'
unless $metadata->{unauthenticated_first_name}
&& $metadata->{unauthenticated_last_name}
&& $metadata->{unauthenticated_email};
return 'failed_captcha' if $metadata->{failed_captcha};
return 0;
}
=head3 append_to_note

View File

@ -24,8 +24,10 @@ use JSON qw( encode_json );
use CGI qw ( -utf8 );
use C4::Auth qw( get_template_and_user );
use C4::Koha;
use C4::Output qw( output_html_with_http_headers );
use POSIX qw( strftime );
use C4::Output qw( output_html_with_http_headers );
use Digest::MD5 qw( md5_base64 );
use POSIX qw( strftime );
use String::Random qw( random_string );
use Koha::ILL::Request::Config;
use Koha::ILL::Requests;
@ -119,6 +121,15 @@ if ( $op eq 'list' ) {
my $req = Koha::ILL::Request->new;
$template->param( backends => $backends );
} else {
if ( C4::Context->preference('ILLOpacUnauthenticatedRequest') && !$patron ) {
my $captcha = random_string("CCCCC");
$template->param(
captcha => $captcha,
captcha_digest => md5_base64($captcha),
);
}
$params->{backend} = 'Standard' if $params->{backend} eq 'FreeForm';
my $request = Koha::ILL::Request->new->load_backend( $params->{backend} );
@ -155,6 +166,12 @@ if ( $op eq 'list' ) {
exit;
}
if ( $request->_backend_capability( 'can_create_request', $params ) ) {
if ( md5_base64( uc( $params->{captcha} ) ) ne $params->{captcha_digest} ) {
$params->{failed_captcha} = 1;
}
}
$params->{cardnumber} = $patron->cardnumber if $patron;
$params->{branchcode} = $patron->branchcode if $patron;
$params->{opac} = 1;