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

(-)a/Koha/REST/V1/Auth.pm (-2 / +12 lines)
Lines 21-28 use Modern::Perl; Link Here
21
21
22
use Mojo::Base 'Mojolicious::Controller';
22
use Mojo::Base 'Mojolicious::Controller';
23
23
24
use Net::OAuth2::AuthorizationServer;
25
26
use C4::Auth qw( check_cookie_auth get_session haspermission );
24
use C4::Auth qw( check_cookie_auth get_session haspermission );
27
use C4::Context;
25
use C4::Context;
28
26
Lines 39-44 use Koha::Exceptions; Link Here
39
use Koha::Exceptions::Authentication;
37
use Koha::Exceptions::Authentication;
40
use Koha::Exceptions::Authorization;
38
use Koha::Exceptions::Authorization;
41
39
40
use Module::Load::Conditional;
42
use Scalar::Util qw( blessed );
41
use Scalar::Util qw( blessed );
43
use Try::Tiny;
42
use Try::Tiny;
44
43
Lines 118-124 sub authenticate_api_request { Link Here
118
    my $authorization = $spec->{'x-koha-authorization'};
117
    my $authorization = $spec->{'x-koha-authorization'};
119
118
120
    my $authorization_header = $c->req->headers->authorization;
119
    my $authorization_header = $c->req->headers->authorization;
120
121
    if ($authorization_header and $authorization_header =~ /^Bearer /) {
121
    if ($authorization_header and $authorization_header =~ /^Bearer /) {
122
        # attempt to use OAuth2 authentication
123
        if ( ! Module::Load::Conditional::can_load('Net::OAuth2::AuthorizationServer') ) {
124
            Koha::Exceptions::Authorization::Unauthorized->throw(
125
                error => 'Authentication failure.'
126
            );
127
        }
128
        else {
129
            require Net::OAuth2::AuthorizationServer;
130
        }
131
122
        my $server = Net::OAuth2::AuthorizationServer->new;
132
        my $server = Net::OAuth2::AuthorizationServer->new;
123
        my $grant = $server->client_credentials_grant(Koha::OAuth::config);
133
        my $grant = $server->client_credentials_grant(Koha::OAuth::config);
124
        my ($type, $token) = split / /, $authorization_header;
134
        my ($type, $token) = split / /, $authorization_header;
(-)a/Koha/REST/V1/OAuth.pm (-4 / +26 lines)
Lines 1-17 Link Here
1
package Koha::REST::V1::OAuth;
1
package Koha::REST::V1::OAuth;
2
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it under the
6
# terms of the GNU General Public License as published by the Free Software
7
# Foundation; either version 3 of the License, or (at your option) any later
8
# version.
9
#
10
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License along
15
# with Koha; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
3
use Modern::Perl;
18
use Modern::Perl;
4
19
5
use Mojo::Base 'Mojolicious::Controller';
20
use Module::Load::Conditional;
6
21
7
use Net::OAuth2::AuthorizationServer;
22
use C4::Context;
8
use Koha::OAuth;
23
use Koha::OAuth;
9
24
10
use C4::Context;
25
use Mojo::Base 'Mojolicious::Controller';
11
26
12
sub token {
27
sub token {
28
13
    my $c = shift->openapi->valid_input or return;
29
    my $c = shift->openapi->valid_input or return;
14
30
31
    if ( Module::Load::Conditional::can_load('Net::OAuth2::AuthorizationServer') ) {
32
        require Net::OAuth2::AuthorizationServer;
33
    }
34
    else {
35
        return $c->render( status => 400, openapi => { error => 'Unimplemented grant type' } );
36
    }
37
15
    my $grant_type = $c->validation->param('grant_type');
38
    my $grant_type = $c->validation->param('grant_type');
16
    unless ( $grant_type eq 'client_credentials' and C4::Context->preference('RESTOAuth2ClientCredentials') ) {
39
    unless ( $grant_type eq 'client_credentials' and C4::Context->preference('RESTOAuth2ClientCredentials') ) {
17
        return $c->render(status => 400, openapi => {error => 'Unimplemented grant type'});
40
        return $c->render(status => 400, openapi => {error => 'Unimplemented grant type'});
18
- 

Return to bug 20624