multifactor.decorators¶
from multifactor.decorators import multifactor_protected
The single public symbol in multifactor.decorators.
multifactor_protected¶
multifactor_protected(
factors: int | Callable[[HttpRequest], int] = 0,
user_filter: dict | None = None,
max_age: int = 0,
advertise: bool = False,
)
Decorator factory. Returns a decorator that wraps a view function.
Parameters¶
Name |
Type |
Effect |
|---|---|---|
|
|
Minimum number of active (verified-and-not-expired) factors required for the view to render. |
|
|
Passed verbatim to |
|
|
Seconds since the most recent factor’s |
|
|
When |
Returns¶
A decorator suitable for function views, or — via
django.utils.decorators.method_decorator — class-based view methods.
Behaviour matrix¶
Request state |
|
|
Action |
|---|---|---|---|
User not authenticated |
any |
any |
View runs unchanged (your auth stack handles it). |
|
any |
any |
View runs unchanged. |
|
any |
any |
View runs unchanged. |
Active factors >= required |
met |
met |
View runs unchanged. |
User has keys but none active |
any |
any |
Redirect to |
|
any |
any |
Flash warning, redirect to |
Active factors < required (with |
unmet |
unmet |
Flash warning, redirect to |
|
met |
met |
View runs unchanged. |
|
met |
met |
View runs; flash one-off info banner. |
Example — function view¶
from django.contrib.auth.decorators import login_required
from multifactor.decorators import multifactor_protected
@login_required
@multifactor_protected(factors=1, max_age=30 * 60, user_filter={"is_staff": True})
def admin_dashboard(request): ...
Example — class-based view¶
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView
from multifactor.decorators import multifactor_protected
@method_decorator(multifactor_protected(factors=1), name="dispatch")
class Billing(TemplateView):
template_name = "billing.html"
Example — entire URL tree¶
from decorator_include import decorator_include
from multifactor.decorators import multifactor_protected
urlpatterns = [
path(
"admin/",
decorator_include(multifactor_protected(factors=1), admin.site.urls),
),
]
See also¶
Guide: protecting views — narrative version.
Mixins reference — class-based alternative.
Source:
multifactor/decorators.py.