View | Details | Raw Unified | Return to bug 21336
Collapse All | Expand All

(-)a/Koha/Token.pm (-2 / +3 lines)
Lines 79-84 sub new { Link Here
79
    });
79
    });
80
80
81
    Generate several types of tokens. Now includes CSRF.
81
    Generate several types of tokens. Now includes CSRF.
82
    For non-CSRF tokens an optional pattern parameter overrides length.
82
    Room for future extension.
83
    Room for future extension.
83
84
84
=cut
85
=cut
Lines 196-203 sub _gen_rand { Link Here
196
    my ( $params ) = @_;
197
    my ( $params ) = @_;
197
    my $length = $params->{length} || 1;
198
    my $length = $params->{length} || 1;
198
    $length = 1 unless $length > 0;
199
    $length = 1 unless $length > 0;
199
200
    my $pattern = $params->{pattern} // '.{'.$length.'}'; # pattern overrides length parameter
200
    return String::Random::random_string( '.' x $length );
201
    return String::Random::random_regex( $pattern );
201
}
202
}
202
203
203
=head1 AUTHOR
204
=head1 AUTHOR
(-)a/t/Token.t (-2 / +11 lines)
Lines 20-26 Link Here
20
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21
21
22
use Modern::Perl;
22
use Modern::Perl;
23
use Test::More tests => 10;
23
use Test::More tests => 11;
24
use Time::HiRes qw|usleep|;
24
use Time::HiRes qw|usleep|;
25
use C4::Context;
25
use C4::Context;
26
use Koha::Token;
26
use Koha::Token;
Lines 89-91 subtest 'Same logged in user with another session (cookie CGISESSID)' => sub { Link Here
89
    });
89
    });
90
    is( $result, '', "CSRF token is not verified if another session is used" );
90
    is( $result, '', "CSRF token is not verified if another session is used" );
91
};
91
};
92
- 
92
93
subtest 'Pattern parameter' => sub {
94
    plan tests => 4;
95
    my $id = $tokenizer->generate({ pattern => '\d\d', length => 8 });
96
    is( length($id), 2, 'Pattern overrides length' );
97
    ok( $id =~ /\d{2}/, 'Two digits found' );
98
    $id = $tokenizer->generate({ pattern => '[A-Z]{10}' });
99
    is( length($id), 10, 'Check length again' );
100
    ok( $id !~ /[^A-Z]/, 'Only uppercase letters' );
101
};

Return to bug 21336