Bug 32030: Add/remove packages to/from EBSCO's holdings
[koha-ffzg.git] / Koha / REST / V1 / TwoFactorAuth.pm
1 package Koha::REST::V1::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
20 use Mojo::Base 'Mojolicious::Controller';
21 use Try::Tiny;
22
23 use C4::Letters qw( GetPreparedLetter );
24
25 =head1 NAME
26
27 Koha::REST::V1::TwoFactorAuth
28
29 =head1 API
30
31 =head2 Methods
32
33 =head3 send_otp_token
34
35 Will send an email with the OTP token needed to complete the second authentication step.
36
37 =cut
38
39
40 sub send_otp_token {
41
42     my $c = shift->openapi->valid_input or return;
43
44     my $patron = Koha::Patrons->find( $c->stash('koha.user')->borrowernumber );
45
46     return try {
47
48         my $code = Koha::Auth::TwoFactorAuth->new({patron => $patron})->code;
49         my $letter = C4::Letters::GetPreparedLetter(
50             module      => 'members',
51             letter_code => '2FA_OTP_TOKEN',
52             branchcode  => $patron->branchcode,
53             substitute  => { otp_token => $code },
54             tables      => {
55                 borrowers => $patron->unblessed,
56             }
57         );
58         my $message_id = C4::Letters::EnqueueLetter(
59             {
60                 letter                 => $letter,
61                 borrowernumber         => $patron->borrowernumber,
62                 message_transport_type => 'email'
63             }
64         );
65         C4::Letters::SendQueuedMessages({message_id => $message_id});
66
67         my $message = C4::Letters::GetMessage($message_id);
68
69         if ( $message->{status} eq 'sent' ) {
70             return $c->render(status => 200, openapi => {});
71         } elsif ( $message->{status} eq 'failed' ) {
72             return $c->render(status => 400, openapi => { error => 'email_not_sent'});
73         }
74     }
75     catch {
76         $c->unhandled_exception($_);
77     };
78
79 }
80
81 =head3 registration
82
83 Ask for a registration secret. It will return a QR code image and a secret32.
84
85 The secret must be sent back to the server with the pin code for the verification step.
86
87 =cut
88
89 sub registration {
90
91     my $c = shift->openapi->valid_input or return;
92
93     my $patron = Koha::Patrons->find( $c->stash('koha.user')->borrowernumber );
94
95     return try {
96         my $secret = Koha::AuthUtils::generate_salt( 'weak', 16 );
97         my $auth   = Koha::Auth::TwoFactorAuth->new(
98             { patron => $patron, secret => $secret } );
99
100         my $response = {
101             issuer   => $auth->issuer,
102             key_id   => $auth->key_id,
103             qr_code  => $auth->qr_code,
104             secret32 => $auth->secret32,
105
106             # IMPORTANT: get secret32 after qr_code call !
107         };
108         $auth->clear;
109
110         return $c->render(status => 201, openapi => $response);
111     }
112     catch {
113         $c->unhandled_exception($_);
114     };
115
116 }
117
118 =head3 verification
119
120 Verify the registration, get the pin code and the secret retrieved from the registration.
121
122 The 2FA_ENABLE notice will be generated if the pin code is correct, and the patron will have their two-factor authentication setup completed.
123
124 =cut
125
126 sub verification {
127
128     my $c = shift->openapi->valid_input or return;
129
130     my $patron = Koha::Patrons->find( $c->stash('koha.user')->borrowernumber );
131
132     return try {
133
134         my $pin_code = $c->validation->param('pin_code');
135         my $secret32 = $c->validation->param('secret32');
136
137         my $auth     = Koha::Auth::TwoFactorAuth->new(
138             { patron => $patron, secret32 => $secret32 } );
139
140         my $verified = $auth->verify(
141             $pin_code,
142             1,        # range
143             $secret32,
144             undef,    # timestamp (defaults to now)
145             30,       # interval (default 30)
146         );
147
148         unless ($verified) {
149             return $c->render(
150                 status  => 400,
151                 openapi => { error => "Invalid pin" }
152             );
153         }
154
155         # FIXME Generate a (new?) secret
156         $patron->encode_secret($secret32);
157         $patron->auth_method('two-factor')->store;
158         if ( $patron->notice_email_address ) {
159             $patron->queue_notice(
160                 {
161                     letter_params => {
162                         module      => 'members',
163                         letter_code => '2FA_ENABLE',
164                         branchcode  => $patron->branchcode,
165                         lang        => $patron->lang,
166                         tables      => {
167                             branches  => $patron->branchcode,
168                             borrowers => $patron->id
169                         },
170                     },
171                     message_transports => ['email'],
172                 }
173             );
174         }
175
176         return $c->render(status => 204, openapi => {});
177     }
178     catch {
179         $c->unhandled_exception($_);
180     };
181
182 }
183
184 1;