mitmproxy/test/http/test_authentication.py

123 lines
3.6 KiB
Python
Raw Normal View History

import binascii
2015-08-01 12:49:15 +00:00
from netlib import odict, http, tutils
2015-07-14 21:02:14 +00:00
from netlib.http import authentication
2013-03-02 21:37:28 +00:00
def test_parse_http_basic_auth():
vals = ("basic", "foo", "bar")
assert http.authentication.parse_http_basic_auth(
http.authentication.assemble_http_basic_auth(*vals)
) == vals
assert not http.authentication.parse_http_basic_auth("")
assert not http.authentication.parse_http_basic_auth("foo bar")
v = "basic " + binascii.b2a_base64("foo")
assert not http.authentication.parse_http_basic_auth(v)
2013-03-02 21:37:28 +00:00
class TestPassManNonAnon:
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:
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")
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:
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:
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:
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")
2013-03-02 21:37:28 +00:00
h = odict.ODictCaseless()
assert ba.auth_challenge_headers()
assert not ba.authenticate(h)
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
hdrs = odict.ODictCaseless()
vals = ("basic", "foo", "bar")
hdrs[ba.AUTH_HEADER] = [authentication.assemble_http_basic_auth(*vals)]
2013-03-02 21:37:28 +00:00
assert ba.authenticate(hdrs)
ba.clean(hdrs)
assert not ba.AUTH_HEADER in hdrs
hdrs[ba.AUTH_HEADER] = [""]
assert not ba.authenticate(hdrs)
hdrs[ba.AUTH_HEADER] = ["foo"]
assert not ba.authenticate(hdrs)
vals = ("foo", "foo", "bar")
hdrs[ba.AUTH_HEADER] = [authentication.assemble_http_basic_auth(*vals)]
2013-03-02 21:37:28 +00:00
assert not ba.authenticate(hdrs)
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")
hdrs[ba.AUTH_HEADER] = [authentication.assemble_http_basic_auth(*vals)]
2013-03-02 21:37:28 +00:00
assert not ba.authenticate(hdrs)
2013-12-08 00:35:42 +00:00
class Bunch:
pass
2013-12-08 00:35:42 +00:00
2013-12-08 00:35:42 +00:00
class TestAuthAction:
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)
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)
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)
assert m.authenticator