Bug 28786: Two-factor authentication for staff client - TOTP
[koha.git] / Koha / Auth / TwoFactorAuth.pm
1 package Koha::Auth::TwoFactorAuth;
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 use Auth::GoogleAuth;
20
21 use base qw( Auth::GoogleAuth );
22
23 =head1 NAME
24
25 Koha::Auth::TwoFactorAuth- Koha class deal with Two factor authentication
26
27 =head1 SYNOPSIS
28
29 use Koha::Auth::TwoFactorAuth;
30
31 my $secret = Koha::AuthUtils::generate_salt( 'weak', 16 );
32 my $auth = Koha::Auth::TwoFactorAuth->new(
33     { patron => $patron, secret => $secret } );
34 my $secret32 = $auth->generate_secret32;
35 my $ok = $auth->verify($pin_code, 1, $secret32);
36
37 It's based on Auth::GoogleAuth
38
39 =cut
40
41 sub get_auth {
42     my ($params) = @_;
43     my $patron   = $params->{patron};
44     my $secret   = $params->{secret};
45     my $secret32 = $params->{secret32};
46
47     if (!$secret && !$secret32){
48         $secret32 = $patron->secret;
49     }
50
51     my $issuer = $patron->library->branchname;
52     my $key_id = sprintf "%s_%s",
53       $issuer, ( $patron->email || $patron->userid );
54
55     return Auth::GoogleAuth->new(
56         {
57             ( $secret   ? ( secret   => $secret )   : () ),
58             ( $secret32 ? ( secret32 => $secret32 ) : () ),
59             issuer => $issuer,
60             key_id => $key_id,
61         }
62     );
63 }
64
65 1;