Bug 21998: Add pattern parameter in Koha::Token

Allow a pattern too in Koha::Token->generate. Only supported length.
Does not affect CSRF calls. So tiny change without further impact.

Test plan:
Run  t/Token.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
(cherry picked from commit b794643bb0)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Marcel de Rooy 2018-09-19 15:36:36 +02:00 committed by Martin Renvoize
parent b875f2668d
commit 49d3cd6a7b
No known key found for this signature in database
GPG Key ID: 422B469130441A0F
2 changed files with 14 additions and 3 deletions

View File

@ -79,6 +79,7 @@ sub new {
});
Generate several types of tokens. Now includes CSRF.
For non-CSRF tokens an optional pattern parameter overrides length.
Room for future extension.
=cut
@ -196,8 +197,8 @@ sub _gen_rand {
my ( $params ) = @_;
my $length = $params->{length} || 1;
$length = 1 unless $length > 0;
return String::Random::random_string( '.' x $length );
my $pattern = $params->{pattern} // '.{'.$length.'}'; # pattern overrides length parameter
return String::Random::random_regex( $pattern );
}
=head1 AUTHOR

View File

@ -20,7 +20,7 @@
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
use Modern::Perl;
use Test::More tests => 10;
use Test::More tests => 11;
use Time::HiRes qw|usleep|;
use C4::Context;
use Koha::Token;
@ -89,3 +89,13 @@ subtest 'Same logged in user with another session (cookie CGISESSID)' => sub {
});
is( $result, '', "CSRF token is not verified if another session is used" );
};
subtest 'Pattern parameter' => sub {
plan tests => 4;
my $id = $tokenizer->generate({ pattern => '\d\d', length => 8 });
is( length($id), 2, 'Pattern overrides length' );
ok( $id =~ /\d{2}/, 'Two digits found' );
$id = $tokenizer->generate({ pattern => '[A-Z]{10}' });
is( length($id), 10, 'Check length again' );
ok( $id !~ /[^A-Z]/, 'Only uppercase letters' );
};