Skip to content

Commit

Permalink
Merge pull request #293 from oktorok/bug_user_header_name
Browse files Browse the repository at this point in the history
Bug user_header_name
  • Loading branch information
bugy authored Apr 19, 2020
2 parents f9ec58a + c69d2fe commit 519eeb4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
14 changes: 13 additions & 1 deletion src/auth/identification.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 4 additions & 5 deletions src/tests/audit_utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions src/tests/ip_idenfication_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
3 changes: 1 addition & 2 deletions src/utils/audit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 519eeb4

Please sign in to comment.