login via pixelfed
This commit is contained in:
parent
489fe17ebb
commit
b2af3e6cbb
5 changed files with 13 additions and 11 deletions
|
@ -90,14 +90,15 @@ def post_toot(site, content, visibility, token, local_only=False):
|
|||
return response
|
||||
|
||||
|
||||
def get_instance_domain(domain_name):
|
||||
def get_instance_info(domain_name):
|
||||
if domain_name.lower().strip() == TWITTER_DOMAIN:
|
||||
return TWITTER_DOMAIN
|
||||
return TWITTER_DOMAIN, ''
|
||||
try:
|
||||
response = get(f'https://{domain_name}/api/v1/instance', headers={'User-Agent': 'NeoDB/1.0'})
|
||||
return response.json()['uri'].lower().split('//')[-1].split('/')[0]
|
||||
j = response.json()
|
||||
return j['uri'].lower().split('//')[-1].split('/')[0], j['version']
|
||||
except:
|
||||
return domain_name
|
||||
return domain_name, ''
|
||||
|
||||
|
||||
def create_app(domain_name):
|
||||
|
|
|
@ -36,11 +36,12 @@ def get_mastodon_application(domain):
|
|||
return app, error_msg
|
||||
|
||||
|
||||
def get_mastodon_login_url(app, login_domain, request):
|
||||
def get_mastodon_login_url(app, login_domain, version, request):
|
||||
url = request.scheme + "://" + request.get_host() + reverse('users:OAuth2_login')
|
||||
if login_domain == TWITTER_DOMAIN:
|
||||
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"
|
||||
return "https://" + login_domain + "/oauth/authorize?client_id=" + app.client_id + "&scope=" + quote(settings.MASTODON_CLIENT_SCOPE) + "&redirect_uri=" + url + "&response_type=code"
|
||||
scope = 'read' if 'Pixelfed' in version else settings.MASTODON_CLIENT_SCOPE
|
||||
return "https://" + login_domain + "/oauth/authorize?client_id=" + app.client_id + "&scope=" + quote(scope) + "&redirect_uri=" + url + "&response_type=code"
|
||||
|
||||
|
||||
def obtain_token(site, request, code):
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from mastodon.models import MastodonApplication
|
||||
from django.conf import settings
|
||||
from mastodon.api import get_instance_domain
|
||||
from mastodon.api import get_instance_info
|
||||
from users.models import User
|
||||
|
||||
|
||||
|
@ -12,7 +12,7 @@ class Command(BaseCommand):
|
|||
for site in MastodonApplication.objects.all():
|
||||
d = site.domain_name
|
||||
login_domain = d.strip().lower().split('//')[-1].split('/')[0].split('@')[-1]
|
||||
domain = get_instance_domain(login_domain)
|
||||
domain, version = get_instance_info(login_domain)
|
||||
if d != domain:
|
||||
print(f'{d} should be {domain}')
|
||||
for u in User.objects.filter(mastodon_site=d, is_active=True):
|
||||
|
|
|
@ -8,7 +8,7 @@ class MastodonApplication(models.Model):
|
|||
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)
|
||||
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)
|
||||
|
||||
|
|
|
@ -145,12 +145,12 @@ def connect(request):
|
|||
return redirect(reverse("users:login"))
|
||||
login_domain = request.session['swap_domain'] if request.session.get('swap_login') else request.GET.get('domain')
|
||||
login_domain = login_domain.strip().lower().split('//')[-1].split('/')[0].split('@')[-1]
|
||||
domain = get_instance_domain(login_domain)
|
||||
domain, version = get_instance_info(login_domain)
|
||||
app, error_msg = get_mastodon_application(domain)
|
||||
if app is None:
|
||||
return render(request, 'common/error.html', {'msg': error_msg, 'secondary_msg': "", })
|
||||
else:
|
||||
login_url = get_mastodon_login_url(app, login_domain, request)
|
||||
login_url = get_mastodon_login_url(app, login_domain, version, request)
|
||||
resp = redirect(login_url)
|
||||
resp.set_cookie("mastodon_domain", domain)
|
||||
return resp
|
||||
|
|
Loading…
Add table
Reference in a new issue