mirror of
https://github.com/jazzband/django-axes.git
synced 2026-03-16 22:30:23 +00:00
use AXES_CLIENT_IP_CALLABLE in get_client_ip_address
This commit is contained in:
parent
7ae0af2a1e
commit
c8bfbe603a
1 changed files with 21 additions and 4 deletions
|
|
@ -6,9 +6,9 @@ from typing import Callable, Optional, Type, Union
|
|||
from urllib.parse import urlencode
|
||||
|
||||
import ipware.ip
|
||||
from django.core.cache import caches, BaseCache
|
||||
from django.core.cache import BaseCache, caches
|
||||
from django.http import HttpRequest, HttpResponse, JsonResponse, QueryDict
|
||||
from django.shortcuts import render, redirect
|
||||
from django.shortcuts import redirect, render
|
||||
from django.utils.module_loading import import_string
|
||||
|
||||
from axes.conf import settings
|
||||
|
|
@ -152,10 +152,27 @@ def get_client_ip_address(request: HttpRequest) -> str:
|
|||
"""
|
||||
Get client IP address as configured by the user.
|
||||
|
||||
The django-ipware package is used for address resolution
|
||||
and parameters can be configured in the Axes package.
|
||||
The order of preference for address resolution is as follows:
|
||||
|
||||
1. If configured, use ``AXES_CLIENT_IP_CALLABLE``, and supply ``request`` as argument
|
||||
2. Use django-ipware package (parameters can be configured in the Axes package)
|
||||
|
||||
:param request: incoming Django ``HttpRequest`` or similar object from authentication backend or other source
|
||||
"""
|
||||
|
||||
if settings.AXES_CLIENT_IP_CALLABLE:
|
||||
log.debug("Using settings.AXES_CLIENT_IP_CALLABLE to get username")
|
||||
|
||||
if callable(settings.AXES_CLIENT_IP_CALLABLE):
|
||||
return settings.AXES_CLIENT_IP_CALLABLE( # pylint: disable=not-callable
|
||||
request
|
||||
)
|
||||
if isinstance(settings.AXES_CLIENT_IP_CALLABLE, str):
|
||||
return import_string(settings.AXES_CLIENT_IP_CALLABLE)(request)
|
||||
raise TypeError(
|
||||
"settings.AXES_CLIENT_IP_CALLABLE needs to be a string, callable, or None."
|
||||
)
|
||||
|
||||
client_ip_address, _ = ipware.ip.get_client_ip(
|
||||
request,
|
||||
proxy_order=settings.AXES_PROXY_ORDER,
|
||||
|
|
|
|||
Loading…
Reference in a new issue