From 23d249e759c9e99d4808e5aa5fa9067cd980eb4c Mon Sep 17 00:00:00 2001 From: David Cook Date: Tue, 27 Feb 2024 06:05:24 +0000 Subject: [PATCH] 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 Signed-off-by: Nick Clemens --- Koha/Template/Plugin/Koha.pm | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Koha/Template/Plugin/Koha.pm b/Koha/Template/Plugin/Koha.pm index ec5f975eb74..55671d2199d 100644 --- a/Koha/Template/Plugin/Koha.pm +++ b/Koha/Template/Plugin/Koha.pm @@ -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; -- 2.30.2