Transition to new library for finding IPs from the Django request

This commit is contained in:
Trenton H 2023-05-11 08:58:32 -07:00
parent 311c0ba4f1
commit 17144c45e5
3 changed files with 16 additions and 16 deletions

View File

@ -21,7 +21,6 @@ django-extensions = "*"
django-filter = "~=22.1"
djangorestframework = "~=3.14"
djangorestframework-guardian = "*"
django-ipware = "*"
filelock = "*"
gunicorn = "*"
imap-tools = "*"
@ -33,6 +32,7 @@ python-gnupg = "*"
python-dotenv = "*"
python-dateutil = "*"
python-magic = "*"
python-ipware = "*"
psycopg2 = "*"
rapidfuzz = "*"
redis = {extras = ["hiredis"], version = "*"}
@ -51,7 +51,6 @@ channels = "~=3.0"
channels-redis = "*"
uvicorn = {extras = ["standard"], version = "*"}
concurrent-log-handler = "*"
pyzbar = "*"
mysqlclient = "*"
celery = {extras = ["redis"], version = "*"}

18
Pipfile.lock generated
View File

@ -1,7 +1,7 @@
{
"_meta": {
"hash": {
"sha256": "8fecf74dce093fa66c1027ae9230da1afb1c61d80eab7264823884e005e7284b"
"sha256": "6aeae592dcf1d7737a6cb6886db1ff7387a9daf3796f782531d25f2e22d608d9"
},
"pipfile-spec": 6,
"requires": {},
@ -543,14 +543,6 @@
"index": "pypi",
"version": "==2.4.0"
},
"django-ipware": {
"hashes": [
"sha256:4fa5607ee85e12ee5e158bc7569ff1e134fb1579681aa1ff3f0ed04be21be153",
"sha256:80b52a3f571a371519cc552798f1015b934dd5dd7738bfad87e101e861bd21b8"
],
"index": "pypi",
"version": "==5.0.0"
},
"djangorestframework": {
"hashes": [
"sha256:579a333e6256b09489cbe0a067e66abe55c6595d8926be6b99423786334350c8",
@ -1301,6 +1293,14 @@
"index": "pypi",
"version": "==0.5.0"
},
"python-ipware": {
"hashes": [
"sha256:01b9fa589521c29d7573f69fc4855c6b95687f0029601b78fffef1eb17f1de27",
"sha256:ee84cd16c2cf862faae197ad5f8fae6c75e4b1f40bb13357944a5d63ddc2a373"
],
"index": "pypi",
"version": "==0.9.0"
},
"python-magic": {
"hashes": [
"sha256:c1ba14b08e4a5f5c31a302b7721239695b2f0f058d125bd5ce1ee36b9d9d3c3b",

View File

@ -1,24 +1,25 @@
import logging
from django.conf import settings
from ipware import get_client_ip
from ipware import IpWare
logger = logging.getLogger("paperless.auth")
# https://docs.djangoproject.com/en/4.1/ref/contrib/auth/#django.contrib.auth.signals.user_login_failed
def handle_failed_login(sender, credentials, request, **kwargs):
client_ip, is_routable = get_client_ip(
request,
proxy_trusted_ips=settings.TRUSTED_PROXIES,
ipware = IpWare(proxy_trusted_list=settings.TRUSTED_PROXIES)
client_ip, _ = ipware.get_client_ip(
meta=request.META,
)
if client_ip is None:
logger.info(
f"Login failed for user `{credentials['username']}`."
" Unable to determine IP address.",
)
else:
if is_routable:
if client_ip.is_global:
# We got the client's IP address
logger.info(
f"Login failed for user `{credentials['username']}`"