Ejemplo n.º 1
0
 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)
Ejemplo n.º 2
0
 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")
Ejemplo n.º 3
0
 def test_principals(self):
     policy = IPAuthenticationPolicy(["123.123.0.0/16"], principals="test")
     # Addresses outside the range don't get metadata set
     request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"})
     self.assertEqual(policy.effective_principals(request), [Everyone])
     # Addresses inside the range do get metadata set
     request = DummyRequest(environ={"REMOTE_ADDR": "123.123.0.1"})
     self.assertEqual(policy.effective_principals(request),
                      [Everyone, "test"])
     policy.userid = "user"
     self.assertEqual(policy.effective_principals(request),
                      ["user", Everyone, Authenticated, "test"])
Ejemplo n.º 4
0
    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])
Ejemplo n.º 5
0
 def test_remember_forget(self):
     policy = IPAuthenticationPolicy(["123.123.0.0/16"], "user")
     request = DummyRequest(environ={"REMOTE_ADDR": "192.168.0.1"})
     self.assertEqual(policy.remember(request, "user"), [])
     self.assertEqual(policy.forget(request), [])