diff --git a/modoboa/core/commands/templates/settings.py.tpl b/modoboa/core/commands/templates/settings.py.tpl index 8676049fb..d554da21a 100644 --- a/modoboa/core/commands/templates/settings.py.tpl +++ b/modoboa/core/commands/templates/settings.py.tpl @@ -196,14 +196,12 @@ REST_FRAMEWORK = { 'DEFAULT_THROTTLE_CLASSES': [ 'rest_framework.throttling.AnonRateThrottle', 'rest_framework.throttling.ScopedRateThrottle', - 'modoboa.lib.throttle.UserDosThrottle', - 'modoboa.lib.throttle.AnonDosThrottle' + 'modoboa.lib.throttle.UserDosThrottle' ], 'DEFAULT_THROTTLE_RATES': { 'anon': '100/hour', 'login': '3/minute', 'ddos': '100/minute', - 'anon_ddos': '10/minute', 'password_recovery_request': '6/hour', 'password_recovery_totp_check': '20/hour', 'password_recovery_apply': '20/hour' diff --git a/modoboa/lib/throttle.py b/modoboa/lib/throttle.py index b5142219c..03c8cf7c2 100644 --- a/modoboa/lib/throttle.py +++ b/modoboa/lib/throttle.py @@ -1,23 +1,17 @@ -from rest_framework.throttling import UserRateThrottle, AnonRateThrottle +from rest_framework.throttling import SimpleRateThrottle -class UserDosThrottle(UserRateThrottle): +class UserDosThrottle(SimpleRateThrottle): """Custom Throttle class for rest_framework. The throttling is applied on a per view basis for authentificated users.""" scope = "ddos" def get_cache_key(self, request, view): - return "throttle_{viewid}_{indent}".format( - viewid=id(view), - indent=self.get_indent(request) - ) + if request.user and request.user.is_authenticated: + ident = request.user.pk + else: + ident = self.get_ident(request) -class AnonDosThrottle(AnonRateThrottle): - """Custom Throttle class for rest_framework. The throttling is applied on a per view basis for visitors.""" - - scope = "anon_ddos" - - def get_cache_key(self, request, view): - return "throttle_{viewid}_{indent}".format( - viewid=id(view), - indent=self.get_indent(request) - ) + return self.cache_format % { + 'scope': id(view), + 'ident': ident + } \ No newline at end of file diff --git a/test_project/test_project/settings.py b/test_project/test_project/settings.py index 76f67e156..e120e2e95 100644 --- a/test_project/test_project/settings.py +++ b/test_project/test_project/settings.py @@ -189,14 +189,12 @@ REST_FRAMEWORK = { 'DEFAULT_THROTTLE_CLASSES': [ 'rest_framework.throttling.AnonRateThrottle', 'rest_framework.throttling.ScopedRateThrottle', - 'modoboa.lib.throttle.UserDosThrottle', - 'modoboa.lib.throttle.AnonDosThrottle' + 'modoboa.lib.throttle.UserDosThrottle' ], 'DEFAULT_THROTTLE_RATES': { 'anon': '100/hour', 'login': '3/minute', 'ddos': '100/minute', - 'anon_ddos': '10/minute', 'password_recovery_request': '6/hour', 'password_recovery_totp_check': '20/hour', 'password_recovery_apply': '20/hour'