From 04da8725ed07447ab15ea447f2a01d64f9539ce8 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Mon, 12 Sep 2022 08:04:16 +0200 Subject: [PATCH] Improve fetch --- app/activitypub.py | 6 ++++++ app/admin.py | 2 ++ app/httpsig.py | 7 ++----- app/templates/lookup.html | 2 ++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/activitypub.py b/app/activitypub.py index 968929d..e3ddac9 100644 --- a/app/activitypub.py +++ b/app/activitypub.py @@ -61,6 +61,10 @@ class ObjectNotFoundError(Exception): pass +class ObjectUnavailableError(Exception): + pass + + class FetchErrorTypeEnum(str, enum.Enum): TIMEOUT = "TIMEOUT" NOT_FOUND = "NOT_FOUND" @@ -167,6 +171,8 @@ async def fetch( # Special handling for deleted object if resp.status_code == 410: raise ObjectIsGoneError(f"{url} is gone") + elif resp.status_code in [401, 403]: + raise ObjectUnavailableError(f"not allowed to fetch {url}") elif resp.status_code == 404: raise ObjectNotFoundError(f"{url} not found") diff --git a/app/admin.py b/app/admin.py index 3a72889..621a8ac 100644 --- a/app/admin.py +++ b/app/admin.py @@ -85,6 +85,8 @@ async def get_lookup( error = ap.FetchErrorTypeEnum.TIMEOUT except (ap.ObjectNotFoundError, ap.ObjectIsGoneError): error = ap.FetchErrorTypeEnum.NOT_FOUND + except (ap.ObjectUnavailableError): + error = ap.FetchErrorTypeEnum.UNAUHTORIZED except Exception: logger.exception(f"Failed to lookup {query}") error = ap.FetchErrorTypeEnum.INTERNAL_ERROR diff --git a/app/httpsig.py b/app/httpsig.py index 7691c2b..583901d 100644 --- a/app/httpsig.py +++ b/app/httpsig.py @@ -115,11 +115,8 @@ async def _get_public_key(db_session: AsyncSession, key_id: str) -> Key: # might race to fetch each other key try: actor = await ap.fetch(key_id, disable_httpsig=True) - except httpx.HTTPStatusError as http_err: - if http_err.response.status_code in [401, 403]: - actor = await ap.fetch(key_id, disable_httpsig=False) - else: - raise + except ap.ObjectUnavailableError: + actor = await ap.fetch(key_id, disable_httpsig=False) if actor["type"] == "Key": # The Key is not embedded in the Person diff --git a/app/templates/lookup.html b/app/templates/lookup.html index 611a944..0b077e2 100644 --- a/app/templates/lookup.html +++ b/app/templates/lookup.html @@ -20,6 +20,8 @@
{% if error.value == "NOT_FOUND" %}

The remote object is unavailable.

+ {% elif error.value == "UNAUTHORIZED" %} +

Missing permissions to fetch the remote object.

{% elif error.value == "TIMEOUT" %}

Lookup timed out, please try refreshing the page.

{% else %}