def test_callbacks(self): def get_userid(ipaddr): if str(ipaddr).startswith('192'): return 'LAN-user' if str(ipaddr).startswith('127'): return 'localhost-user' return None def get_principals(userid, ipaddr): principals = { 'LAN-user': ['view'], 'localhost-user': ['view', 'edit'], } return principals.get(userid, []) policy = IPAuthenticationPolicy("all", get_userid=get_userid, get_principals=get_principals) # Addresses outside the range don't authenticate request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"}) self.assertEqual(policy.unauthenticated_userid(request), "LAN-user") self.assertEqual(policy.authenticated_userid(request), "LAN-user") self.assertEqual(policy.effective_principals(request), ["LAN-user", Everyone, Authenticated, 'view']) request = DummyRequest(environ={"REMOTE_ADDR": "127.0.0.1"}) self.assertEqual(policy.unauthenticated_userid(request), "localhost-user") self.assertEqual(policy.authenticated_userid(request), "localhost-user") self.assertEqual(policy.effective_principals(request), ["localhost-user", Everyone, Authenticated, 'view', 'edit']) request = DummyRequest(environ={"REMOTE_ADDR": "86.8.8.8"}) self.assertEqual(policy.unauthenticated_userid(request), None) self.assertEqual(policy.authenticated_userid(request), None) self.assertEqual(policy.effective_principals(request), [Everyone])
def test_remote_addr(self): policy = IPAuthenticationPolicy(["123.123.0.0/16"], "user") # Addresses outside the range don't authenticate request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"}) self.assertEqual(policy.authenticated_userid(request), None) # Addresses inside the range do authenticate request = DummyRequest(environ={"REMOTE_ADDR": "123.123.0.1"}) self.assertEqual(policy.authenticated_userid(request), "user") request = DummyRequest(environ={"REMOTE_ADDR": "123.123.1.2"}) self.assertEqual(policy.authenticated_userid(request), "user")
def test_remote_addr(self): policy = IPAuthenticationPolicy(["123.123.0.0/16"], "user") # Addresses outside the range don't authenticate request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"}) self.assertEquals(policy.authenticated_userid(request), None) # Addresses inside the range do authenticate request = DummyRequest(environ={"REMOTE_ADDR": "123.123.0.1"}) self.assertEquals(policy.authenticated_userid(request), "user") request = DummyRequest(environ={"REMOTE_ADDR": "123.123.1.2"}) self.assertEquals(policy.authenticated_userid(request), "user")
def test_noncontiguous_ranges(self): policy = IPAuthenticationPolicy(["123.123.0.0/16", "124.124.1.0/24"], "user") # Addresses outside the range don't authenticate request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"}) self.assertEqual(policy.authenticated_userid(request), None) request = DummyRequest(environ={"REMOTE_ADDR": "124.124.0.1"}) self.assertEqual(policy.authenticated_userid(request), None) # Addresses inside the range do authenticate request = DummyRequest(environ={"REMOTE_ADDR": "123.123.0.1"}) self.assertEqual(policy.authenticated_userid(request), "user") request = DummyRequest(environ={"REMOTE_ADDR": "124.124.1.2"}) self.assertEqual(policy.authenticated_userid(request), "user")
def test_x_forwarded_for(self): policy = IPAuthenticationPolicy(["123.123.0.0/16"], "user", proxies=["124.124.0.0/24"]) # Requests without X-Forwarded-For work as normal request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"}) self.assertEqual(policy.authenticated_userid(request), None) request = DummyRequest(environ={"REMOTE_ADDR": "123.123.0.1"}) self.assertEqual(policy.authenticated_userid(request), "user") # Requests with untrusted X-Forwarded-For don't authenticate request = DummyRequest(environ={ "REMOTE_ADDR": "192.168.0.1", "HTTP_X_FORWARDED_FOR": "123.123.0.1" }) self.assertEqual(policy.authenticated_userid(request), None) # Requests from single trusted proxy do authenticate request = DummyRequest(environ={ "REMOTE_ADDR": "124.124.0.1", "HTTP_X_FORWARDED_FOR": "123.123.0.1" }) self.assertEqual(policy.authenticated_userid(request), "user") # Requests from chain of trusted proxies do authenticate request = DummyRequest( environ={ "REMOTE_ADDR": "124.124.0.2", "HTTP_X_FORWARDED_FOR": "123.123.0.1, 124.124.0.1" }) self.assertEqual(policy.authenticated_userid(request), "user") # Requests with untrusted proxy in chain don't authenticate request = DummyRequest( environ={ "REMOTE_ADDR": "124.124.0.1", "HTTP_X_FORWARDED_FOR": "123.123.0.1, 192.168.0.1" }) self.assertEqual(policy.authenticated_userid(request), None)
def test_x_forwarded_for(self): policy = IPAuthenticationPolicy(["123.123.0.0/16"], "user", proxies=["124.124.0.0/24"]) # Requests without X-Forwarded-For work as normal request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"}) self.assertEqual(policy.authenticated_userid(request), None) request = DummyRequest(environ={"REMOTE_ADDR": "123.123.0.1"}) self.assertEqual(policy.authenticated_userid(request), "user") # Requests with untrusted X-Forwarded-For don't authenticate request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1", "HTTP_X_FORWARDED_FOR": "123.123.0.1"}) self.assertEqual(policy.authenticated_userid(request), None) # Requests from single trusted proxy do authenticate request = DummyRequest(environ={"REMOTE_ADDR": "124.124.0.1", "HTTP_X_FORWARDED_FOR": "123.123.0.1"}) self.assertEqual(policy.authenticated_userid(request), "user") # Requests from chain of trusted proxies do authenticate request = DummyRequest( environ={ "REMOTE_ADDR": "124.124.0.2", "HTTP_X_FORWARDED_FOR": "123.123.0.1, 124.124.0.1"}) self.assertEqual(policy.authenticated_userid(request), "user") # Requests with untrusted proxy in chain don't authenticate request = DummyRequest( environ={ "REMOTE_ADDR": "124.124.0.1", "HTTP_X_FORWARDED_FOR": "123.123.0.1, 192.168.0.1"}) self.assertEqual(policy.authenticated_userid(request), None)
def test_callbacks(self): def get_userid(ipaddr): if str(ipaddr).startswith('192'): return 'LAN-user' if str(ipaddr).startswith('127'): return 'localhost-user' return None def get_principals(userid, ipaddr): principals = { 'LAN-user': ['view'], 'localhost-user': ['view', 'edit'], } return principals.get(userid, []) policy = IPAuthenticationPolicy("all", get_userid=get_userid, get_principals=get_principals) # Addresses outside the range don't authenticate request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"}) self.assertEqual(policy.unauthenticated_userid(request), "LAN-user") self.assertEqual(policy.authenticated_userid(request), "LAN-user") self.assertEqual(policy.effective_principals(request), ["LAN-user", Everyone, Authenticated, 'view']) request = DummyRequest(environ={"REMOTE_ADDR": "127.0.0.1"}) self.assertEqual(policy.unauthenticated_userid(request), "localhost-user") self.assertEqual(policy.authenticated_userid(request), "localhost-user") self.assertEqual( policy.effective_principals(request), ["localhost-user", Everyone, Authenticated, 'view', 'edit']) request = DummyRequest(environ={"REMOTE_ADDR": "86.8.8.8"}) self.assertEqual(policy.unauthenticated_userid(request), None) self.assertEqual(policy.authenticated_userid(request), None) self.assertEqual(policy.effective_principals(request), [Everyone])