Bug 25501: Supress warnings on installing translation
[koha.git] / Koha / ExternalContent / OverDrive.pm
blobad36c18ce894829b16240747d6136c6bc5caf8a4
1 # Copyright 2014 Catalyst
3 # This file is part of Koha.
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.
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.
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>.
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;
27 =head1 NAME
29 Koha::ExternalContent::OverDrive
31 =head1 SYNOPSIS
33 Register return url with OverDrive:
34 base app url + /cgi-bin/koha/external/overdrive/auth.pl
36 use Koha::ExternalContent::OverDrive;
37 my $od_client = Koha::ExternalContent::OverDrive->new();
38 my $od_auth_url = $od_client->auth_url($return_page_url);
40 =head1 DESCRIPTION
42 A (very) thin wrapper around C<WebService::ILS::OverDrive::Patron>
44 Takes "OverDrive*" Koha preferences
46 =cut
48 sub new {
49 my $class = shift;
50 my $params = shift || {};
51 $params->{koha_session_id} or croak "No koha_session_id";
53 my $self = $class->SUPER::new($params);
54 unless ($params->{client}) {
55 my $client_key = C4::Context->preference('OverDriveClientKey')
56 or croak("OverDriveClientKey pref not set");
57 my $client_secret = C4::Context->preference('OverDriveClientSecret')
58 or croak("OverDriveClientSecret pref not set");
59 my $library_id = C4::Context->preference('OverDriveLibraryID')
60 or croak("OverDriveLibraryID pref not set");
61 my ($token, $token_type) = $self->get_token_from_koha_session();
62 $self->client( WebService::ILS::OverDrive::Patron->new(
63 client_id => $client_key,
64 client_secret => $client_secret,
65 library_id => $library_id,
66 access_token => $token,
67 access_token_type => $token_type,
68 user_agent_params => { agent => $class->agent_string }
69 ) );
72 return $self;
75 =head1 L<WebService::ILS::OverDrive::Patron> METHODS
77 Methods used without mods:
79 =over 4
81 =item C<error_message()>
83 =item C<patron()>
85 =item C<checkouts()>
87 =item C<holds()>
89 =item C<checkout($id, $format)>
91 =item C<checkout_download_url($id)>
93 =item C<return($id)>
95 =item C<place_hold($id)>
97 =item C<remove_hold($id)>
99 =back
101 Methods with slightly moded interfaces:
103 =head2 auth_url($page_url)
105 Input: url of the page from which OverDrive authentication was requested
107 Returns: Post OverDrive auth return handler url (see SYNOPSIS)
109 =cut
111 sub auth_url {
112 my $self = shift;
113 my $page_url = shift or croak "Page url not provided";
115 my ($return_url, $page) = $self->_return_url($page_url);
116 $self->set_return_page_in_koha_session($page);
117 return $self->client->auth_url($return_url);
120 =head2 auth_by_code($code, $base_url)
122 To be called in external/overdrive/auth.pl upon return from OverDrive Granted auth
124 =cut
126 sub auth_by_code {
127 my $self = shift;
128 my $code = shift or croak "OverDrive auth code not provided";
129 my $base_url = shift or croak "App base url not provided";
131 my ($access_token, $access_token_type, $auth_token)
132 = $self->client->auth_by_code($code, $self->_return_url($base_url));
133 $access_token or die "Invalid OverDrive code returned";
134 $self->set_token_in_koha_session($access_token, $access_token_type);
136 if (my $koha_patron = $self->koha_patron) {
137 $koha_patron->set({overdrive_auth_token => $auth_token})->store;
139 return $self->get_return_page_from_koha_session;
142 =head2 auth_by_userid($userid, $password, $website_id, $authorization_name)
144 To be called to check auth of patron using OverDrive Patron Authentication method
145 This requires a SIP connection configured with OverDrive
147 =cut
149 sub auth_by_userid {
150 my $self = shift;
151 my $userid = shift or croak "No user provided";
152 my $password = shift;
153 croak "No password provided" unless ($password || !C4::Context->preference("OverDrivePasswordRequired"));
154 my $website_id = shift or croak "OverDrive Library ID not provided";
155 my $authorization_name = shift or croak "OverDrive Authname not provided";
157 my ($access_token, $access_token_type, $auth_token)
158 = $self->client->auth_by_user_id($userid, $password, $website_id, $authorization_name);
159 $access_token or die "Invalid OverDrive code returned";
160 $self->set_token_in_koha_session($access_token, $access_token_type);
162 $self->koha_patron->set({overdrive_auth_token => $auth_token})->store;
163 return $self->get_return_page_from_koha_session;
166 use constant AUTH_RETURN_HANDLER => "/cgi-bin/koha/external/overdrive/auth.pl";
167 sub _return_url {
168 my $self = shift;
169 my $page_url = shift or croak "Page url not provided";
171 my ($base_url, $page) = ($page_url =~ m!^(https?://[^/]+)(.*)!);
172 my $return_url = $base_url.AUTH_RETURN_HANDLER;
174 return wantarray ? ($return_url, $page) : $return_url;
177 use constant RETURN_PAGE_SESSION_KEY => "overdrive.return_page";
178 sub get_return_page_from_koha_session {
179 my $self = shift;
180 my $return_page = $self->get_from_koha_session(RETURN_PAGE_SESSION_KEY) || "";
181 $self->logger->debug("get_return_page_from_koha_session: $return_page");
182 return $return_page;
184 sub set_return_page_in_koha_session {
185 my $self = shift;
186 my $return_page = shift || "";
187 $self->logger->debug("set_return_page_in_koha_session: $return_page");
188 return $self->set_in_koha_session( RETURN_PAGE_SESSION_KEY, $return_page );
191 use constant ACCESS_TOKEN_SESSION_KEY => "overdrive.access_token";
192 my $ACCESS_TOKEN_DELIMITER = ":";
193 sub get_token_from_koha_session {
194 my $self = shift;
195 my ($token, $token_type)
196 = split $ACCESS_TOKEN_DELIMITER, $self->get_from_koha_session(ACCESS_TOKEN_SESSION_KEY) || "";
197 $self->logger->debug("get_token_from_koha_session: ".($token || "(none)"));
198 return ($token, $token_type);
200 sub set_token_in_koha_session {
201 my $self = shift;
202 my $token = shift || "";
203 my $token_type = shift || "";
204 $self->logger->debug("set_token_in_koha_session: $token $token_type");
205 return $self->set_in_koha_session(
206 ACCESS_TOKEN_SESSION_KEY,
207 join($ACCESS_TOKEN_DELIMITER, $token, $token_type)
211 =head1 OTHER METHODS
213 =head2 is_logged_in()
215 Returns boolean
217 =cut
219 sub is_logged_in {
220 my $self = shift;
221 my ($token, $token_type) = $self->get_token_from_koha_session();
222 $token ||= $self->auth_by_saved_token;
223 return $token;
226 sub auth_by_saved_token {
227 my $self = shift;
229 my $koha_patron = $self->koha_patron or return;
231 if (my $auth_token = $koha_patron->overdrive_auth_token) {
232 my ($access_token, $access_token_type, $new_auth_token)
233 = $self->client->make_access_token_request();
234 $self->set_token_in_koha_session($access_token, $access_token_type);
235 $koha_patron->set({overdrive_auth_token => $new_auth_token})->store;
236 return $access_token;
239 return;
242 =head2 forget()
244 Removes stored OverDrive token
246 =cut
248 sub forget {
249 my $self = shift;
251 $self->set_token_in_koha_session("", "");
252 if (my $koha_patron = $self->koha_patron) {
253 $koha_patron->set({overdrive_auth_token => undef})->store;
257 use vars qw{$AUTOLOAD};
258 sub AUTOLOAD {
259 my $self = shift;
260 (my $method = $AUTOLOAD) =~ s/.*:://;
261 my $od = $self->client;
262 local $@;
263 my $ret = eval { $od->$method(@_) };
264 if ($@) {
265 if ( $od->is_access_token_error($@) && $self->auth_by_saved_token ) {
266 return $od->$method(@_);
268 die $@;
270 return $ret;
272 sub DESTROY { }
274 =head1 AUTHOR
276 CatalystIT
278 =cut