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