Coder Social home page Coder Social logo

Comments (1)

PetrDlouhy avatar PetrDlouhy commented on July 18, 2024

I have got this solved by storing the info about 2FA authentication in session. But I feel it is a bit hacky and unneedingly complicated.

The pipeline:

from django.shortcuts import redirect
from django.urls import reverse
from social_core.pipeline.partial import partial
from two_factor.utils import default_device


@partial
def two_factor_auth(strategy, details, *args, user=None, **kwargs):
    current_partial = kwargs.get("current_partial")
    request = kwargs["request"]
    if request.session.get("tfa_completed", False):
        return
    if default_device(user):
        return redirect(
            reverse("two_factor_authentication") + f"?partial_token={current_partial.token}"
        )
    return

The view:

from django.urls import reverse
from django.views.generic import FormView
from social_django.utils import load_strategy
from two_factor.forms import AuthenticationTokenForm
from two_factor.utils import default_device


class AuthenticationView(FormView):
    template_name = "two_factor/core/login.html"
    form_class = AuthenticationTokenForm
    
    def get_success_url(self):
        partial = self.get_partial()
        self.request.session["tfa_completed"] = True
        return (
            reverse("social:complete", kwargs={"backend": partial.backend})
            + f"?partial_token={partial.token}"
        )

    def get_partial(self):
        strategy = load_strategy()
        partial_token = self.request.GET.get("partial_token")
        partial = strategy.partial_load(partial_token)
        return partial

    def get_form_kwargs(self, *args, **kwargs):
        kwargs = super().get_form_kwargs(*args, **kwargs)
        user = self.get_partial().kwargs["user"]
        kwargs["user"] = user
        kwargs["initial_device"] = default_device(user)
        return kwargs

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["step"] = "auth"
        return context

from python-social-auth.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.