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 type => 'CSRF', id => $id, secret => $secret,
37 # generate/check CSRF token with defaults and session id
38 my $csrf_token = $tokenizer->generate_csrf({ session_id => $x });
39 my $result = $tokenizer->check_csrf({
40 session_id => $x, token => $token,
45 Designed for providing general tokens.
46 Created due to the need for a nonblocking call to Bytes::Random::Secure
47 when generating a CSRF token.
52 use Bytes
::Random
::Secure
();
53 use String
::Random
();
55 use Digest
::MD5
qw(md5_base64);
56 use Encode
qw( encode );
57 use base
qw(Class::Accessor);
58 use constant HMAC_SHA1_LENGTH
=> 20;
59 use constant CSRF_EXPIRY_HOURS
=> 8; # 8 hours instead of 7 days..
65 Create object (via Class::Accessor).
71 return $class->SUPER::new
();
76 my $token = $tokenizer->generate({ length => 20 });
77 my $csrf_token = $tokenizer->generate({
78 type => 'CSRF', id => $id, secret => $secret,
81 Generate several types of tokens. Now includes CSRF.
82 Room for future extension.
87 my ( $self, $params ) = @_;
88 if( $params->{type
} && $params->{type
} eq 'CSRF' ) {
89 $self->{lasttoken
} = _gen_csrf
( $params );
91 $self->{lasttoken
} = _gen_rand
( $params );
93 return $self->{lasttoken
};
98 Like: generate({ type => 'CSRF', ... })
99 Note: id defaults to userid from context, secret to database password.
100 session_id is mandatory; it is combined with id.
105 my ( $self, $params ) = @_;
106 return if !$params->{session_id
};
107 $params = _add_default_csrf_params
( $params );
108 return $self->generate({ %$params, type
=> 'CSRF' });
113 my $result = $tokenizer->check({
114 type => 'CSRF', id => $id, token => $token,
117 Check several types of tokens. Now includes CSRF.
118 Room for future extension.
123 my ( $self, $params ) = @_;
124 if( $params->{type
} && $params->{type
} eq 'CSRF' ) {
125 return _chk_csrf
( $params );
132 Like: check({ type => 'CSRF', ... })
133 Note: id defaults to userid from context, secret to database password.
134 session_id is mandatory; it is combined with id.
139 my ( $self, $params ) = @_;
140 return if !$params->{session_id
};
141 $params = _add_default_csrf_params
( $params );
142 return $self->check({ %$params, type
=> 'CSRF' });
145 # --- Internal routines ---
147 sub _add_default_csrf_params
{
149 $params->{session_id
} //= '';
150 if( !$params->{id
} ) {
151 $params->{id
} = Encode
::encode
( 'UTF-8', C4
::Context
->userenv->{id
} . $params->{session_id
} );
153 $params->{id
} .= $params->{session_id
};
155 $params->{id
} //= Encode
::encode
( 'UTF-8', C4
::Context
->userenv->{id
} );
156 my $pw = C4
::Context
->config('pass');
157 $params->{secret
} //= md5_base64
( Encode
::encode
( 'UTF-8', $pw ) ),
163 # Since WWW::CSRF::generate_csrf_token does not use the NonBlocking
164 # parameter of Bytes::Random::Secure, we are passing random bytes from
165 # a non blocking source to WWW::CSRF via its Random parameter.
168 return if !$params->{id
} || !$params->{secret
};
171 my $randomizer = Bytes
::Random
::Secure
->new( NonBlocking
=> 1 );
172 # this is most fundamental: do not use /dev/random since it is
173 # blocking, but use /dev/urandom !
174 my $random = $randomizer->bytes( HMAC_SHA1_LENGTH
);
175 my $token = WWW
::CSRF
::generate_csrf_token
(
176 $params->{id
}, $params->{secret
}, { Random
=> $random },
184 return if !$params->{id
} || !$params->{secret
} || !$params->{token
};
186 my $csrf_status = WWW
::CSRF
::check_csrf_token
(
190 { MaxAge
=> $params->{MaxAge
} // ( CSRF_EXPIRY_HOURS
* 3600 ) },
192 return $csrf_status == WWW
::CSRF
::CSRF_OK
();
197 my $length = $params->{length} || 1;
198 $length = 1 unless $length > 0;
200 return String
::Random
::random_string
( '.' x
$length );
205 Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands