View | Details | Raw Unified | Return to bug 31378
Collapse All | Expand All

(-)a/Koha/Exceptions/Auth.pm (+41 lines)
Line 0 Link Here
1
package Koha::Exceptions::Auth;
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 Koha::Exception;
21
22
use Exception::Class (
23
    'Koha::Exceptions::Auth' => {
24
        isa    => 'Koha::Exception',
25
        fields => ['code']
26
    },
27
    'Koha::Exceptions::Auth::NoValidDomain' => {
28
        isa         => 'Koha::Exceptions::Auth',
29
        description => 'No valid domain found'
30
    },
31
    'Koha::Exceptions::Auth::Unauthorized' => {
32
        isa         => 'Koha::Exceptions::Auth',
33
        description => 'External auth user cannot access resource'
34
    },
35
    'Koha::Exceptions::Auth::CannotCreateSession' => {
36
        isa         => 'Koha::Exceptions::Auth',
37
        description => 'Cannot create session'
38
    },
39
);
40
41
1;
(-)a/Koha/REST/Plugin/Auth.pm (-1 / +106 lines)
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;

Return to bug 31378