Bug 19191: Remove need for bug 19966
[koha.git] / Koha / ExternalContent / OverDrive.pm
blob4cde086417a7c98414c6f832ea52a0e88ce82427
1 # Copyright 2014 Catalyst
3 # This file is part of Koha.
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.
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.
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.
18 package Koha::ExternalContent::OverDrive;
20 use Modern::Perl;
21 use Carp;
23 use base qw(Koha::ExternalContent);
24 use WebService::ILS::OverDrive::Patron;
25 use C4::Context;
26 use Koha::Logger;
28 use constant logger => Koha::Logger->get();
30 =head1 NAME
32 Koha::ExternalContent::OverDrive
34 =head1 SYNOPSIS
36 Register return url with OverDrive:
37 base app url + /cgi-bin/koha/external/overdrive/auth.pl
39 use Koha::ExternalContent::OverDrive;
40 my $od_client = Koha::ExternalContent::OverDrive->new();
41 my $od_auth_url = $od_client->auth_url($return_page_url);
43 =head1 DESCRIPTION
45 A (very) thin wrapper around C<WebService::ILS::OverDrive::Patron>
47 Takes "OverDrive*" Koha preferences
49 =cut
51 sub new {
52 my $class = shift;
53 my $params = shift || {};
54 $params->{koha_session_id} or croak "No koha_session_id";
56 my $self = $class->SUPER::new($params);
57 unless ($params->{client}) {
58 my $client_key = C4::Context->preference('OverDriveClientKey')
59 or croak("OverDriveClientKey pref not set");
60 my $client_secret = C4::Context->preference('OverDriveClientSecret')
61 or croak("OverDriveClientSecret pref not set");
62 my $library_id = C4::Context->preference('OverDriveLibraryID')
63 or croak("OverDriveLibraryID pref not set");
64 my ($token, $token_type) = $self->get_token_from_koha_session();
65 $self->client( WebService::ILS::OverDrive::Patron->new(
66 client_id => $client_key,
67 client_secret => $client_secret,
68 library_id => $library_id,
69 access_token => $token,
70 access_token_type => $token_type,
71 user_agent_params => { agent => $class->agent_string }
72 ) );
74 return $self;
77 =head1 L<WebService::ILS::OverDrive::Patron> METHODS
79 Methods used without mods:
81 =over 4
83 =item C<error_message()>
85 =item C<patron()>
87 =item C<checkouts()>
89 =item C<holds()>
91 =item C<checkout($id, $format)>
93 =item C<checkout_download_url($id)>
95 =item C<return($id)>
97 =item C<place_hold($id)>
99 =item C<remove_hold($id)>
101 =back
103 Methods with slightly moded interfaces:
105 =head2 auth_url($page_url)
107 Input: url of the page from which OverDrive authentication was requested
109 Returns: Post OverDrive auth return handler url (see SYNOPSIS)
111 =cut
113 sub auth_url {
114 my $self = shift;
115 my $page_url = shift or croak "Page url not provided";
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);
122 =head2 auth_by_code($code, $base_url)
124 To be called in external/overdrive/auth.pl upon return from OverDrive auth
126 =cut
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";
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);
138 $self->koha_patron->set({overdrive_auth_token => $auth_token})->store;
139 return $self->get_return_page_from_koha_session;
142 use constant AUTH_RETURN_HANDLER => "/cgi-bin/koha/external/overdrive/auth.pl";
143 sub _return_url {
144 my $self = shift;
145 my $page_url = shift or croak "Page url not provided";
147 my ($base_url, $page) = ($page_url =~ m!^(https?://[^/]+)(.*)!);
148 my $return_url = $base_url.AUTH_RETURN_HANDLER;
150 return wantarray ? ($return_url, $page) : $return_url;
153 use constant RETURN_PAGE_SESSION_KEY => "overdrive.return_page";
154 sub get_return_page_from_koha_session {
155 my $self = shift;
156 my $return_page = $self->get_from_koha_session(RETURN_PAGE_SESSION_KEY) || "";
157 $self->logger->debug("get_return_page_from_koha_session: $return_page");
158 return $return_page;
160 sub set_return_page_in_koha_session {
161 my $self = shift;
162 my $return_page = shift || "";
163 $self->logger->debug("set_return_page_in_koha_session: $return_page");
164 return $self->set_in_koha_session( RETURN_PAGE_SESSION_KEY, $return_page );
167 use constant ACCESS_TOKEN_SESSION_KEY => "overdrive.access_token";
168 my $ACCESS_TOKEN_DELIMITER = ":";
169 sub get_token_from_koha_session {
170 my $self = shift;
171 my ($token, $token_type)
172 = split $ACCESS_TOKEN_DELIMITER, $self->get_from_koha_session(ACCESS_TOKEN_SESSION_KEY) || "";
173 $self->logger->debug("get_token_from_koha_session: ".($token || "(none)"));
174 return ($token, $token_type);
176 sub set_token_in_koha_session {
177 my $self = shift;
178 my $token = shift || "";
179 my $token_type = shift || "";
180 $self->logger->debug("set_token_in_koha_session: $token $token_type");
181 return $self->set_in_koha_session(
182 ACCESS_TOKEN_SESSION_KEY,
183 join($ACCESS_TOKEN_DELIMITER, $token, $token_type)
187 =head1 OTHER METHODS
189 =head2 is_logged_in()
191 Returns boolean
193 =cut
195 sub is_logged_in {
196 my $self = shift;
197 my ($token, $token_type) = $self->get_token_from_koha_session();
198 $token ||= $self->auth_by_saved_token;
199 return $token;
202 sub auth_by_saved_token {
203 my $self = shift;
205 my $koha_patron = $self->koha_patron;
206 if (my $auth_token = $koha_patron->overdrive_auth_token) {
207 my ($access_token, $access_token_type, $new_auth_token)
208 = $self->client->auth_by_token($auth_token);
209 $self->set_token_in_koha_session($access_token, $access_token_type);
210 $koha_patron->set({overdrive_auth_token => $new_auth_token})->store;
211 return $access_token;
214 return;
217 =head2 forget()
219 Removes stored OverDrive token
221 =cut
223 sub forget {
224 my $self = shift;
226 $self->set_token_in_koha_session("", "");
227 $self->koha_patron->set({overdrive_auth_token => undef})->store;
230 use vars qw{$AUTOLOAD};
231 sub AUTOLOAD {
232 my $self = shift;
233 (my $method = $AUTOLOAD) =~ s/.*:://;
234 my $od = $self->client;
235 local $@;
236 my $ret = eval { $od->$method(@_) };
237 if ($@) {
238 if ( $od->is_access_token_error($@) && $self->auth_by_saved_token ) {
239 return $od->$method(@_);
241 die $@;
243 return $ret;
245 sub DESTROY { }
247 =head1 AUTHOR
249 CatalystIT
251 =cut