Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a middleware that takes a list of URLs / paths / regex patterns where MFA is enforced #69

Open
MarkusH opened this issue Jun 1, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@MarkusH
Copy link
Collaborator

MarkusH commented Jun 1, 2023

This is a feature request that stems from #60.

Feature request

As a developer, I want to ensure that views under some URL path are protected with multi-factor authentication. For example, I want everything under /admin/ to be protected with MFA. When the user hasn't provided MFA credentials since they logged in, they're redirected to a view where they can provide an TOTP token, backup code, or WebAuthN. After a successful verification, they're then redirected to the originally requested page.

This ticket likely requires #68 before it can be implemented.

Implementation idea

One way I can imagine this to work, is a middleware that looks at the current request path and compares it to a list (or rather set) of paths or a set of regex patterns. Something along these lines:

MFA_URLS = {"/my-view/", "/another/path/to/a/view"}
MFA_REGEX_PATTERNS = [r"^/admin/", r"^/internal/.+/something/$"]

def ensure_mfa_middleware(get_response):
    regex = re.compile(r"|".join(MFA_REGEX_PATTERNS))

    def middleware(request):
        if not request.session.get("kagi_verified", False):  # See #68
            if request.path in MFA_URLS or regex.match(request.path):
                return redirect("kagi:verify-second-factor")

        response = get_response(request)
        return response

    return middleware
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant