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;
31 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
33 template_name => 'members/two_factor_auth.tt',
36 flagsrequired => { editcatalogue => '*' },
40 unless ( C4::Context->preference('TwoFactorAuthentication') ) {
41 print $cgi->redirect("/cgi-bin/koha/errors/404.pl");
45 my $logged_in_user = Koha::Patrons->find($loggedinuser);
46 my $op = $cgi->param('op') // '';
48 if ( !C4::Context->config('encryption_key') ) {
49 $template->param( missing_key => 1 );
54 session_id => scalar $cgi->cookie('CGISESSID'),
55 token => scalar $cgi->param('csrf_token'),
58 if ( $op eq 'register-2FA' ) {
59 output_and_exit( $cgi, $cookie, $template, 'wrong_csrf_token' )
60 unless Koha::Token->new->check_csrf($csrf_pars);
62 my $pin_code = $cgi->param('pin_code');
63 my $secret32 = $cgi->param('secret32');
64 my $auth = Koha::Auth::TwoFactorAuth->new(
65 { patron => $logged_in_user, secret32 => $secret32 } );
67 my $verified = $auth->verify(
71 undef, # timestamp (defaults to now)
72 30, # interval (default 30)
77 # FIXME Generate a (new?) secret
78 $logged_in_user->encode_secret($secret32);
79 $logged_in_user->auth_method('two-factor')->store;
81 if ( $logged_in_user->notice_email_address ) {
82 $logged_in_user->queue_notice(
86 letter_code => '2FA_ENABLE',
87 branchcode => $logged_in_user->branchcode,
88 lang => $logged_in_user->lang,
90 branches => $logged_in_user->branchcode,
91 borrowers => $logged_in_user->id
94 message_transports => ['email'],
100 $template->param( invalid_pin => 1, );
105 if ( $op eq 'enable-2FA' ) {
106 my $secret = Koha::AuthUtils::generate_salt( 'weak', 16 );
107 my $auth = Koha::Auth::TwoFactorAuth->new(
108 { patron => $logged_in_user, secret => $secret } );
111 issuer => $auth->issuer,
112 key_id => $auth->key_id,
113 qr_code => $auth->qr_code,
114 secret32 => $auth->secret32,
116 # IMPORTANT: get secret32 after qr_code call !
121 elsif ( $op eq 'disable-2FA' ) {
122 output_and_exit( $cgi, $cookie, $template, 'wrong_csrf_token' )
123 unless Koha::Token->new->check_csrf($csrf_pars);
125 Koha::Auth::TwoFactorAuth->new( { patron => $logged_in_user } );
126 $logged_in_user->secret(undef);
127 $logged_in_user->auth_method('password')->store;
128 if ( $logged_in_user->notice_email_address ) {
129 $logged_in_user->queue_notice(
133 letter_code => '2FA_DISABLE',
134 branchcode => $logged_in_user->branchcode,
135 lang => $logged_in_user->lang,
137 branches => $logged_in_user->branchcode,
138 borrowers => $logged_in_user->id
141 message_transports => ['email'],
149 csrf_token => Koha::Token->new->generate_csrf(
150 { session_id => scalar $cgi->cookie('CGISESSID') }
152 patron => $logged_in_user,
156 output_html_with_http_headers $cgi, $cookie, $template->output;