Skip to content

Commit

Permalink
Show lcp:hashed_passphrase only for LCP DRM-protected items (#39)
Browse files Browse the repository at this point in the history
* Show lcp:hashed_passphrase only for LCP DRM-protected items
* Refactor to align with tags on Adobe DRM.

Co-authored-by: Jonathan Green <[email protected]>
  • Loading branch information
vbessonov and jonathangreen authored Dec 15, 2021
1 parent 0eac415 commit a1c5974
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 65 deletions.
30 changes: 0 additions & 30 deletions opds.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
Pagination,
SearchFacets,
)
from .lcp.credential import LCPCredentialFactory
from .lcp.exceptions import LCPError
from .model import (
CachedFeed,
Contributor,
DataSource,
Edition,
ExternalIntegration,
Hyperlink,
Identifier,
Measurement,
Expand Down Expand Up @@ -1690,33 +1687,6 @@ def acquisition_link(cls, rel, href, types, active_loan=None):
link = cls.link(rel, href, initial_type)
indirect = cls.indirect_acquisition(indirect_types)

# In the case of LCP we have to include a patron's hashed passphrase
# inside the acquisition link so client applications can use it to bypass authentication
# and will not ask patrons to enter their passphrases
# For more information please look here:
# https://readium.org/lcp-specs/notes/lcp-key-retrieval.html#including-a-hashed-passphrase-in-an-opds-1-catalog
if active_loan and active_loan.license_pool.collection.protocol in [
ExternalIntegration.LCP,
ExternalIntegration.ODL,
ExternalIntegration.ODL2,
]:
db = Session.object_session(active_loan)
lcp_credential_factory = LCPCredentialFactory()

try:
hashed_passphrase = lcp_credential_factory.get_hashed_passphrase(
db, active_loan.patron
)
hashed_passphrase_element = AtomFeed.makeelement(
"{%s}hashed_passphrase" % AtomFeed.LCP_NS
)
hashed_passphrase_element.text = hashed_passphrase

link.append(hashed_passphrase_element)
except LCPError:
# The patron's passphrase wasn't generated yet and not present in the database.
pass

if indirect is not None:
link.append(indirect)
return link
Expand Down
36 changes: 1 addition & 35 deletions tests/test_opds.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import logging
import re
import xml.etree.ElementTree as ET
from io import StringIO

Expand All @@ -26,7 +25,6 @@
from ..external_search import MockExternalSearchIndex
from ..facets import FacetConstants
from ..lane import Facets, FeaturedFacets, Pagination, SearchFacets, WorkList
from ..lcp.credential import LCPCredentialFactory
from ..model import (
CachedFeed,
Contributor,
Expand Down Expand Up @@ -545,38 +543,6 @@ def test_acquisition_feed_includes_permanent_work_id(self):
entry = parsed["entries"][0]
assert work.presentation_edition.permanent_work_id == entry["simplified_pwid"]

def test_lcp_acquisition_link_contains_hashed_passphrase(self):
# Arrange
lcp_collection = self._collection(protocol=ExternalIntegration.LCP)
data_source = DataSource.lookup(self._db, DataSource.LCP, autocreate=True)
data_source_name = data_source.name
license_pool = self._licensepool(
edition=None, data_source_name=data_source_name, collection=lcp_collection
)
hashed_passphrase = "12345"
patron = self._patron()
lcp_credential_factory = LCPCredentialFactory()
loan, _ = license_pool.loan_to(patron)
rel = AcquisitionFeed.ACQUISITION_REL
href = self._url
types = [DeliveryMechanism.LCP_DRM, Representation.EPUB_MEDIA_TYPE]
expected_result = (
'<link href="{0}" rel="http://opds-spec.org/acquisition" '
'type="application/vnd.readium.lcp.license.v1.0+json">'
'<ns0:hashed_passphrase xmlns:ns0="http://readium.org/lcp-specs/ns">{1}</ns0:hashed_passphrase>'
'<ns0:indirectAcquisition xmlns:ns0="http://opds-spec.org/2010/catalog" type="application/epub+zip"/>'
"</link>"
).format(href, hashed_passphrase)

# Act
lcp_credential_factory.set_hashed_passphrase(
self._db, patron, hashed_passphrase
)
acquisition_link = AcquisitionFeed.acquisition_link(rel, href, types, loan)

# Assert
self._assert_xml_equal(acquisition_link, expected_result)

def test_lane_feed_contains_facet_links(self):
work = self._work(with_open_access_download=True)

Expand Down Expand Up @@ -2261,7 +2227,7 @@ def feed(self, annotator=VerboseAnnotator, **kwargs):
"http://whatever.io",
[],
annotator=annotator,
**kwargs
**kwargs,
)

def entry(self, identifier, work, annotator=VerboseAnnotator, **kwargs):
Expand Down

0 comments on commit a1c5974

Please sign in to comment.