Skip to content

Middleware

hypha.apply.users.middleware

logger module-attribute

logger = getLogger('django.security.two_factor')

TWO_FACTOR_EXEMPTED_PATH_PREFIXES module-attribute

TWO_FACTOR_EXEMPTED_PATH_PREFIXES = ['/auth/', '/login/', '/logout/', '/account/', '/apply/submissions/success/']

SocialAuthExceptionMiddleware

Bases: SocialAuthExceptionMiddleware

Wrapper around SocialAuthExceptionMiddleware to customise messages

get_message

get_message(request, exception)
Source code in hypha/apply/users/middleware.py
def get_message(self, request, exception):
    if isinstance(exception, AuthForbidden):
        return "Your credentials are not recognised."

    super().get_message(request, exception)

TwoFactorAuthenticationMiddleware

TwoFactorAuthenticationMiddleware(get_response)

Middleware to enforce 2FA activation for unverified users

To activate this middleware set env variable ENFORCE_TWO_FACTOR as True.

This will redirect all request from unverified users to enable 2FA first. Except the request made on the url paths listed in TWO_FACTOR_EXEMPTED_PATH_PREFIXES.

Source code in hypha/apply/users/middleware.py
def __init__(self, get_response):
    if not settings.ENFORCE_TWO_FACTOR:
        raise MiddlewareNotUsed()

    self.get_response = get_response

reason class-attribute instance-attribute

reason = gettext_lazy('Two factor authentication required')

get_response instance-attribute

get_response = get_response

whitelisted_paths

whitelisted_paths(path)
Source code in hypha/apply/users/middleware.py
def whitelisted_paths(self, path):
    if path == "/":
        return True

    for sub_path in TWO_FACTOR_EXEMPTED_PATH_PREFIXES:
        if path.startswith(sub_path):
            return True
    return False

get_urls_open_rounds

get_urls_open_rounds()
Source code in hypha/apply/users/middleware.py
def get_urls_open_rounds(self):
    from hypha.apply.funds.models import ApplicationBase

    return map(
        get_page_path, ApplicationBase.objects.order_by_end_date().specific()
    )

get_urls_open_labs

get_urls_open_labs()
Source code in hypha/apply/users/middleware.py
def get_urls_open_labs(self):
    from hypha.apply.funds.models import LabBase

    return map(
        get_page_path,
        LabBase.objects.public().live().specific(),
    )

get_page_path

get_page_path(wagtail_page)
Source code in hypha/apply/users/middleware.py
def get_page_path(wagtail_page):
    _, _, page_path = wagtail_page.get_url_parts()
    return page_path