Permalink
Browse files

Merge pull request #1354 from internetarchive/testing/lending-changes…

…-cdl2

Testing/lending changes cdl2
  • Loading branch information...
mekarpeles committed Oct 16, 2018
2 parents ba34fd3 + 652ac7c commit d60606b845cbb03b277b1af7a912a2711585696f
Showing with 31 additions and 48 deletions.
  1. +0 −32 openlibrary/core/lending.py
  2. +31 −16 openlibrary/plugins/upstream/borrow.py
@@ -24,11 +24,6 @@
logger = logging.getLogger(__name__)
# How long the auth token given to the BookReader should last. After the auth token
# expires the BookReader will not be able to access the book. The BookReader polls
# OL periodically to get fresh tokens.
BOOKREADER_AUTH_SECONDS = dateutil.MINUTE_SECS * 10
# When we generate a loan offer (.acsm) for a user we assume that the loan has occurred.
# Once the loan fulfillment inside Digital Editions the book status server will know
# the loan has occurred. We allow this timeout so that we don't delete the OL loan
@@ -642,33 +637,6 @@ def get_resource_id(identifier, resource_type):
if rtype == resource_type:
return resource_id
def make_ia_token(item_id, expiry_seconds):
"""Make a key that allows a client to access the item on archive.org for the number of
seconds from now.
"""
access_key = config_ia_access_secret
if access_key is None:
raise Exception("config value config.ia_access_secret is not present -- check your config")
timestamp = int(time.time() + expiry_seconds)
token_data = '%s-%d' % (item_id, timestamp)
token = '%d-%s' % (timestamp, hmac.new(access_key, token_data).hexdigest())
return token
def make_bookreader_auth_link(loan_key, item_id, book_path, ol_host):
"""
Generate a link to BookReaderAuth.php that starts the BookReader with the information to initiate reading
a borrowed book
"""
access_token = make_ia_token(item_id, BOOKREADER_AUTH_SECONDS)
auth_url = 'https://%s/bookreader/BookReaderAuth.php?uuid=%s&token=%s&id=%s&bookPath=%s&olHost=%s' % (
config_bookreader_host, loan_key, access_token, item_id, book_path, ol_host
)
return auth_url
def update_loan_status(identifier):
"""Update the loan status in OL based off status in ACS4. Used to check for early returns."""
@@ -5,6 +5,7 @@
import hmac
import re
import simplejson
import urllib
import urllib2
import logging
@@ -21,9 +22,10 @@
from openlibrary.core import msgbroker
from openlibrary.core import lending
from openlibrary.core import waitinglist
from openlibrary.core import ab
from openlibrary.accounts.model import OpenLibraryAccount
from openlibrary import accounts
from openlibrary.core import ab
from openlibrary.utils import dateutil
from lxml import etree
@@ -60,7 +62,8 @@
# How long the auth token given to the BookReader should last. After the auth token
# expires the BookReader will not be able to access the book. The BookReader polls
# OL periodically to get fresh tokens.
bookreader_auth_seconds = 10*60
BOOKREADER_AUTH_SECONDS = dateutil.MINUTE_SECS * 10
READER_AUTH_SECONDS = dateutil.MINUTE_SECS * 2
# Base URL for BookReader
try:
@@ -181,7 +184,9 @@ def POST(self, key):
stats.increment('ol.loans.bookreader')
raise web.seeother(make_bookreader_auth_link(
loan.get_key(), edition.ocaid, '/stream/' + edition.ocaid, ol_host))
loan.get_key(), edition.ocaid,
'/stream/' + edition.ocaid, ol_host,
ia_userid=ia_itemname))
elif resource_type == 'pdf':
stats.increment('ol.loans.pdf')
raise web.seeother(loan_link)
@@ -226,7 +231,10 @@ def POST(self, key):
loans = get_loans(user)
for loan in loans:
if loan['book'] == edition.key:
raise web.seeother(make_bookreader_auth_link(loan['_key'], edition.ocaid, '/stream/' + edition.ocaid, ol_host))
raise web.seeother(make_bookreader_auth_link(
loan['_key'], edition.ocaid, '/stream/' + edition.ocaid,
ol_host, ia_userid=ia_itemname
))
elif action == 'join-waitinglist':
return self.POST_join_waitinglist(edition, user)
elif action == 'leave-waitinglist':
@@ -899,7 +907,10 @@ def get_ia_auth_dict(user, item_id, user_specified_loan_key, access_token):
if not user_has_current_loan:
raise Exception('lending: no current loan for this user found but no error condition specified')
return { 'success': True, 'token': make_ia_token(item_id, bookreader_auth_seconds) }
return {
'success': True,
'token': make_ia_token(item_id, BOOKREADER_AUTH_SECONDS)
}
def make_ia_token(item_id, expiry_seconds):
@@ -952,19 +963,23 @@ def ia_token_is_current(item_id, access_token):
return False
def make_bookreader_auth_link(loan_key, item_id, book_path, ol_host):
def make_bookreader_auth_link(loan_key, item_id, book_path, ol_host, ia_userid=None):
"""
Generate a link to BookReaderAuth.php that starts the BookReader with the information to initiate reading
a borrowed book
Generate a link to BookReaderAuth.php that starts the BookReader
with the information to initiate reading a borrowed book
"""
olAuthUrl = "https://{0}/ia_auth/XXX".format(ol_host)
access_token = make_ia_token(item_id, bookreader_auth_seconds)
auth_url = 'https://%s/bookreader/BookReaderAuth.php?uuid=%s&token=%s&id=%s&bookPath=%s&olHost=%s&olAuthUrl=%s' % (
bookreader_host, loan_key, access_token, item_id, book_path, ol_host, olAuthUrl
)
return auth_url
auth_link = 'https://%s/bookreader/BookReaderAuth.php?' % bookreader_host
params = {
'uuid': loan_key,
'token': make_ia_token(item_id, BOOKREADER_AUTH_SECONDS),
'id': item_id,
'bookPath': book_path,
'olHost': ol_host,
'olAuthUrl': "https://{0}/ia_auth/XXX".format(ol_host),
'iaUserId': ia_userid,
'iaAuthToken': make_ia_token(ia_userid, READER_AUTH_SECONDS)
}
return auth_link + urllib.urlencode(params)
def on_loan_update(loan):
# update the waiting list and ebook document.

0 comments on commit d60606b

Please sign in to comment.