Bug 35955: Cache CSRF token in template plugin

This change uses the Koha::Cache::Memory::Lite cache to
cache the CSRF token, so that it is only generated once,
and is re-used by the Koha::Template::Plugin::Koha object
throughout the entire template processing for the HTTP request.

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
David Cook 2024-02-27 06:05:24 +00:00 committed by Jonathan Druart
parent e2440f2c61
commit 108c955eac
Signed by: jonathan.druart
GPG key ID: A085E712BEF0E0F0

View file

@ -24,6 +24,7 @@ use base qw( Template::Plugin );
use C4::Context;
use Koha::Token;
use Koha;
use Koha::Cache::Memory::Lite;
=head1 NAME
@ -107,8 +108,16 @@ Generate a new CSRF token.
sub GenerateCSRF {
my ($self) = @_;
my $memory_cache = Koha::Cache::Memory::Lite->get_instance;
my $cache_key = "CSRF_TOKEN";
my $cached = $memory_cache->get_from_cache($cache_key);
return $cached if $cached;
my $session_id = $self->{_CONTEXT}->stash->{sessionID};
return Koha::Token->new->generate_csrf( { session_id => scalar $session_id } );
my $csrf_token = Koha::Token->new->generate_csrf( { session_id => scalar $session_id } );
$memory_cache->set_in_cache( $cache_key, $csrf_token );
return $csrf_token;
}
1;