Line 0
Link Here
|
0 |
- |
1 |
package Koha::REST::Plugin::Auth; |
|
|
2 |
|
3 |
# Copyright Theke Solutions 2022 |
4 |
# |
5 |
# This file is part of Koha. |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
|
22 |
use Mojo::Base 'Mojolicious::Plugin'; |
23 |
|
24 |
use Koha::Exceptions; |
25 |
use Koha::Exceptions::Auth; |
26 |
use Koha::Patron; |
27 |
|
28 |
use C4::Auth; |
29 |
|
30 |
use CGI; |
31 |
|
32 |
=head1 NAME |
33 |
|
34 |
Koha::REST::Plugin::Auth |
35 |
|
36 |
=head1 API |
37 |
|
38 |
=head2 Helper methods |
39 |
|
40 |
=cut |
41 |
|
42 |
sub register { |
43 |
my ( $self, $app ) = @_; |
44 |
|
45 |
=head3 auth.register |
46 |
|
47 |
my $patron = $c->auth->register( |
48 |
{ data => $patron_data, |
49 |
domain => $domain, |
50 |
interface => $interface |
51 |
} |
52 |
); |
53 |
|
54 |
If no patron passed, creates a new I<Koha::Patron> if the provider is configured |
55 |
to do so for the domain. |
56 |
|
57 |
=cut |
58 |
|
59 |
$app->helper( |
60 |
'auth.register' => sub { |
61 |
my ( $c, $params ) = @_; |
62 |
my $data = $params->{data}; |
63 |
my $domain = $params->{domain}; |
64 |
my $interface = $params->{interface}; |
65 |
|
66 |
unless ( $interface eq 'opac' && $domain->auto_register ) { |
67 |
Koha::Exceptions::Auth::Unauthorized->throw( code => 401 ); |
68 |
} |
69 |
|
70 |
return Koha::Patron->new($data)->store; |
71 |
} |
72 |
); |
73 |
|
74 |
=head3 auth.session |
75 |
|
76 |
my ( $status, $cookie, $session_id ) = $c->auth->session( $patron ); |
77 |
|
78 |
Generates a new session. |
79 |
|
80 |
=cut |
81 |
|
82 |
$app->helper( |
83 |
'auth.session' => sub { |
84 |
my ( $c, $patron ) = @_; |
85 |
my $userid = $patron->userid; |
86 |
my $cardnumber = $patron->cardnumber; |
87 |
my $cgi = CGI->new; |
88 |
|
89 |
$cgi->param( userid => $userid ); |
90 |
$cgi->param( cardnumber => $cardnumber ); |
91 |
$cgi->param( auth_client_login => 1 ); |
92 |
|
93 |
my ( $status, $cookie, $session_id ) = C4::Auth::check_api_auth($cgi); |
94 |
|
95 |
Koha::Exceptions::UnderMaintenance->throw( code => 503 ) |
96 |
if $status eq "maintenance"; |
97 |
|
98 |
Koha::Exceptions::Auth::CannotCreateSession->throw( code => 500 ) |
99 |
unless $status eq "ok"; |
100 |
|
101 |
return ( $status, $cookie, $session_id ); |
102 |
} |
103 |
); |
104 |
} |
105 |
|
106 |
1; |