From a5889fa43c655f8297965172c0f74a1acfdeedc5 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 2 Dec 2023 19:15:35 +0000 Subject: [PATCH] Separate logical instance domain from physical URL. Some instances don't have the same domain name, e.g. Julia Evans's personal instance lives at https://social.jvns.ca but the domain name for username purposes is user@jvns.ca. Now I think we're correctly distinguishing the two concepts. --- client.py | 14 +++++++------- login.py | 17 ++++++++++------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/client.py b/client.py index 92569ef..1a05115 100644 --- a/client.py +++ b/client.py @@ -25,18 +25,18 @@ class Client: self.bearer_token = None self.log_response = lambda *args, **kws: None - def set_instance(self, instance): - self.instance = instance + def set_instance_url(self, instance_url): + self.instance_url = instance_url self.urls = { None: '', - 'auth': "https://" + instance + "/oauth/", - 'api': "https://" + instance + "/api/v1/", + 'auth': instance_url + "/oauth/", + 'api': instance_url + "/api/v1/", } - def set_username(self, username, account_id): + def set_username(self, username, instance_domain, account_id): self.username = username - self.fq_username = f"{self.username}@{self.instance}" - self.at_fq_username = f"@{self.username}@{self.instance}" + self.instance_domain = instance_domain + self.fq_username = f"{self.username}@{self.instance_domain}" self.account_id = account_id def enable_debug(self, logfile): diff --git a/login.py b/login.py index aab5afc..279f28f 100644 --- a/login.py +++ b/login.py @@ -12,11 +12,11 @@ class LoginUI(client.Client): def run(self): redirect_uri = 'urn:ietf:wg:oauth:2.0:oob' - instance = input("Enter a Mastodon instance name: ") - if "://" not in instance: - instance = "https://" + instance + instance_url = input("Enter a Mastodon instance URL: ") + if "://" not in instance_url: + instance_url = "https://" + instance_url - self.set_instance(instance) + self.set_instance_url(instance_url) app = self.post( "apps", @@ -66,12 +66,14 @@ class LoginUI(client.Client): self.bearer_token = user_token['access_token'] account = self.get("accounts/verify_credentials") + instance = self.get("instance") self.bearer_token = None data = { 'account_id': account['id'], 'username': account['username'], - 'instance': instance.split("://", 1)[-1], + 'instance_url': instance_url, + 'instance_domain': instance['uri'], 'client_id': client_id, 'client_secret': client_secret, 'user_token': user_token['access_token'], @@ -96,6 +98,7 @@ def setup_client(cl): with open(config_file()) as fh: data = json.load(fh) - cl.set_instance(data['instance']) - cl.set_username(data['username'], data['account_id']) + cl.set_instance_url(data['instance_url']) + cl.set_username(data['username'], data['instance_domain'], + data['account_id']) cl.bearer_token = data['user_token'] -- 2.30.2