b9c785fa0e7d0bce526fa61773c2963424a8f512
[srvgit] / 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 GD::Barcode;
20 use MIME::Base64 qw( encode_base64 );
21
22 use C4::Letters;
23 use Koha::Exceptions;
24 use Koha::Exceptions::Patron;
25
26 use base qw( Auth::GoogleAuth );
27
28 =head1 NAME
29
30 Koha::Auth::TwoFactorAuth- Koha class deal with Two factor authentication
31
32 =head1 SYNOPSIS
33
34 use Koha::Auth::TwoFactorAuth;
35
36 my $secret = Koha::AuthUtils::generate_salt( 'weak', 16 );
37 my $auth = Koha::Auth::TwoFactorAuth->new({ patron => $patron, secret => $secret });
38 my $image_src = $auth->qr_code;
39 my $ok = $auth->verify( $pin_code, 1 );
40
41 It's based on Auth::GoogleAuth
42
43 =head2 METHODS
44
45 =head3 new
46
47     $obj = Koha::Auth::TwoFactorAuth->new({ patron => $p, secret => $s });
48
49     Patron is mandatory.
50     Secret is optional, defaults to patron's secret.
51     Passing secret32 overrules secret! Secret32 should be base32.
52
53 =cut
54
55 sub new {
56     my ($class, $params) = @_;
57     my $patron   = $params->{patron};
58     my $secret32 = $params->{secret32};
59     my $secret = $params->{secret};
60
61     # FIXME Raise an exception if the syspref is disabled
62
63     Koha::Exceptions::MissingParameter->throw("Mandatory patron parameter missing")
64         unless $patron && ref($patron) eq 'Koha::Patron';
65
66     my $type = 'secret32';
67     if( $secret32 ) {
68         Koha::Exceptions::BadParameter->throw("Secret32 should be base32")
69             if $secret32 =~ /[^a-z2-7]/;
70     } elsif( $secret ) {
71         $type = 'secret';
72     } elsif( $patron->secret ) {
73         $secret32 = $patron->decoded_secret; # saved already in base32
74     } else {
75         Koha::Exceptions::MissingParameter->throw("No secret passed or patron has no secret");
76     }
77
78     my $issuer = $patron->library->branchname;
79     my $key_id = sprintf "%s_%s",
80       $issuer, ( $patron->email || $patron->userid );
81
82     return $class->SUPER::new({
83         $type => $secret32 || $secret,
84         issuer => $issuer,
85         key_id => $key_id,
86     });
87 }
88
89 =head3 qr_code
90
91     my $image_src = $auth->qr_code;
92
93     Replacement for (unsafer) Auth::GoogleAuth::qr_code.
94     Returns the data URL to fill the src attribute of the
95     image tag on the registration form.
96
97 =cut
98
99 sub qr_code {
100     my ( $self ) = @_;
101
102     my $otpauth = $self->SUPER::qr_code( undef, undef, undef, 1);
103         # no need to pass secret, key and issuer again
104     my $qrcode = GD::Barcode->new( 'QRcode', $otpauth, { Ecc => 'M', Version => 8, ModuleSize => 4 } );
105     my $data = $qrcode->plot->png;
106     return "data:image/png;base64,". encode_base64( $data, q{} ); # does not contain newlines
107 }
108
109 1;