diff --git a/fediseer/apis/v1/base.py b/fediseer/apis/v1/base.py index d430df8..33ac7fd 100644 --- a/fediseer/apis/v1/base.py +++ b/fediseer/apis/v1/base.py @@ -10,6 +10,8 @@ from fediseer import exceptions as e from fediseer.utils import hash_api_key from fediseer.messaging import activitypub_pm from pythorhead import Lemmy +from fediseer.fediverse import get_admin_for_software, get_nodeinfo +from fediseer.consts import SUPPORTED_SOFTWARE api = Namespace('v1', 'API Version 1' ) @@ -50,3 +52,48 @@ class Suspicions(Resource): return {"domains": [instance["domain"] for instance in sus_instances]},200 return {"instances": sus_instances},200 + + +def ensure_instance_registered(domain): + if domain.endswith("test.dbzer0.com"): + # Fake instances for testing chain of trust + requested_lemmy = Lemmy(f"https://{domain}") + requested_lemmy._requestor.nodeinfo = {"software":{"name":"lemmy"}} + open_registrations = False + email_verify = True + software = "lemmy" + admin_usernames = ["db0"] + nodeinfo = get_nodeinfo("lemmy.dbzer0.com") + requested_lemmy = Lemmy(f"https://{domain}") + site = requested_lemmy.site.get() + else: + nodeinfo = get_nodeinfo(domain) + if not nodeinfo: + raise e.BadRequest(f"Error encountered while polling domain {domain}. Please check it's running correctly") + software = nodeinfo["software"]["name"] + if software not in SUPPORTED_SOFTWARE: + raise e.BadRequest(f"Fediverse software {software} not supported at this time") + if software == "lemmy": + requested_lemmy = Lemmy(f"https://{domain}") + site = requested_lemmy.site.get() + if not site: + raise e.BadRequest(f"Error encountered while polling lemmy domain {domain}. Please check it's running correctly") + open_registrations = site["site_view"]["local_site"]["registration_mode"] == "open" + email_verify = site["site_view"]["local_site"]["require_email_verification"] + software = software + admin_usernames = [a["person"]["name"] for a in site["admins"]] + else: + open_registrations = nodeinfo["openRegistrations"] + email_verify = False + admin_usernames = get_admin_for_software(software, domain) + instance = database.find_instance_by_domain(domain) + if instance: + return instance, nodeinfo, site, admin_usernames + new_instance = Instance( + domain=domain, + open_registrations=open_registrations, + email_verify=email_verify, + software=software, + ) + new_instance.create() + return new_instance, nodeinfo, site, admin_usernames diff --git a/fediseer/apis/v1/endorsements.py b/fediseer/apis/v1/endorsements.py index 56f0e05..96ee153 100644 --- a/fediseer/apis/v1/endorsements.py +++ b/fediseer/apis/v1/endorsements.py @@ -81,7 +81,9 @@ class Endorsements(Resource): unbroken_chain, chainbreaker = database.has_unbroken_chain(instance.id) if not unbroken_chain: raise e.Forbidden(f"Guarantee chain for this instance has been broken. Chain ends at {chainbreaker.domain}!") - target_instance = database.find_instance_by_domain(domain=domain) + target_instance, nodeinfo, site, admin_usernames = ensure_instance_registered(domain) + if not target_instance: + raise e.NotFound(f"Something went wrong trying to register this instance.") if len(target_instance.guarantors) == 0: raise e.Forbidden("Not Guaranteed instances can be endorsed. Please guarantee for them, or find someone who will.") if not target_instance: diff --git a/fediseer/apis/v1/guarantees.py b/fediseer/apis/v1/guarantees.py index e82a414..1b85afc 100644 --- a/fediseer/apis/v1/guarantees.py +++ b/fediseer/apis/v1/guarantees.py @@ -79,14 +79,14 @@ class Guarantees(Resource): raise e.NotFound(f"No Instance found matching provided API key and domain. Have you remembered to register it?") if len(instance.guarantors) == 0: raise e.Forbidden("Only guaranteed instances can guarantee others.") - if len(instance.guarantors) >= 20 and instance.id != 0: + if len(instance.guarantees) >= 20 and instance.id != 0: raise e.Forbidden("You cannot guarantee for more than 20 instances") unbroken_chain, chainbreaker = database.has_unbroken_chain(instance.id) if not unbroken_chain: raise e.Forbidden(f"Guarantee chain for this instance has been broken. Chain ends at {chainbreaker.domain}!") - target_instance = database.find_instance_by_domain(domain=domain) + target_instance, nodeinfo, site, admin_usernames = ensure_instance_registered(domain) if not target_instance: - raise e.BadRequest("Instance to endorse not found") + raise e.NotFound(f"Something went wrong trying to register this instance.") if database.get_guarantee(target_instance.id,instance.id): return {"message":'OK'}, 200 gdomain = target_instance.get_guarantor_domain() diff --git a/fediseer/apis/v1/whitelist.py b/fediseer/apis/v1/whitelist.py index 06f40d5..37f7a23 100644 --- a/fediseer/apis/v1/whitelist.py +++ b/fediseer/apis/v1/whitelist.py @@ -1,8 +1,6 @@ from fediseer.apis.v1.base import * from fediseer.messaging import activitypub_pm -from fediseer.fediverse import get_admin_for_software, get_nodeinfo from fediseer.classes.user import User, Claim -from fediseer.consts import SUPPORTED_SOFTWARE class Whitelist(Resource): get_parser = reqparse.RequestParser() @@ -41,7 +39,7 @@ class WhitelistDomain(Resource): '''Display info about a specific instance ''' self.args = self.get_parser.parse_args() - instance, nodeinfo, site, admin_usernames = self.ensure_instance_registered(domain) + instance, nodeinfo, site, admin_usernames = ensure_instance_registered(domain) if not instance: raise e.NotFound(f"Something went wrong trying to register this instance.") return instance.get_details(),200 @@ -64,7 +62,7 @@ class WhitelistDomain(Resource): self.args = self.put_parser.parse_args() if '@' in self.args.admin: raise e.BadRequest("Please send the username without any @ signs or domains") - instance, nodeinfo, site, admin_usernames = self.ensure_instance_registered(domain) + instance, nodeinfo, site, admin_usernames = ensure_instance_registered(domain) guarantor_instance = None if self.args.guarantor: guarantor_instance = database.find_instance_by_domain(self.args.guarantor) @@ -161,47 +159,3 @@ class WhitelistDomain(Resource): db.session.commit() logger.warning(f"{domain} deleted") return {"message":'Changed'}, 200 - - def ensure_instance_registered(self, domain): - if domain.endswith("test.dbzer0.com"): - # Fake instances for testing chain of trust - requested_lemmy = Lemmy(f"https://{domain}") - requested_lemmy._requestor.nodeinfo = {"software":{"name":"lemmy"}} - open_registrations = False - email_verify = True - software = "lemmy" - admin_usernames = ["db0"] - nodeinfo = get_nodeinfo("lemmy.dbzer0.com") - requested_lemmy = Lemmy(f"https://{domain}") - site = requested_lemmy.site.get() - else: - nodeinfo = get_nodeinfo(domain) - if not nodeinfo: - raise e.BadRequest(f"Error encountered while polling domain {domain}. Please check it's running correctly") - software = nodeinfo["software"]["name"] - if software not in SUPPORTED_SOFTWARE: - raise e.BadRequest(f"Fediverse software {software} not supported at this time") - if software == "lemmy": - requested_lemmy = Lemmy(f"https://{domain}") - site = requested_lemmy.site.get() - if not site: - raise e.BadRequest(f"Error encountered while polling lemmy domain {domain}. Please check it's running correctly") - open_registrations = site["site_view"]["local_site"]["registration_mode"] == "open" - email_verify = site["site_view"]["local_site"]["require_email_verification"] - software = software - admin_usernames = [a["person"]["name"] for a in site["admins"]] - else: - open_registrations = nodeinfo["openRegistrations"] - email_verify = False - admin_usernames = get_admin_for_software(software, domain) - instance = database.find_instance_by_domain(domain) - if instance: - return instance, nodeinfo, site, admin_usernames - new_instance = Instance( - domain=domain, - open_registrations=open_registrations, - email_verify=email_verify, - software=software, - ) - new_instance.create() - return new_instance, nodeinfo, site, admin_usernames