more info for login

This commit is contained in:
Your Name 2023-01-10 16:52:00 -05:00
parent d2d4f7a87a
commit 7cd73f6c67
3 changed files with 35 additions and 14 deletions

View file

@ -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

View file

@ -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})"
return f"{self.uid}({self.local_id}) in {self.target_site}({self.site_id})"

View file

@ -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