Line 0
Link Here
|
|
|
1 |
package Koha::REST::V1::OAuth::Client; |
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::Auth::Client::OAuth; |
21 |
|
22 |
use Mojo::Base 'Mojolicious::Controller'; |
23 |
use Mojo::URL; |
24 |
use Scalar::Util qw(blessed); |
25 |
use Try::Tiny; |
26 |
use Koha::Logger; |
27 |
use URI::Escape qw(uri_escape_utf8); |
28 |
|
29 |
=head1 NAME |
30 |
|
31 |
Koha::REST::V1::OAuth::Client - Controller library for handling OAuth2-related login attempts |
32 |
|
33 |
=head1 API |
34 |
|
35 |
=head2 Methods |
36 |
|
37 |
=head3 login |
38 |
|
39 |
Controller method handling login requests |
40 |
|
41 |
=cut |
42 |
|
43 |
sub login { |
44 |
my $c = shift->openapi->valid_input or return; |
45 |
|
46 |
my $provider = $c->validation->param('provider_code'); |
47 |
my $interface = $c->validation->param('interface'); |
48 |
|
49 |
my $logger = Koha::Logger->get({ interface => 'api' }); |
50 |
|
51 |
my $provider_config = $c->oauth2->providers->{$provider}; |
52 |
|
53 |
unless ( $provider_config ) { |
54 |
return $c->render( |
55 |
status => 404, |
56 |
openapi => { |
57 |
error => 'Object not found', |
58 |
error_code => 'not_found', |
59 |
} |
60 |
); |
61 |
} |
62 |
|
63 |
unless ( $provider_config->{authorize_url} =~ /response_type=code/ ) { |
64 |
my $authorize_url = Mojo::URL->new($provider_config->{authorize_url}); |
65 |
$authorize_url->query->append(response_type => 'code'); |
66 |
$provider_config->{authorize_url} = $authorize_url->to_string; |
67 |
} |
68 |
|
69 |
my $uri; |
70 |
|
71 |
if ( $interface eq 'opac' ) { |
72 |
if ( C4::Context->preference('OpacPublic') ) { |
73 |
$uri = '/cgi-bin/koha/opac-user.pl'; |
74 |
} else { |
75 |
$uri = '/cgi-bin/koha/opac-main.pl'; |
76 |
} |
77 |
} else { |
78 |
$uri = '/cgi-bin/koha/mainpage.pl'; |
79 |
} |
80 |
|
81 |
return $c->oauth2->get_token_p($provider)->then( |
82 |
sub { |
83 |
return unless my $response = shift; |
84 |
|
85 |
my ( $patron, $mapped_data, $domain ) = Koha::Auth::Client::OAuth->new->get_user( |
86 |
{ provider => $provider, |
87 |
data => $response, |
88 |
interface => $interface, |
89 |
config => $c->oauth2->providers->{$provider} |
90 |
} |
91 |
); |
92 |
|
93 |
try { |
94 |
# FIXME: We could check if the backend allows registering |
95 |
if ( !$patron ) { |
96 |
$patron = $c->auth->register( |
97 |
{ |
98 |
data => $mapped_data, |
99 |
domain => $domain, |
100 |
interface => $interface |
101 |
} |
102 |
); |
103 |
} |
104 |
|
105 |
my ( $status, $cookie, $session_id ) = $c->auth->session($patron); |
106 |
|
107 |
$c->cookie( CGISESSID => $session_id, { path => "/" } ); |
108 |
|
109 |
$c->redirect_to($uri); |
110 |
} catch { |
111 |
my $error = $_; |
112 |
$logger->error($error); |
113 |
# TODO: Review behavior |
114 |
if ( blessed $error ) { |
115 |
if ( $error->isa('Koha::Exceptions::Auth::Unauthorized') ) { |
116 |
$error = "$error"; |
117 |
} |
118 |
} |
119 |
|
120 |
$error = uri_escape_utf8($error); |
121 |
|
122 |
$c->redirect_to($uri."?auth_error=$error"); |
123 |
}; |
124 |
} |
125 |
)->catch( |
126 |
sub { |
127 |
my $error = shift; |
128 |
$logger->error($error); |
129 |
$error = uri_escape_utf8($error); |
130 |
$c->redirect_to($uri."?auth_error=$error"); |
131 |
} |
132 |
)->wait; |
133 |
} |
134 |
|
135 |
1; |