3 # This file is part of Koha.
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.
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.
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>.
22 use C4::Auth qw( get_template_and_user );
23 use C4::Output qw( output_and_exit output_html_with_http_headers );
26 use Koha::Auth::TwoFactorAuth;
30 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
32 template_name => 'members/two_factor_auth.tt',
35 flagsrequired => { editcatalogue => '*' },
39 unless ( C4::Context->preference('TwoFactorAuthentication') ) {
40 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
44 my $logged_in_user = Koha::Patrons->find($loggedinuser);
46 my $op = $cgi->param('op') // '';
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 } );
54 my $verified = $auth->verify(
58 undef, # timestamp (defaults to now)
59 30, # interval (default 30)
63 $logged_in_user->secret($secret32);
66 # FIXME Generate a (new?) secret
67 $logged_in_user->auth_method('two-factor')->store;
70 $template->param( invalid_pin => 1, );
75 if ( $op eq 'enable-2FA' ) {
77 my $secret = Koha::AuthUtils::generate_salt( 'weak', 16 );
78 my $auth = Koha::Auth::TwoFactorAuth->new(
79 { patron => $logged_in_user, secret => $secret } );
81 my $secret32 = $auth->generate_secret32;
83 $auth->qr_code( $secret32, $auth->key_id, $auth->issuer, );
86 issuer => $auth->issuer,
87 key_id => $auth->key_id,
88 secret32 => $secret32,
89 qr_code_url => $qr_code_url,
94 elsif ( $op eq 'disable-2FA' ) {
95 $logged_in_user->auth_method('password')->store;
99 csrf_token => Koha::Token->new->generate_csrf(
100 { session_id => scalar $cgi->cookie('CGISESSID') }
102 patron => $logged_in_user,
106 output_html_with_http_headers $cgi, $cookie, $template->output;