Line 0
Link Here
|
|
|
1 |
package Koha::REST::V1::Auth::Password; |
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 |
|
22 |
use C4::Auth qw/checkpw/; |
23 |
use Koha::Patrons; |
24 |
|
25 |
=head1 NAME |
26 |
|
27 |
Koha::REST::V1::Auth::Password - Controller library for handling |
28 |
validation of username and password. |
29 |
|
30 |
Intended use case is authenticating Koha patrons in external |
31 |
applications via Koha's REST API. |
32 |
|
33 |
=head2 Operations |
34 |
|
35 |
=head3 validate |
36 |
|
37 |
Controller method that checks a patron's password |
38 |
|
39 |
=cut |
40 |
|
41 |
sub validate { |
42 |
my $c = shift->openapi->valid_input or return; |
43 |
my $body = $c->validation->param('body'); |
44 |
my $username = $body->{username} // ''; |
45 |
my $patron = Koha::Patrons->find({ userid => $username }); |
46 |
|
47 |
unless ($patron) { |
48 |
return $c->render( status => 400, openapi => { error => "Validation failed" } ); |
49 |
} |
50 |
|
51 |
my $password = $body->{password} // ""; |
52 |
|
53 |
return try { |
54 |
my ($status, $cardnumber, $userid) = C4::Auth::checkpw($patron->userid, $password ); |
55 |
warn $status; |
56 |
warn $cardnumber; |
57 |
warn $userid; |
58 |
unless ( $status ) { |
59 |
return $c->render( |
60 |
status => 400, |
61 |
openapi => { error => "Validation failed" } |
62 |
); |
63 |
} |
64 |
|
65 |
return $c->render( status => 204, openapi => '' ); |
66 |
} |
67 |
catch { |
68 |
if ( blessed $_ and $_->isa('Koha::Exceptions::Password') ) { |
69 |
return $c->render( |
70 |
status => 400, |
71 |
openapi => { error => "$_" } |
72 |
); |
73 |
} |
74 |
|
75 |
$c->unhandled_exception($_); |
76 |
}; |
77 |
} |
78 |
|
79 |
1; |