diff --git a/src/auth/identification.py b/src/auth/identification.py index 7d405cbb..9fd64a4e 100644 --- a/src/auth/identification.py +++ b/src/auth/identification.py @@ -15,6 +15,10 @@ class Identification(metaclass=abc.ABCMeta): def identify(self, request_handler): pass + @abc.abstractmethod + def identify_for_audit(self, request_handler): + pass + class AuthBasedIdentification(Identification): def __init__(self, authentication_provider) -> None: @@ -24,9 +28,11 @@ def identify(self, request_handler): current_user = self._authentication_provider.get_username(request_handler) if not current_user: raise Exception('Not authenticated') - return current_user + def identify_for_audit(self, request_handler): + return self.identify(request_handler) + class IpBasedIdentification(Identification): EXPIRES_DAYS = 14 @@ -69,6 +75,12 @@ def identify(self, request_handler): return new_id + def identify_for_audit(self, request_handler): + remote_ip = request_handler.request.remote_ip + if (remote_ip in self._trusted_ips) and (self._user_header_name): + return request_handler.request.headers.get(self._user_header_name, None) + return None + def _resolve_ip(self, request_handler): proxied_ip = tornado_utils.get_proxied_ip(request_handler) if proxied_ip: diff --git a/src/tests/audit_utils_test.py b/src/tests/audit_utils_test.py index 086ed8fe..04cf9dfb 100644 --- a/src/tests/audit_utils_test.py +++ b/src/tests/audit_utils_test.py @@ -3,16 +3,15 @@ from tests.test_utils import mock_object from utils import audit_utils, os_utils - +from auth.identification import AuthBasedIdentification def mock_request_handler(ip=None, proxy_username=None, auth_username=None, proxied_ip=None): handler_mock = mock_object() handler_mock.application = mock_object() - handler_mock.application.auth = mock_object() - - handler_mock.application.auth.get_username = lambda x: auth_username - + + handler_mock.application.identification = mock_object() + handler_mock.application.identification.identify_for_audit = lambda x: auth_username handler_mock.request = mock_object() handler_mock.request.headers = {} if proxy_username: diff --git a/src/tests/ip_idenfication_test.py b/src/tests/ip_idenfication_test.py index 4b163d24..82958b72 100644 --- a/src/tests/ip_idenfication_test.py +++ b/src/tests/ip_idenfication_test.py @@ -13,6 +13,7 @@ def mock_request_handler(ip=None, x_forwarded_for=None, x_real_ip=None, saved_to handler_mock.application = mock_object() handler_mock.application.auth = TornadoAuth(None) + handler_mock.application.identification = IpBasedIdentification(['127.0.0.1'], user_header_name) handler_mock.request = mock_object() handler_mock.request.headers = {} diff --git a/src/utils/audit_utils.py b/src/utils/audit_utils.py index 51c74ce1..724f2d9b 100644 --- a/src/utils/audit_utils.py +++ b/src/utils/audit_utils.py @@ -18,8 +18,7 @@ def get_all_audit_names(request_handler): result = {} - auth = request_handler.application.auth - auth_username = auth.get_username(request_handler) + auth_username = request_handler.application.identification.identify_for_audit(request_handler) if auth_username: result[AUTH_USERNAME] = auth_username