Bug 28998: (follow-up) Check missing encryption key in script and module
[koha-ffzg.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 output_and_exit( $cgi, $cookie, $template, 'Entry encryption_key is missing in koha-conf.xml' )
46     if !C4::Context->config('encryption_key');
47
48 my $logged_in_user = Koha::Patrons->find($loggedinuser);
49
50 my $op = $cgi->param('op') // '';
51 my $csrf_pars = {
52     session_id => scalar $cgi->cookie('CGISESSID'),
53     token  => scalar $cgi->param('csrf_token'),
54 };
55
56 if ( $op eq 'register-2FA' ) {
57     output_and_exit( $cgi, $cookie, $template, 'wrong_csrf_token' )
58         unless Koha::Token->new->check_csrf($csrf_pars);
59
60     my $pin_code = $cgi->param('pin_code');
61     my $secret32 = $cgi->param('secret32');
62     my $auth     = Koha::Auth::TwoFactorAuth->new(
63         { patron => $logged_in_user, secret32 => $secret32 } );
64
65     my $verified = $auth->verify(
66         $pin_code,
67         1,    # range
68         $secret32,
69         undef,    # timestamp (defaults to now)
70         30,       # interval (default 30)
71     );
72
73     if ($verified) {
74         # FIXME Generate a (new?) secret
75         $logged_in_user->encode_secret($secret32);
76         $logged_in_user->auth_method('two-factor')->store;
77         $op = 'registered';
78         if( $logged_in_user->notice_email_address ) {
79             $logged_in_user->queue_notice({
80                 letter_params => {
81                     module => 'members', letter_code => '2FA_ENABLE',
82                     branchcode => $logged_in_user->branchcode, lang => $logged_in_user->lang,
83                     tables => { branches => $logged_in_user->branchcode, borrowers => $logged_in_user->id },
84                 },
85                 message_transports => [ 'email' ],
86             });
87         }
88     }
89     else {
90         $template->param( invalid_pin => 1, );
91         $op = 'enable-2FA';
92     }
93 }
94
95 if ( $op eq 'enable-2FA' ) {
96     my $secret = Koha::AuthUtils::generate_salt( 'weak', 16 );
97     my $auth = Koha::Auth::TwoFactorAuth->new(
98         { patron => $logged_in_user, secret => $secret } );
99
100     $template->param(
101         issuer      => $auth->issuer,
102         key_id      => $auth->key_id,
103         qr_code  => $auth->qr_code,
104         secret32    => $auth->secret32,
105             # IMPORTANT: get secret32 after qr_code call !
106     );
107     $auth->clear;
108     $op = 'register';
109 }
110 elsif ( $op eq 'disable-2FA' ) {
111     output_and_exit( $cgi, $cookie, $template, 'wrong_csrf_token' )
112         unless Koha::Token->new->check_csrf($csrf_pars);
113     my $auth = Koha::Auth::TwoFactorAuth->new({ patron => $logged_in_user });
114     $logged_in_user->secret(undef);
115     $logged_in_user->auth_method('password')->store;
116     if( $logged_in_user->notice_email_address ) {
117         $logged_in_user->queue_notice({
118             letter_params => {
119                 module => 'members', letter_code => '2FA_DISABLE',
120                 branchcode => $logged_in_user->branchcode, lang => $logged_in_user->lang,
121                 tables => { branches => $logged_in_user->branchcode, borrowers => $logged_in_user->id },
122             },
123             message_transports => [ 'email' ],
124         });
125     }
126 }
127
128 $template->param(
129     csrf_token => Koha::Token->new->generate_csrf(
130         { session_id => scalar $cgi->cookie('CGISESSID') }
131     ),
132     patron => $logged_in_user,
133     op     => $op,
134 );
135
136 output_html_with_http_headers $cgi, $cookie, $template->output;