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
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
;
23 use base
qw(Koha::ExternalContent);
24 use WebService
::ILS
::OverDrive
::Patron
;
28 use constant logger
=> Koha
::Logger
->get();
32 Koha::ExternalContent::OverDrive
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);
45 A (very) thin wrapper around C<WebService::ILS::OverDrive::Patron>
47 Takes "OverDrive*" Koha preferences
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 }
77 =head1 L<WebService::ILS::OverDrive::Patron> METHODS
79 Methods used without mods:
83 =item C<error_message()>
91 =item C<checkout($id, $format)>
93 =item C<checkout_download_url($id)>
97 =item C<place_hold($id)>
99 =item C<remove_hold($id)>
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)
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
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";
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
{
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");
160 sub set_return_page_in_koha_session
{
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
{
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
{
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)
189 =head2 is_logged_in()
197 my ($token, $token_type) = $self->get_token_from_koha_session();
198 $token ||= $self->auth_by_saved_token;
202 sub auth_by_saved_token
{
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;
219 Removes stored OverDrive token
226 $self->set_token_in_koha_session("", "");
227 $self->koha_patron->set({overdrive_auth_token
=> undef})->store;
230 use vars
qw{$AUTOLOAD};
233 (my $method = $AUTOLOAD) =~ s/.*:://;
234 my $od = $self->client;
236 my $ret = eval { $od->$method(@_) };
238 if ( $od->is_access_token_error($@
) && $self->auth_by_saved_token ) {
239 return $od->$method(@_);