Bug 14868: (QA followup) POD fixes
[koha.git] / Koha / Token.pm
1 package Koha::Token;
2
3 # Created as wrapper for CSRF tokens, but designed for more general use
4
5 # Copyright 2016 Rijksmuseum
6 #
7 # This file is part of Koha.
8 #
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
12 # version.
13 #
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.
17 #
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.
21
22 =head1 NAME
23
24 Koha::Token - Tokenizer
25
26 =head1 SYNOPSIS
27
28     use Koha::Token;
29     my $tokenizer = Koha::Token->new;
30     my $token = $tokenizer->generate({ length => 20 });
31
32     # safely generate a CSRF token (nonblocking)
33     my $csrf_token = $tokenizer->generate({
34         type => 'CSRF', id => $id, secret => $secret,
35     });
36
37     # or check a CSRF token
38     my $result = $tokenizer->check_csrf({
39         id => $id, secret => $secret, token => $token,
40     });
41
42 =head1 DESCRIPTION
43
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.
47
48 =cut
49
50 use Modern::Perl;
51 use Bytes::Random::Secure ();
52 use String::Random ();
53 use WWW::CSRF ();
54 use base qw(Class::Accessor);
55 use constant HMAC_SHA1_LENGTH => 20;
56
57 =head1 METHODS
58
59 =head2 new
60
61     Create object (via Class::Accessor).
62
63 =cut
64
65 sub new {
66     my ( $class ) = @_;
67     return $class->SUPER::new();
68 }
69
70 =head2 generate
71
72     my $token = $tokenizer->generate({ length => 20 });
73     my $csrf_token = $tokenizer->generate({
74         type => 'CSRF', id => $id, secret => $secret,
75     });
76
77     Generate several types of tokens. Now includes CSRF.
78     Room for future extension.
79
80 =cut
81
82 sub generate {
83     my ( $self, $params ) = @_;
84     if( $params->{type} && $params->{type} eq 'CSRF' ) {
85         $self->{lasttoken} = _gen_csrf( $params );
86     } else {
87         $self->{lasttoken} = _gen_rand( $params );
88     }
89     return $self->{lasttoken};
90 }
91
92 =head2 generate_csrf
93
94     Shortcut for: generate({ type => 'CSRF', ... })
95
96 =cut
97
98 sub generate_csrf {
99     my ( $self, $params ) = @_;
100     return $self->generate({ %$params, type => 'CSRF' });
101 }
102
103 =head2 check
104
105     my $result = $tokenizer->check({
106         type => 'CSRF', id => $id, secret => $secret, token => $token,
107     });
108
109     Check several types of tokens. Now includes CSRF.
110     Room for future extension.
111
112 =cut
113
114 sub check {
115     my ( $self, $params ) = @_;
116     if( $params->{type} && $params->{type} eq 'CSRF' ) {
117         return _chk_csrf( $params );
118     }
119     return;
120 }
121
122 =head2 check_csrf
123
124     Shortcut for: check({ type => 'CSRF', ... })
125
126 =cut
127
128 sub check_csrf {
129     my ( $self, $params ) = @_;
130     return $self->check({ %$params, type => 'CSRF' });
131 }
132
133 # --- Internal routines ---
134
135 sub _gen_csrf {
136
137 # Since WWW::CSRF::generate_csrf_token does not use the NonBlocking
138 # parameter of Bytes::Random::Secure, we are passing random bytes from
139 # a non blocking source to WWW::CSRF via its Random parameter.
140
141     my ( $params ) = @_;
142     return if !$params->{id} || !$params->{secret};
143
144
145     my $randomizer = Bytes::Random::Secure->new( NonBlocking => 1 );
146         # this is most fundamental: do not use /dev/random since it is
147         # blocking, but use /dev/urandom !
148     my $random = $randomizer->bytes( HMAC_SHA1_LENGTH );
149     my $token = WWW::CSRF::generate_csrf_token(
150         $params->{id}, $params->{secret}, { Random => $random },
151     );
152
153     return $token;
154 }
155
156 sub _chk_csrf {
157     my ( $params ) = @_;
158     return if !$params->{id} || !$params->{secret} || !$params->{token};
159
160     my $csrf_status = WWW::CSRF::check_csrf_token(
161         $params->{id},
162         $params->{secret},
163         $params->{token},
164     );
165     return $csrf_status == WWW::CSRF::CSRF_OK();
166 }
167
168 sub _gen_rand {
169     my ( $params ) = @_;
170     my $length = $params->{length} || 1;
171     $length = 1 unless $length > 0;
172
173     return String::Random::random_string( '.' x $length );
174 }
175
176 =head1 AUTHOR
177
178     Marcel de Rooy, Rijksmuseum Amsterdam, The Netherlands
179
180 =cut
181
182 1;