Added requests interceptors support. IP addresses can now be banned.

This commit is contained in:
Daniele Maglie 2024-01-19 15:25:53 +01:00
parent f3ed518b65
commit 183fa3e27a
14 changed files with 196 additions and 208 deletions

View file

@ -0,0 +1,34 @@
from piracyshield_service.security.blacklist.exists_by_ip_address import SecurityBlacklistExistsByIPAddressService
from ioutils.errors import ErrorCode, ErrorMessage
class BlacklistInterceptor:
"""
Applies bans.
"""
security_blacklist_exists_by_ip_address_service = None
async def execute(self, r):
await self._prepare_modules()
if self.security_blacklist_exists_by_ip_address_service.execute(
ip_address = r.request.remote_ip
) == True:
raise BlacklistInterceptorException()
async def _prepare_modules(self):
self.security_blacklist_exists_by_ip_address_service = SecurityBlacklistExistsByIPAddressService()
class BlacklistInterceptorException(Exception):
"""
The IP address is temporary banned.
"""
status_code = 403
error_code = ErrorCode.IP_ADDRESS_BLACKLISTED
error_message = ErrorMessage.IP_ADDRESS_BLACKLISTED