Fix: Use source ip if user is not authenticated (#1379)

* Fix: Use source ip if user is not authenticated

* Fix lint

Co-authored-by: Adrià Casajús <adria.casajus@proton.ch>
This commit is contained in:
Adrià Casajús 2022-10-27 13:37:45 +02:00 committed by GitHub
parent f6463a5adc
commit a5056b3fcc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@ from datetime import timedelta
from functools import wraps
from typing import Callable, Any, Optional
from flask import request
from flask_login import current_user
from limits.storage import RedisStorage
from werkzeug import exceptions
@ -52,7 +53,10 @@ class _InnerLock:
return f(*args, **kwargs)
lock_value = str(uuid.uuid4())[:10]
lock_name = f"cl:{current_user.id}:{lock_suffix}"
if "id" in dir(current_user):
lock_name = f"cl:{current_user.id}:{lock_suffix}"
else:
lock_name = f"cl:{request.remote_addr}:{lock_suffix}"
self.acquire_lock(lock_name, lock_value)
try:
return f(*args, **kwargs)