apf part 36: Use signed get if remote site responds 401

This commit is contained in:
freamon 2024-11-25 12:46:34 +00:00
parent ff90e50332
commit 26eb967a1c

View file

@ -258,7 +258,7 @@ def instance_allowed(host: str) -> bool:
return instance is not None return instance is not None
def find_actor_or_create(actor: str, create_if_not_found=True, community_only=False, signed_get=False) -> Union[User, Community, None]: def find_actor_or_create(actor: str, create_if_not_found=True, community_only=False) -> Union[User, Community, None]:
if isinstance(actor, dict): # Discourse does this if isinstance(actor, dict): # Discourse does this
actor = actor['id'] actor = actor['id']
actor_url = actor.strip() actor_url = actor.strip()
@ -316,7 +316,6 @@ def find_actor_or_create(actor: str, create_if_not_found=True, community_only=Fa
else: # User does not exist in the DB, it's going to need to be created from it's remote home instance else: # User does not exist in the DB, it's going to need to be created from it's remote home instance
if create_if_not_found: if create_if_not_found:
if actor.startswith('https://'): if actor.startswith('https://'):
if not signed_get:
try: try:
actor_data = get_request(actor_url, headers={'Accept': 'application/activity+json'}) actor_data = get_request(actor_url, headers={'Accept': 'application/activity+json'})
except httpx.HTTPError: except httpx.HTTPError:
@ -337,13 +336,17 @@ def find_actor_or_create(actor: str, create_if_not_found=True, community_only=Fa
if community_only and not isinstance(actor_model, Community): if community_only and not isinstance(actor_model, Community):
return None return None
return actor_model return actor_model
else: elif actor_data.status_code == 401:
try: try:
site = Site.query.get(1) site = Site.query.get(1)
actor_data = signed_get_request(actor_url, site.private_key, actor_data = signed_get_request(actor_url, site.private_key,
f"https://{current_app.config['SERVER_NAME']}/actor#main-key") f"https://{current_app.config['SERVER_NAME']}/actor#main-key")
if actor_data.status_code == 200: if actor_data.status_code == 200:
try:
actor_json = actor_data.json() actor_json = actor_data.json()
except Exception as e:
actor_data.close()
return None
actor_data.close() actor_data.close()
actor_model = actor_json_to_model(actor_json, address, server) actor_model = actor_json_to_model(actor_json, address, server)
if community_only and not isinstance(actor_model, Community): if community_only and not isinstance(actor_model, Community):