Bug 14242: (QA follow-up) Update hint on suggestion form: 2016 > 2022
[koha.git] / members / two_factor_auth.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use CGI qw(-utf8);
21
22 use C4::Auth qw( get_template_and_user );
23 use C4::Output qw( output_and_exit output_html_with_http_headers );
24
25 use Koha::Patrons;
26 use Koha::Auth::TwoFactorAuth;
27 use Koha::Token;
28
29 my $cgi = CGI->new;
30
31 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
32     {
33         template_name => 'members/two_factor_auth.tt',
34         query         => $cgi,
35         type          => 'intranet',
36         flagsrequired => { editcatalogue => '*' },
37     }
38 );
39
40 unless ( C4::Context->preference('TwoFactorAuthentication') ) {
41     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
42     exit;
43 }
44
45 my $logged_in_user = Koha::Patrons->find($loggedinuser);
46
47 my $op = $cgi->param('op') // '';
48 my $csrf_pars = {
49     session_id => scalar $cgi->cookie('CGISESSID'),
50     token  => scalar $cgi->param('csrf_token'),
51 };
52
53 if ( $op eq 'register-2FA' ) {
54     output_and_exit( $cgi, $cookie, $template, 'wrong_csrf_token' )
55         unless Koha::Token->new->check_csrf($csrf_pars);
56
57     my $pin_code = $cgi->param('pin_code');
58     my $secret32 = $cgi->param('secret32');
59     my $auth     = Koha::Auth::TwoFactorAuth->new(
60         { patron => $logged_in_user, secret32 => $secret32 } );
61
62     my $verified = $auth->verify(
63         $pin_code,
64         1,    # range
65         $secret32,
66         undef,    # timestamp (defaults to now)
67         30,       # interval (default 30)
68     );
69
70     if ($verified) {
71         # FIXME Generate a (new?) secret
72         $logged_in_user->secret($secret32);
73         $logged_in_user->auth_method('two-factor')->store;
74         $op = 'registered';
75         if( $logged_in_user->notice_email_address ) {
76             $logged_in_user->queue_notice({
77                 letter_params => {
78                     module => 'members', letter_code => '2FA_ENABLE',
79                     branchcode => $logged_in_user->branchcode, lang => $logged_in_user->lang,
80                     tables => { branches => $logged_in_user->branchcode, borrowers => $logged_in_user->id },
81                 },
82                 message_transports => [ 'email' ],
83             });
84         }
85     }
86     else {
87         $template->param( invalid_pin => 1, );
88         $op = 'enable-2FA';
89     }
90 }
91
92 if ( $op eq 'enable-2FA' ) {
93     my $secret = Koha::AuthUtils::generate_salt( 'weak', 16 );
94     my $auth = Koha::Auth::TwoFactorAuth->new(
95         { patron => $logged_in_user, secret => $secret } );
96
97     $template->param(
98         issuer      => $auth->issuer,
99         key_id      => $auth->key_id,
100         qr_code  => $auth->qr_code,
101         secret32    => $auth->secret32,
102             # IMPORTANT: get secret32 after qr_code call !
103     );
104     $auth->clear;
105     $op = 'register';
106 }
107 elsif ( $op eq 'disable-2FA' ) {
108     output_and_exit( $cgi, $cookie, $template, 'wrong_csrf_token' )
109         unless Koha::Token->new->check_csrf($csrf_pars);
110     my $auth = Koha::Auth::TwoFactorAuth->new({ patron => $logged_in_user });
111     $logged_in_user->secret(undef);
112     $logged_in_user->auth_method('password')->store;
113     if( $logged_in_user->notice_email_address ) {
114         $logged_in_user->queue_notice({
115             letter_params => {
116                 module => 'members', letter_code => '2FA_DISABLE',
117                 branchcode => $logged_in_user->branchcode, lang => $logged_in_user->lang,
118                 tables => { branches => $logged_in_user->branchcode, borrowers => $logged_in_user->id },
119             },
120             message_transports => [ 'email' ],
121         });
122     }
123 }
124
125 $template->param(
126     csrf_token => Koha::Token->new->generate_csrf(
127         { session_id => scalar $cgi->cookie('CGISESSID') }
128     ),
129     patron => $logged_in_user,
130     op     => $op,
131 );
132
133 output_html_with_http_headers $cgi, $cookie, $template->output;