Exemple #1
0
def old_login(request):
    if request.method == "GET":
        form = LoginForm()
        return render(request, 'setup/login.html', {'form': form})
    elif request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            api_base_url = form.cleaned_data['instance']
            tmp_base = parse.urlparse(api_base_url.lower())
            if tmp_base.netloc == '':
                api_base_url = parse.urlunparse(
                    ('https', tmp_base.path, '', '', '', ''))
            else:
                api_base_url = api_base_url.lower()

            request.session['instance'] = api_base_url
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']

            try:
                client = Client.objects.get(api_base_id=api_base_url)
            except (Client.DoesNotExist, Client.MultipleObjectsReturned):
                (client_id, client_secret) = Mastodon.create_app(
                    'brutaldon', api_base_url=api_base_url)
                client = Client(api_base_id=api_base_url,
                                client_id=client_id,
                                client_secret=client_secret)
                client.save()

            mastodon = Mastodon(client_id=client.client_id,
                                client_secret=client.client_secret,
                                api_base_url=api_base_url)

            try:
                account = Account.objects.get(username=username,
                                              client_id=client.id)
            except (Account.DoesNotExist, Account.MultipleObjectsReturned):
                account = Account(username=username,
                                  access_token="",
                                  client=client)
            try:
                access_token = mastodon.log_in(username, password)
                account.access_token = access_token
                account.save()
                request.session['username'] = username
                user = mastodon.account_verify_credentials()
                request.session['user'] = user

                return redirect(home)
            except:
                # FIXME: add the errors
                return render(request, 'setup/login.html', {'form': form})
        else:
            return render(request, 'setup/login.html', {'form': form})
Exemple #2
0
def login(request):
    # User posts instance name in form.
    # POST page redirects user to instance, where they log in.
    # Instance redirects user to oauth_after_login view.
    # oauth_after_login view saves credential in session, then redirects to home.
    if request.method == "GET":
        form = OAuthLoginForm()
        return render(request, 'setup/login-oauth.html', {'form': form})
    elif request.method == "POST":
        form = OAuthLoginForm(request.POST)
        redirect_uris = request.build_absolute_uri(reverse('oauth_callback'))
        if form.is_valid():
            api_base_url = form.cleaned_data['instance']
            tmp_base = parse.urlparse(api_base_url.lower())
            if tmp_base.netloc == '':
                api_base_url = parse.urlunparse(
                    ('https', tmp_base.path, '', '', '', ''))
                request.session['instance_hostname'] = tmp_base.path
            else:
                api_base_url = api_base_url.lower()
                request.session['instance_hostname'] = tmp_base.netloc

            request.session['instance'] = api_base_url
            try:
                client = Client.objects.get(api_base_id=api_base_url)
            except (Client.DoesNotExist, Client.MultipleObjectsReturned):
                (client_id, client_secret) = Mastodon.create_app(
                    'brutaldon',
                    api_base_url=api_base_url,
                    redirect_uris=redirect_uris,
                    scopes=['read', 'write', 'follow'])
                client = Client(api_base_id=api_base_url,
                                client_id=client_id,
                                client_secret=client_secret)
                client.save()

            request.session['client_id'] = client.client_id
            request.session['client_secret'] = client.client_secret

            mastodon = Mastodon(client_id=client.client_id,
                                client_secret=client.client_secret,
                                api_base_url=api_base_url)
            return redirect(
                mastodon.auth_request_url(redirect_uris=redirect_uris,
                                          scopes=['read', 'write', 'follow']))
        else:
            return render(request, 'setup/login.html', {'form': form})

    else:
        return redirect(login)
Exemple #3
0
def old_login(request):
    if request.method == "GET":
        form = LoginForm()
        return render(request, 'setup/login.html', {'form': form})
    elif request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            api_base_url = form.cleaned_data['instance']
            tmp_base = parse.urlparse(api_base_url.lower())
            if tmp_base.netloc == '':
                api_base_url = parse.urlunparse(
                    ('https', tmp_base.path, '', '', '', ''))
                request.session['instance_hostname'] = tmp_base.path
            else:
                api_base_url = api_base_url.lower()
                request.session['instance_hostname'] = tmp_base.netloc

            request.session['instance'] = api_base_url
            email = form.cleaned_data['email']
            password = form.cleaned_data['password']

            try:
                client = Client.objects.get(api_base_id=api_base_url)
            except (Client.DoesNotExist, Client.MultipleObjectsReturned):
                (client_id, client_secret) = Mastodon.create_app(
                    'brutaldon',
                    api_base_url=api_base_url,
                    scopes=['read', 'write', 'follow'])
                client = Client(api_base_id=api_base_url,
                                client_id=client_id,
                                client_secret=client_secret)
                client.save()

            mastodon = Mastodon(client_id=client.client_id,
                                client_secret=client.client_secret,
                                api_base_url=api_base_url)

            try:
                account = Account.objects.get(email=email, client_id=client.id)
            except (Account.DoesNotExist, Account.MultipleObjectsReturned):
                preferences = Preference(theme=Theme.objects.get(id=1))
                preferences.save()
                account = Account(email=email,
                                  access_token="",
                                  client=client,
                                  preferences=preferences)
            try:
                access_token = mastodon.log_in(
                    email, password, scopes=['read', 'write', 'follow'])
                account.access_token = access_token
                user = mastodon.account_verify_credentials()
                request.session['user'] = user
                request.session[
                    'username'] = user.username + '@' + request.session[
                        'instance_hostname']
                account.username = request.session['username']
                request.session['timezone'] = account.preferences.timezone
                account.save()
                return redirect(home)

            except Exception as ex:
                form.add_error('', ex)
                return render(request, 'setup/login.html', {'form': form})
        else:
            return render(request, 'setup/login.html', {'form': form})