Bug 28786: Correctly inherit from Auth::GoogleAuth
[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;
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
28 my $cgi = CGI->new;
29
30 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
31     {
32         template_name => 'members/two_factor_auth.tt',
33         query         => $cgi,
34         type          => 'intranet',
35         flagsrequired => { editcatalogue => '*' },
36     }
37 );
38
39 unless ( C4::Context->preference('TwoFactorAuthentication') ) {
40     print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
41     exit;
42 }
43
44 my $logged_in_user = Koha::Patrons->find($loggedinuser);
45
46 my $op = $cgi->param('op') // '';
47
48 if ( $op eq 'register-2FA' ) {
49     my $pin_code = $cgi->param('pin_code');
50     my $secret32 = $cgi->param('secret32');
51     my $auth     = Koha::Auth::TwoFactorAuth->new(
52         { patron => $logged_in_user, secret32 => $secret32 } );
53
54     my $verified = $auth->verify(
55         $pin_code,
56         1,    # range
57         $secret32,
58         undef,    # timestamp (defaults to now)
59         30,       # interval (default 30)
60     );
61
62     if ($verified) {
63         $logged_in_user->secret($secret32);
64         $op = 'registered';
65
66         # FIXME Generate a (new?) secret
67         $logged_in_user->auth_method('two-factor')->store;
68     }
69     else {
70         $template->param( invalid_pin => 1, );
71         $op = 'enable-2FA';
72     }
73 }
74
75 if ( $op eq 'enable-2FA' ) {
76
77     my $secret = Koha::AuthUtils::generate_salt( 'weak', 16 );
78     my $auth = Koha::Auth::TwoFactorAuth->new(
79         { patron => $logged_in_user, secret => $secret } );
80
81     my $secret32 = $auth->generate_secret32;
82     my $qr_code_url =
83       $auth->qr_code( $secret32, $auth->key_id, $auth->issuer, );
84
85     $template->param(
86         issuer      => $auth->issuer,
87         key_id      => $auth->key_id,
88         secret32    => $secret32,
89         qr_code_url => $qr_code_url,
90     );
91     $auth->clear;
92     $op = 'register';
93 }
94 elsif ( $op eq 'disable-2FA' ) {
95     $logged_in_user->auth_method('password')->store;
96 }
97
98 $template->param(
99     csrf_token => Koha::Token->new->generate_csrf(
100         { session_id => scalar $cgi->cookie('CGISESSID') }
101     ),
102     patron => $logged_in_user,
103     op     => $op,
104 );
105
106 output_html_with_http_headers $cgi, $cookie, $template->output;