3 # Created as wrapper for CSRF tokens, but designed for more general use
5 # Copyright 2016 Rijksmuseum
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 3 of the License, or (at your option) any later
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along
19 # with Koha; if not, write to the Free Software Foundation, Inc.,
20 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 Koha::Token - Tokenizer
29 my $tokenizer = Koha::Token->new;
30 my $token = $tokenizer->generate({ length => 20 });
32 # safely generate a CSRF token (nonblocking)
33 my $csrf_token = $tokenizer->generate({
34 CSRF => 1, id => $id, secret => $secret,
37 # or check a CSRF token
38 my $result = $tokenizer->check({
39 CSRF => 1, id => $id, secret => $secret, token => $token,
44 Designed for providing general tokens.
45 Created due to the need for a nonblocking call to Bytes::Random::Secure
46 when generating a CSRF token.
51 use base qw(Class::Accessor);
52 use constant HMAC_SHA1_LENGTH => 20;
58 Create object (via Class::Accessor).
64 return $class->SUPER::new();
69 my $token = $tokenizer->generate({ length => 20 });
70 my $csrf_token = $tokenizer->generate({
71 CSRF => 1, id => $id, secret => $secret,
74 Generate several types of tokens. Now includes CSRF.
75 Room for future extension.
80 my ( $self, $params ) = @_;
81 if( $params->{CSRF} ) {
82 $self->{lasttoken} = _gen_csrf( $params );
84 $self->{lasttoken} = _gen_rand( $params );
86 return $self->{lasttoken};
91 my $result = $tokenizer->check({
92 CSRF => 1, id => $id, secret => $secret, token => $token,
95 Check several types of tokens. Now includes CSRF.
96 Room for future extension.
101 my ( $self, $params ) = @_;
102 if( $params->{CSRF} ) {
103 return _chk_csrf( $params );
108 # --- Internal routines ---
112 # Since WWW::CSRF::generate_csrf_token does not use the NonBlocking
113 # parameter of Bytes::Random::Secure, we are passing random bytes from
114 # a non blocking source to WWW::CSRF via its Random parameter.
117 return if !$params->{id} || !$params->{secret};
119 require Bytes::Random::Secure;
122 my $randomizer = Bytes::Random::Secure->new( NonBlocking => 1 );
123 # this is most fundamental: do not use /dev/random since it is
124 # blocking, but use /dev/urandom !
125 my $random = $randomizer->bytes( HMAC_SHA1_LENGTH );
126 my $token = WWW::CSRF::generate_csrf_token(
127 $params->{id}, $params->{secret}, { Random => $random },
135 return if !$params->{id} || !$params->{secret} || !$params->{token};
138 my $csrf_status = WWW::CSRF::check_csrf_token(
143 return $csrf_status == WWW::CSRF::CSRF_OK();
148 my $length = $params->{length} || 1;
149 $length = 1 unless $length > 0;
151 require String::Random;
152 return String::Random::random_string( '.' x $length );
157 Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands