Bug 33482: Propagate errors from EBSCO's ws to the UI
[koha.git] / Koha / ExternalContent / OverDrive.pm
1 # Copyright 2014 Catalyst
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 package Koha::ExternalContent::OverDrive;
19
20 use Modern::Perl;
21 use Carp qw( croak );
22
23 use base qw(Koha::ExternalContent);
24 use WebService::ILS::OverDrive::Patron;
25 use C4::Context;
26 use LWP::UserAgent;
27
28 =head1 NAME
29
30 Koha::ExternalContent::OverDrive
31
32 =head1 SYNOPSIS
33
34     Register return url with OverDrive:
35       base app url + /cgi-bin/koha/external/overdrive/auth.pl
36
37     use Koha::ExternalContent::OverDrive;
38     my $od_client = Koha::ExternalContent::OverDrive->new();
39     my $od_auth_url = $od_client->auth_url($return_page_url);
40
41 =head1 DESCRIPTION
42
43 A (very) thin wrapper around C<WebService::ILS::OverDrive::Patron>
44
45 Takes "OverDrive*" Koha preferences
46
47 =cut
48
49 sub new {
50     my $class  = shift;
51     my $params = shift || {};
52     $params->{koha_session_id} or croak "No koha_session_id";
53
54     my $self = $class->SUPER::new($params);
55     unless ($params->{client}) {
56         my $client_key     = C4::Context->preference('OverDriveClientKey')
57           or croak("OverDriveClientKey pref not set");
58         my $client_secret  = C4::Context->preference('OverDriveClientSecret')
59           or croak("OverDriveClientSecret pref not set");
60         my $library_id     = C4::Context->preference('OverDriveLibraryID')
61           or croak("OverDriveLibraryID pref not set");
62         my ($token, $token_type) = $self->get_token_from_koha_session();
63         $self->client( WebService::ILS::OverDrive::Patron->new(
64             client_id         => $client_key,
65             client_secret     => $client_secret,
66             library_id        => $library_id,
67             access_token      => $token,
68             access_token_type => $token_type,
69             user_agent_params => { agent => $class->agent_string }
70         ) );
71     }
72
73     return $self;
74 }
75
76 =head1 L<WebService::ILS::OverDrive::Patron> METHODS
77
78 Methods used without mods:
79
80 =over 4
81
82 =item C<error_message()>
83
84 =item C<patron()>
85
86 =item C<checkouts()>
87
88 =item C<holds()>
89
90 =item C<checkout($id, $format)>
91
92 =item C<checkout_download_url($id)>
93
94 =item C<return($id)>
95
96 =item C<place_hold($id)>
97
98 =item C<remove_hold($id)>
99
100 =back
101
102 Methods with slightly moded interfaces:
103
104 =head2 auth_url($page_url)
105
106   Input: url of the page from which OverDrive authentication was requested
107
108   Returns: Post OverDrive auth return handler url (see SYNOPSIS)
109
110 =cut
111
112 sub auth_url {
113     my $self = shift;
114     my $page_url = shift or croak "Page url not provided";
115
116     my ($return_url, $page) = $self->_return_url($page_url);
117     $self->set_return_page_in_koha_session($page);
118     return $self->client->auth_url($return_url);
119 }
120
121 =head2 auth_by_code($code, $base_url)
122
123   To be called in external/overdrive/auth.pl upon return from OverDrive Granted auth
124
125 =cut
126
127 sub auth_by_code {
128     my $self = shift;
129     my $code = shift or croak "OverDrive auth code not provided";
130     my $base_url = shift or croak "App base url not provided";
131
132     my ($access_token, $access_token_type, $auth_token)
133       = $self->client->auth_by_code($code, $self->_return_url($base_url));
134     $access_token or die "Invalid OverDrive code returned";
135     $self->set_token_in_koha_session($access_token, $access_token_type);
136
137     if (my $koha_patron = $self->koha_patron) {
138         $koha_patron->set({overdrive_auth_token => $auth_token})->store;
139     }
140     return $self->get_return_page_from_koha_session;
141 }
142
143 =head2 auth_by_userid($userid, $password, $website_id, $authorization_name)
144
145   To be called to check auth of patron using OverDrive Patron Authentication method
146   This requires a SIP connection configured with OverDrive
147
148 =cut
149
150 sub auth_by_userid {
151     my $self = shift;
152     my $userid = shift or croak "No user provided";
153     my $password = shift;
154     croak "No password provided" unless ($password || !C4::Context->preference("OverDrivePasswordRequired"));
155     my $website_id = shift or croak "OverDrive Library ID not provided";
156     my $authorization_name = shift or croak "OverDrive Authname not provided";
157
158     my ($access_token, $access_token_type, $auth_token)
159       = $self->client->auth_by_user_id($userid, $password, $website_id, $authorization_name);
160     $access_token or die "Invalid OverDrive code returned";
161     $self->set_token_in_koha_session($access_token, $access_token_type);
162
163     $self->koha_patron->set({overdrive_auth_token => $auth_token})->store;
164     return $self->get_return_page_from_koha_session;
165 }
166
167 use constant AUTH_RETURN_HANDLER => "/cgi-bin/koha/external/overdrive/auth.pl";
168 sub _return_url {
169     my $self = shift;
170     my $page_url = shift or croak "Page url not provided";
171
172     my ($base_url, $page) = ($page_url =~ m!^(https?://[^/]+)(.*)!);
173     my $return_url = $base_url.AUTH_RETURN_HANDLER;
174
175     return wantarray ? ($return_url, $page) : $return_url;
176 }
177
178 use constant RETURN_PAGE_SESSION_KEY => "overdrive.return_page";
179 sub get_return_page_from_koha_session {
180     my $self = shift;
181     my $return_page = $self->get_from_koha_session(RETURN_PAGE_SESSION_KEY) || "";
182     $self->logger->debug("get_return_page_from_koha_session: $return_page");
183     return $return_page;
184 }
185 sub set_return_page_in_koha_session {
186     my $self = shift;
187     my $return_page = shift || "";
188     $self->logger->debug("set_return_page_in_koha_session: $return_page");
189     return $self->set_in_koha_session( RETURN_PAGE_SESSION_KEY, $return_page );
190 }
191
192 use constant ACCESS_TOKEN_SESSION_KEY => "overdrive.access_token";
193 my $ACCESS_TOKEN_DELIMITER = ":";
194 sub get_token_from_koha_session {
195     my $self = shift;
196     my ($token, $token_type)
197       = split $ACCESS_TOKEN_DELIMITER, $self->get_from_koha_session(ACCESS_TOKEN_SESSION_KEY) || "";
198     $self->logger->debug("get_token_from_koha_session: ".($token || "(none)"));
199     return ($token, $token_type);
200 }
201 sub set_token_in_koha_session {
202     my $self = shift;
203     my $token = shift || "";
204     my $token_type = shift || "";
205     $self->logger->debug("set_token_in_koha_session: $token $token_type");
206     return $self->set_in_koha_session(
207         ACCESS_TOKEN_SESSION_KEY,
208         join($ACCESS_TOKEN_DELIMITER, $token, $token_type)
209     );
210 }
211
212 =head2 checkout_download_url($item_id)
213
214   Input: id of the item to download
215
216   Returns: Fulfillment URL for reidrection
217
218 =cut
219
220 sub checkout_download_url {
221     my $self = shift;
222     my $item_id = shift or croak "Item ID not specified";
223
224     my $ua = LWP::UserAgent->new;
225     $ua->max_redirect(0);
226     my $response = $ua->get(
227         "https://patron.api.overdrive.com/v1/patrons/me/checkouts/".$item_id."/formats/downloadredirect",
228         'Authorization' => "Bearer ".$self->client->access_token,
229         );
230
231     my $redirect = { redirect => $response->{_headers}->{location} };
232     return $redirect;
233 }
234
235 =head1 OTHER METHODS
236
237 =head2 is_logged_in()
238
239   Returns boolean
240
241 =cut
242
243 sub is_logged_in {
244     my $self = shift;
245     my ($token, $token_type) = $self->get_token_from_koha_session();
246     $token ||= $self->auth_by_saved_token;
247     return $token;
248 }
249
250 sub auth_by_saved_token {
251     my $self = shift;
252
253     my $koha_patron = $self->koha_patron or return;
254
255     if (my $auth_token = $koha_patron->overdrive_auth_token) {
256         my ($access_token, $access_token_type, $new_auth_token)
257           = $self->client->make_access_token_request();
258         $self->set_token_in_koha_session($access_token, $access_token_type);
259         $koha_patron->set({overdrive_auth_token => $new_auth_token})->store;
260         return $access_token;
261     }
262
263     return;
264 }
265
266 =head2 forget()
267
268   Removes stored OverDrive token
269
270 =cut
271
272 sub forget {
273     my $self = shift;
274
275     $self->set_token_in_koha_session("", "");
276     if (my $koha_patron = $self->koha_patron) {
277         $koha_patron->set({overdrive_auth_token => undef})->store;
278     }
279 }
280
281 use vars qw{$AUTOLOAD};
282 sub AUTOLOAD {
283     my $self = shift;
284     (my $method = $AUTOLOAD) =~ s/.*:://;
285     my $od = $self->client;
286     local $@;
287     my $ret = eval { $od->$method(@_) };
288     if ($@) {
289         if ( $od->is_access_token_error($@) && $self->auth_by_saved_token ) {
290             return $od->$method(@_);
291         }
292         die $@;
293     }
294     return $ret;
295 }
296 sub DESTROY { }
297
298 =head1 AUTHOR
299
300 CatalystIT
301
302 =cut
303
304 1;