From 7cd73f6c67694912f348d72ef8e7154b4dfd06a4 Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 10 Jan 2023 16:52:00 -0500 Subject: [PATCH] more info for login --- mastodon/api.py | 6 +++--- mastodon/models.py | 25 +++++++++++++++---------- users/account.py | 18 +++++++++++++++++- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/mastodon/api.py b/mastodon/api.py index 7f75d9ca..d7d3746f 100644 --- a/mastodon/api.py +++ b/mastodon/api.py @@ -115,8 +115,8 @@ def post_toot(site, content, visibility, token, local_only=False, update_id=None def get_instance_info(domain_name): if domain_name.lower().strip() == TWITTER_DOMAIN: return TWITTER_DOMAIN, "" + url = f"https://{domain_name}/api/v1/instance" try: - url = f"https://{domain_name}/api/v1/instance" response = get(url, headers={"User-Agent": USER_AGENT}) j = response.json() return j["uri"].lower().split("//")[-1].split("/")[0], j["version"] @@ -355,7 +355,7 @@ def get_mastodon_login_url(app, login_domain, version, request): return f"https://twitter.com/i/oauth2/authorize?response_type=code&client_id={app.client_id}&redirect_uri={quote(url)}&scope={quote(settings.TWITTER_CLIENT_SCOPE)}&state=state&code_challenge=challenge&code_challenge_method=plain" scope = ( settings.MASTODON_LEGACY_CLIENT_SCOPE - if "Pixelfed" in version + if "Pixelfed" in version or version[0:2] == "0." else settings.MASTODON_CLIENT_SCOPE ) return ( @@ -490,7 +490,7 @@ def share_mark(mark): mark.save(update_fields=["shared_link"]) return True else: - print(response) + logger.error(response) return False diff --git a/mastodon/models.py b/mastodon/models.py index 49346810..90bd1223 100644 --- a/mastodon/models.py +++ b/mastodon/models.py @@ -4,16 +4,20 @@ from django.utils.translation import gettext_lazy as _ class MastodonApplication(models.Model): - domain_name = models.CharField(_('site domain name'), max_length=100, unique=True) - app_id = models.PositiveIntegerField(_('in-site app id')) # TODO Remove? bc 1) it seems useless 2) GoToSocial returns a hash text id - client_id = models.CharField(_('client id'), max_length=100) - client_secret = models.CharField(_('client secret'), max_length=100) - vapid_key = models.CharField(_('vapid key'), max_length=200, null=True, blank=True) - star_mode = models.PositiveIntegerField(_('0: custom emoji; 1: unicode moon; 2: text'), blank=False, default=0) - max_status_len = models.PositiveIntegerField(_('max toot len'), blank=False, default=500) + domain_name = models.CharField(_("site domain name"), max_length=100, unique=True) + app_id = models.CharField(_("in-site app id"), max_length=100) + client_id = models.CharField(_("client id"), max_length=100) + client_secret = models.CharField(_("client secret"), max_length=100) + vapid_key = models.CharField(_("vapid key"), max_length=200, null=True, blank=True) + star_mode = models.PositiveIntegerField( + _("0: custom emoji; 1: unicode moon; 2: text"), blank=False, default=0 + ) + max_status_len = models.PositiveIntegerField( + _("max toot len"), blank=False, default=500 + ) is_proxy = models.BooleanField(default=False, blank=True) - proxy_to = models.CharField(max_length=100, blank=True, default='') + proxy_to = models.CharField(max_length=100, blank=True, default="") # website # name # redirect_uris @@ -34,8 +38,9 @@ class CrossSiteUserInfo(models.Model): class Meta: constraints = [ models.UniqueConstraint( - fields=['uid', 'target_site'], name="unique_cross_site_user_info") + fields=["uid", "target_site"], name="unique_cross_site_user_info" + ) ] def __str__(self): - return f"{self.uid}({self.local_id}) in {self.target_site}({self.site_id})" \ No newline at end of file + return f"{self.uid}({self.local_id}) in {self.target_site}({self.site_id})" diff --git a/users/account.py b/users/account.py index f8ace2cd..a682d764 100644 --- a/users/account.py +++ b/users/account.py @@ -96,13 +96,29 @@ def OAuth2_login(request): return HttpResponseBadRequest() code = request.GET.get("code") + if not code: + return render( + request, + "common/error.html", + {"msg": _("认证失败😫"), "secondary_msg": _("Mastodon服务未能返回有效认证信息")}, + ) site = request.COOKIES.get("mastodon_domain") + if not code: + return render( + request, + "common/error.html", + {"msg": _("认证失败😫"), "secondary_msg": _("无效Cookie信息")}, + ) try: token, refresh_token = obtain_token(site, request, code) except ObjectDoesNotExist: return HttpResponseBadRequest("Mastodon site not registered") if not token: - return render(request, "common/error.html", {"msg": _("认证失败😫")}) + return render( + request, + "common/error.html", + {"msg": _("认证失败😫"), "secondary_msg": _("Mastodon服务未能返回有效认证令牌")}, + ) if ( request.session.get("swap_login", False) and request.user.is_authenticated