Bug 22128: Removed outdated comment
[koha.git] / Koha / ExternalContent / OverDrive.pm
blob628e76122efe26d42ab4c4dbd2e8d4ef78d7da3f
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 Granted 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 if (my $koha_patron = $self->koha_patron) {
139 $koha_patron->set({overdrive_auth_token => $auth_token})->store;
141 return $self->get_return_page_from_koha_session;
144 =head2 auth_by_userid($userid, $password, $website_id, $authorization_name)
146 To be called to check auth of patron using OverDrive Patron Authentication method
147 This requires a SIP connection configured with OverDrive
149 =cut
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";
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);
164 $self->koha_patron->set({overdrive_auth_token => $auth_token})->store;
165 return $self->get_return_page_from_koha_session;
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";
173 my ($base_url, $page) = ($page_url =~ m!^(https?://[^/]+)(.*)!);
174 my $return_url = $base_url.AUTH_RETURN_HANDLER;
176 return wantarray ? ($return_url, $page) : $return_url;
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;
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 );
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);
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)
213 =head1 OTHER METHODS
215 =head2 is_logged_in()
217 Returns boolean
219 =cut
221 sub is_logged_in {
222 my $self = shift;
223 my ($token, $token_type) = $self->get_token_from_koha_session();
224 $token ||= $self->auth_by_saved_token;
225 return $token;
228 sub auth_by_saved_token {
229 my $self = shift;
231 my $koha_patron = $self->koha_patron or return;
233 if (my $auth_token = $koha_patron->overdrive_auth_token) {
234 my ($access_token, $access_token_type, $new_auth_token)
235 = $self->client->make_access_token_request();
236 $self->set_token_in_koha_session($access_token, $access_token_type);
237 $koha_patron->set({overdrive_auth_token => $new_auth_token})->store;
238 return $access_token;
241 return;
244 =head2 forget()
246 Removes stored OverDrive token
248 =cut
250 sub forget {
251 my $self = shift;
253 $self->set_token_in_koha_session("", "");
254 if (my $koha_patron = $self->koha_patron) {
255 $koha_patron->set({overdrive_auth_token => undef})->store;
259 use vars qw{$AUTOLOAD};
260 sub AUTOLOAD {
261 my $self = shift;
262 (my $method = $AUTOLOAD) =~ s/.*:://;
263 my $od = $self->client;
264 local $@;
265 my $ret = eval { $od->$method(@_) };
266 if ($@) {
267 if ( $od->is_access_token_error($@) && $self->auth_by_saved_token ) {
268 return $od->$method(@_);
270 die $@;
272 return $ret;
274 sub DESTROY { }
276 =head1 AUTHOR
278 CatalystIT
280 =cut