Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise RemoteInitiatedServerError on expired session key (PP-996) #1700

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion api/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1703,7 +1703,8 @@ def _parse(self, parsed: dict[str, Any], **kwargs: Any) -> T:
class Axis360FulfillmentInfoResponseParser(
JSONResponseParser[
tuple[Union[FindawayManifest, "AxisNowManifest"], datetime.datetime]
]
],
LoggerMixin,
):
"""Parse JSON documents into Findaway audiobook manifests or AxisNow manifests."""

Expand Down Expand Up @@ -1771,6 +1772,19 @@ def parse_findaway(
sessionKey = k("FNDSessionKey", parsed)
checkoutId = k("FNDTransactionID", parsed)

if sessionKey == "Expired":
try:
identifier_msg = f"{license_pool.identifier.type}/{license_pool.identifier.identifier}"
except AttributeError:
identifier_msg = f"LicensePool.id {license_pool.id}"

message = f"Expired findaway session key for {identifier_msg}. Request data: {json.dumps(parsed)}"
self.log.error(message)
raise RemoteInitiatedServerError(
message,
self.SERVICE_NAME,
)

# Acquire the TOC information
metadata_response = self.api.get_audiobook_metadata(fulfillmentId)
parser = AudiobookMetadataParser()
Expand Down
10 changes: 5 additions & 5 deletions api/controller/loan.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from lxml import etree
from werkzeug import Response as wkResponse

from api.circulation_exceptions import CirculationException
from api.circulation_exceptions import CirculationException, RemoteInitiatedServerError
from api.controller.circulation_manager import CirculationManagerController
from api.problem_details import (
BAD_DELIVERY_MECHANISM,
Expand Down Expand Up @@ -146,7 +146,7 @@ def _borrow(self, patron, credential, pool, mechanism):
patron, credential, pool, mechanism
)
result = loan or hold
except CirculationException as e:
except (CirculationException, RemoteInitiatedServerError) as e:
result = e.problem_detail

if result is None:
Expand Down Expand Up @@ -322,7 +322,7 @@ def fulfill(
requested_license_pool,
mechanism,
)
except CirculationException as e:
except (CirculationException, RemoteInitiatedServerError) as e:
return e.problem_detail

# A subclass of FulfillmentInfo may want to bypass the whole
Expand Down Expand Up @@ -446,15 +446,15 @@ def revoke(self, license_pool_id):
if loan:
try:
self.circulation.revoke_loan(patron, credential, pool)
except CirculationException as e:
except (CirculationException, RemoteInitiatedServerError) as e:
return e.problem_detail
elif hold:
if not self.circulation.can_revoke_hold(pool, hold):
title = _("Cannot release a hold once it enters reserved state.")
return CANNOT_RELEASE_HOLD.detailed(title, 400)
try:
self.circulation.release_hold(patron, credential, pool)
except CirculationException as e:
except (CirculationException, RemoteInitiatedServerError) as e:
return e.problem_detail

work = pool.work
Expand Down
7 changes: 7 additions & 0 deletions tests/api/test_axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,13 @@ def get_data():
m(bad_date, license_pool=pool)
assert "Could not parse expiration date: not-a-date" in str(excinfo.value)

# Try with an expired session key.
expired_session_key = get_data()
expired_session_key["FNDSessionKey"] = "Expired"
with pytest.raises(RemoteInitiatedServerError) as excinfo:
m(expired_session_key, license_pool=pool)
assert "Expired findaway session key" in str(excinfo.value)

def test__parse_axisnow(self, axis360parsers: Axis360FixturePlusParsers) -> None:
# _parse will create a valid AxisNowManifest given a
# complete document.
Expand Down
Loading