2015-07-15 20:32:14 +00:00
|
|
|
import binascii
|
|
|
|
|
2015-09-05 16:15:47 +00:00
|
|
|
from netlib import tutils
|
|
|
|
from netlib.http import authentication, Headers
|
2013-03-02 21:37:28 +00:00
|
|
|
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2015-07-15 20:32:14 +00:00
|
|
|
def test_parse_http_basic_auth():
|
|
|
|
vals = ("basic", "foo", "bar")
|
2015-09-05 16:15:47 +00:00
|
|
|
assert authentication.parse_http_basic_auth(
|
|
|
|
authentication.assemble_http_basic_auth(*vals)
|
2015-07-15 20:32:14 +00:00
|
|
|
) == vals
|
2015-09-05 16:15:47 +00:00
|
|
|
assert not authentication.parse_http_basic_auth("")
|
|
|
|
assert not authentication.parse_http_basic_auth("foo bar")
|
2015-07-15 20:32:14 +00:00
|
|
|
v = "basic " + binascii.b2a_base64("foo")
|
2015-09-05 16:15:47 +00:00
|
|
|
assert not authentication.parse_http_basic_auth(v)
|
2015-07-15 20:32:14 +00:00
|
|
|
|
|
|
|
|
2013-03-02 21:37:28 +00:00
|
|
|
class TestPassManNonAnon:
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2013-03-02 21:37:28 +00:00
|
|
|
def test_simple(self):
|
2015-07-14 21:02:14 +00:00
|
|
|
p = authentication.PassManNonAnon()
|
2013-03-02 21:37:28 +00:00
|
|
|
assert not p.test("", "")
|
|
|
|
assert p.test("user", "")
|
|
|
|
|
|
|
|
|
|
|
|
class TestPassManHtpasswd:
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2013-03-02 21:37:28 +00:00
|
|
|
def test_file_errors(self):
|
2015-05-30 00:02:58 +00:00
|
|
|
tutils.raises(
|
|
|
|
"malformed htpasswd file",
|
2015-07-14 21:02:14 +00:00
|
|
|
authentication.PassManHtpasswd,
|
2015-05-30 00:02:58 +00:00
|
|
|
tutils.test_data.path("data/server.crt"))
|
2013-03-02 21:37:28 +00:00
|
|
|
|
|
|
|
def test_simple(self):
|
2015-07-14 21:02:14 +00:00
|
|
|
pm = authentication.PassManHtpasswd(tutils.test_data.path("data/htpasswd"))
|
2013-03-02 21:37:28 +00:00
|
|
|
|
|
|
|
vals = ("basic", "test", "test")
|
2015-07-15 20:32:14 +00:00
|
|
|
authentication.assemble_http_basic_auth(*vals)
|
2013-03-02 21:37:28 +00:00
|
|
|
assert pm.test("test", "test")
|
|
|
|
assert not pm.test("test", "foo")
|
|
|
|
assert not pm.test("foo", "test")
|
|
|
|
assert not pm.test("test", "")
|
|
|
|
assert not pm.test("", "")
|
|
|
|
|
|
|
|
|
|
|
|
class TestPassManSingleUser:
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2013-03-02 21:37:28 +00:00
|
|
|
def test_simple(self):
|
2015-07-14 21:02:14 +00:00
|
|
|
pm = authentication.PassManSingleUser("test", "test")
|
2013-03-02 21:37:28 +00:00
|
|
|
assert pm.test("test", "test")
|
|
|
|
assert not pm.test("test", "foo")
|
|
|
|
assert not pm.test("foo", "test")
|
|
|
|
|
|
|
|
|
|
|
|
class TestNullProxyAuth:
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2013-03-02 21:37:28 +00:00
|
|
|
def test_simple(self):
|
2015-07-14 21:02:14 +00:00
|
|
|
na = authentication.NullProxyAuth(authentication.PassManNonAnon())
|
2013-03-02 21:37:28 +00:00
|
|
|
assert not na.auth_challenge_headers()
|
|
|
|
assert na.authenticate("foo")
|
|
|
|
na.clean({})
|
|
|
|
|
|
|
|
|
|
|
|
class TestBasicProxyAuth:
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2013-03-02 21:37:28 +00:00
|
|
|
def test_simple(self):
|
2015-07-14 21:02:14 +00:00
|
|
|
ba = authentication.BasicProxyAuth(authentication.PassManNonAnon(), "test")
|
2015-09-05 16:15:47 +00:00
|
|
|
headers = Headers()
|
2013-03-02 21:37:28 +00:00
|
|
|
assert ba.auth_challenge_headers()
|
2015-09-05 16:15:47 +00:00
|
|
|
assert not ba.authenticate(headers)
|
2013-03-02 21:37:28 +00:00
|
|
|
|
|
|
|
def test_authenticate_clean(self):
|
2015-07-14 21:02:14 +00:00
|
|
|
ba = authentication.BasicProxyAuth(authentication.PassManNonAnon(), "test")
|
2013-03-02 21:37:28 +00:00
|
|
|
|
2015-09-05 16:15:47 +00:00
|
|
|
headers = Headers()
|
2013-03-02 21:37:28 +00:00
|
|
|
vals = ("basic", "foo", "bar")
|
2015-09-05 16:15:47 +00:00
|
|
|
headers[ba.AUTH_HEADER] = authentication.assemble_http_basic_auth(*vals)
|
|
|
|
assert ba.authenticate(headers)
|
2013-03-02 21:37:28 +00:00
|
|
|
|
2015-09-05 16:15:47 +00:00
|
|
|
ba.clean(headers)
|
|
|
|
assert not ba.AUTH_HEADER in headers
|
2013-03-02 21:37:28 +00:00
|
|
|
|
2015-09-05 16:15:47 +00:00
|
|
|
headers[ba.AUTH_HEADER] = ""
|
|
|
|
assert not ba.authenticate(headers)
|
2013-03-02 21:37:28 +00:00
|
|
|
|
2015-09-05 16:15:47 +00:00
|
|
|
headers[ba.AUTH_HEADER] = "foo"
|
|
|
|
assert not ba.authenticate(headers)
|
2013-03-02 21:37:28 +00:00
|
|
|
|
|
|
|
vals = ("foo", "foo", "bar")
|
2015-09-05 16:15:47 +00:00
|
|
|
headers[ba.AUTH_HEADER] = authentication.assemble_http_basic_auth(*vals)
|
|
|
|
assert not ba.authenticate(headers)
|
2013-03-02 21:37:28 +00:00
|
|
|
|
2015-07-14 21:02:14 +00:00
|
|
|
ba = authentication.BasicProxyAuth(authentication.PassMan(), "test")
|
2013-03-02 21:37:28 +00:00
|
|
|
vals = ("basic", "foo", "bar")
|
2015-09-05 16:15:47 +00:00
|
|
|
headers[ba.AUTH_HEADER] = authentication.assemble_http_basic_auth(*vals)
|
|
|
|
assert not ba.authenticate(headers)
|
2013-03-02 21:37:28 +00:00
|
|
|
|
2013-12-08 00:35:42 +00:00
|
|
|
|
2015-05-27 09:18:54 +00:00
|
|
|
class Bunch:
|
|
|
|
pass
|
2013-12-08 00:35:42 +00:00
|
|
|
|
2013-12-08 01:24:00 +00:00
|
|
|
|
2013-12-08 00:35:42 +00:00
|
|
|
class TestAuthAction:
|
2015-05-27 09:18:54 +00:00
|
|
|
|
2013-12-08 00:35:42 +00:00
|
|
|
def test_nonanonymous(self):
|
|
|
|
m = Bunch()
|
2015-07-14 21:02:14 +00:00
|
|
|
aa = authentication.NonanonymousAuthAction(None, "authenticator")
|
2013-12-08 00:35:42 +00:00
|
|
|
aa(None, m, None, None)
|
2015-05-27 09:18:54 +00:00
|
|
|
assert m.authenticator
|
2013-12-08 00:35:42 +00:00
|
|
|
|
|
|
|
def test_singleuser(self):
|
|
|
|
m = Bunch()
|
2015-07-14 21:02:14 +00:00
|
|
|
aa = authentication.SingleuserAuthAction(None, "authenticator")
|
2013-12-08 00:35:42 +00:00
|
|
|
aa(None, m, "foo:bar", None)
|
2015-05-27 09:18:54 +00:00
|
|
|
assert m.authenticator
|
2013-12-08 00:35:42 +00:00
|
|
|
tutils.raises("invalid", aa, None, m, "foo", None)
|
|
|
|
|
|
|
|
def test_httppasswd(self):
|
|
|
|
m = Bunch()
|
2015-07-14 21:02:14 +00:00
|
|
|
aa = authentication.HtpasswdAuthAction(None, "authenticator")
|
2013-12-08 00:35:42 +00:00
|
|
|
aa(None, m, tutils.test_data.path("data/htpasswd"), None)
|
2015-05-27 09:18:54 +00:00
|
|
|
assert m.authenticator
|