| 1 |
import time |
| 2 |
import urllib |
| 3 |
|
| 4 |
import pytest |
| 5 |
|
| 6 |
import httplib2 |
| 7 |
import tests |
| 8 |
|
| 9 |
|
| 10 |
def test_credentials(): |
| 11 |
c = httplib2.Credentials() |
| 12 |
c.add("joe", "password") |
| 13 |
assert tuple(c.iter("bitworking.org"))[0] == ("joe", "password") |
| 14 |
assert tuple(c.iter(""))[0] == ("joe", "password") |
| 15 |
c.add("fred", "password2", "wellformedweb.org") |
| 16 |
assert tuple(c.iter("bitworking.org"))[0] == ("joe", "password") |
| 17 |
assert len(tuple(c.iter("bitworking.org"))) == 1 |
| 18 |
assert len(tuple(c.iter("wellformedweb.org"))) == 2 |
| 19 |
assert ("fred", "password2") in tuple(c.iter("wellformedweb.org")) |
| 20 |
c.clear() |
| 21 |
assert len(tuple(c.iter("bitworking.org"))) == 0 |
| 22 |
c.add("fred", "password2", "wellformedweb.org") |
| 23 |
assert ("fred", "password2") in tuple(c.iter("wellformedweb.org")) |
| 24 |
assert len(tuple(c.iter("bitworking.org"))) == 0 |
| 25 |
assert len(tuple(c.iter(""))) == 0 |
| 26 |
|
| 27 |
|
| 28 |
def test_basic(): |
| 29 |
# Test Basic Authentication |
| 30 |
http = httplib2.Http() |
| 31 |
password = tests.gen_password() |
| 32 |
handler = tests.http_reflect_with_auth(allow_scheme="basic", allow_credentials=(("joe", password),)) |
| 33 |
with tests.server_request(handler, request_count=3) as uri: |
| 34 |
response, content = http.request(uri, "GET") |
| 35 |
assert response.status == 401 |
| 36 |
http.add_credentials("joe", password) |
| 37 |
response, content = http.request(uri, "GET") |
| 38 |
assert response.status == 200 |
| 39 |
|
| 40 |
|
| 41 |
def test_basic_for_domain(): |
| 42 |
# Test Basic Authentication |
| 43 |
http = httplib2.Http() |
| 44 |
password = tests.gen_password() |
| 45 |
handler = tests.http_reflect_with_auth(allow_scheme="basic", allow_credentials=(("joe", password),)) |
| 46 |
with tests.server_request(handler, request_count=4) as uri: |
| 47 |
response, content = http.request(uri, "GET") |
| 48 |
assert response.status == 401 |
| 49 |
http.add_credentials("joe", password, "example.org") |
| 50 |
response, content = http.request(uri, "GET") |
| 51 |
assert response.status == 401 |
| 52 |
domain = urllib.parse.urlparse(uri)[1] |
| 53 |
http.add_credentials("joe", password, domain) |
| 54 |
response, content = http.request(uri, "GET") |
| 55 |
assert response.status == 200 |
| 56 |
|
| 57 |
|
| 58 |
def test_basic_two_credentials(): |
| 59 |
# Test Basic Authentication with multiple sets of credentials |
| 60 |
http = httplib2.Http() |
| 61 |
password1 = tests.gen_password() |
| 62 |
password2 = tests.gen_password() |
| 63 |
allowed = [("joe", password1)] # exploit shared mutable list |
| 64 |
handler = tests.http_reflect_with_auth(allow_scheme="basic", allow_credentials=allowed) |
| 65 |
with tests.server_request(handler, request_count=7) as uri: |
| 66 |
http.add_credentials("fred", password2) |
| 67 |
response, content = http.request(uri, "GET") |
| 68 |
assert response.status == 401 |
| 69 |
http.add_credentials("joe", password1) |
| 70 |
response, content = http.request(uri, "GET") |
| 71 |
assert response.status == 200 |
| 72 |
allowed[0] = ("fred", password2) |
| 73 |
response, content = http.request(uri, "GET") |
| 74 |
assert response.status == 200 |
| 75 |
|
| 76 |
|
| 77 |
def test_digest(): |
| 78 |
# Test that we support Digest Authentication |
| 79 |
http = httplib2.Http() |
| 80 |
password = tests.gen_password() |
| 81 |
handler = tests.http_reflect_with_auth(allow_scheme="digest", allow_credentials=(("joe", password),)) |
| 82 |
with tests.server_request(handler, request_count=3) as uri: |
| 83 |
response, content = http.request(uri, "GET") |
| 84 |
assert response.status == 401 |
| 85 |
http.add_credentials("joe", password) |
| 86 |
response, content = http.request(uri, "GET") |
| 87 |
assert response.status == 200, content.decode() |
| 88 |
|
| 89 |
|
| 90 |
def test_digest_next_nonce_nc(): |
| 91 |
# Test that if the server sets nextnonce that we reset |
| 92 |
# the nonce count back to 1 |
| 93 |
http = httplib2.Http() |
| 94 |
password = tests.gen_password() |
| 95 |
grenew_nonce = [None] |
| 96 |
handler = tests.http_reflect_with_auth( |
| 97 |
allow_scheme="digest", allow_credentials=(("joe", password),), out_renew_nonce=grenew_nonce, |
| 98 |
) |
| 99 |
with tests.server_request(handler, request_count=5) as uri: |
| 100 |
http.add_credentials("joe", password) |
| 101 |
response1, _ = http.request(uri, "GET") |
| 102 |
info = httplib2.auth._parse_authentication_info(response1) |
| 103 |
print("debug: response1 authentication-info: {}\nparsed: {}".format(response1.get("authentication-info"), info)) |
| 104 |
assert response1.status == 200 |
| 105 |
assert info.get("nc") == "00000001", info |
| 106 |
assert not info.get("digest", {}).get("nextnonce"), info |
| 107 |
response2, _ = http.request(uri, "GET") |
| 108 |
info2 = httplib2.auth._parse_authentication_info(response2) |
| 109 |
assert info2.get("nc") == "00000002", info2 |
| 110 |
grenew_nonce[0]() |
| 111 |
response3, content = http.request(uri, "GET") |
| 112 |
info3 = httplib2.auth._parse_authentication_info(response3) |
| 113 |
assert response3.status == 200 |
| 114 |
assert info3.get("nc") == "00000001", info3 |
| 115 |
|
| 116 |
|
| 117 |
def test_digest_auth_stale(): |
| 118 |
# Test that we can handle a nonce becoming stale |
| 119 |
http = httplib2.Http() |
| 120 |
password = tests.gen_password() |
| 121 |
grenew_nonce = [None] |
| 122 |
requests = [] |
| 123 |
handler = tests.http_reflect_with_auth( |
| 124 |
allow_scheme="digest", |
| 125 |
allow_credentials=(("joe", password),), |
| 126 |
out_renew_nonce=grenew_nonce, |
| 127 |
out_requests=requests, |
| 128 |
) |
| 129 |
with tests.server_request(handler, request_count=4) as uri: |
| 130 |
http.add_credentials("joe", password) |
| 131 |
response, _ = http.request(uri, "GET") |
| 132 |
assert response.status == 200 |
| 133 |
info = httplib2.auth._parse_www_authenticate(requests[0][1].headers, "www-authenticate") |
| 134 |
grenew_nonce[0]() |
| 135 |
response, _ = http.request(uri, "GET") |
| 136 |
assert response.status == 200 |
| 137 |
assert not response.fromcache |
| 138 |
assert getattr(response, "_stale_digest", False) |
| 139 |
info2 = httplib2.auth._parse_www_authenticate(requests[2][1].headers, "www-authenticate") |
| 140 |
nonce1 = info.get("digest", {}).get("nonce", "") |
| 141 |
nonce2 = info2.get("digest", {}).get("nonce", "") |
| 142 |
assert nonce1 != "" |
| 143 |
assert nonce2 != "" |
| 144 |
assert nonce1 != nonce2, (nonce1, nonce2) |
| 145 |
|
| 146 |
|
| 147 |
@pytest.mark.parametrize( |
| 148 |
"data", |
| 149 |
( |
| 150 |
({}, {}), |
| 151 |
({"www-authenticate": ""}, {}), |
| 152 |
( |
| 153 |
{"www-authenticate": 'Test realm="test realm" , foo=foo ,bar="bar", baz=baz,qux=qux'}, |
| 154 |
{"test": {"realm": "test realm", "foo": "foo", "bar": "bar", "baz": "baz", "qux": "qux"}}, |
| 155 |
), |
| 156 |
( |
| 157 |
{"www-authenticate": 'T*!%#st realm=to*!%#en, to*!%#en="quoted string"'}, |
| 158 |
{"t*!%#st": {"realm": "to*!%#en", "to*!%#en": "quoted string"}}, |
| 159 |
), |
| 160 |
({"www-authenticate": 'Test realm="a \\"test\\" realm"'}, {"test": {"realm": 'a "test" realm'}},), |
| 161 |
({"www-authenticate": 'Basic realm="me"'}, {"basic": {"realm": "me"}}), |
| 162 |
({"www-authenticate": 'Basic realm="me", algorithm="MD5"'}, {"basic": {"realm": "me", "algorithm": "MD5"}},), |
| 163 |
({"www-authenticate": 'Basic realm="me", algorithm=MD5'}, {"basic": {"realm": "me", "algorithm": "MD5"}},), |
| 164 |
({"www-authenticate": 'Basic realm="me",other="fred" '}, {"basic": {"realm": "me", "other": "fred"}},), |
| 165 |
({"www-authenticate": 'Basic REAlm="me" '}, {"basic": {"realm": "me"}}), |
| 166 |
( |
| 167 |
{"www-authenticate": 'Digest realm="digest1", qop="auth,auth-int", nonce="7102dd2", opaque="e9517f"'}, |
| 168 |
{"digest": {"realm": "digest1", "qop": "auth,auth-int", "nonce": "7102dd2", "opaque": "e9517f"}}, |
| 169 |
), |
| 170 |
# comma between schemas (glue for multiple headers with same name) |
| 171 |
( |
| 172 |
{"www-authenticate": 'Digest realm="2-comma-d", qop="auth-int", nonce="c0c8ff1", Basic realm="2-comma-b"'}, |
| 173 |
{"digest": {"realm": "2-comma-d", "qop": "auth-int", "nonce": "c0c8ff1"}, "basic": {"realm": "2-comma-b"}}, |
| 174 |
), |
| 175 |
# comma between schemas + WSSE (glue for multiple headers with same name) |
| 176 |
( |
| 177 |
{"www-authenticate": 'Digest realm="com3d", Basic realm="com3b", WSSE realm="com3w", profile="token"'}, |
| 178 |
{"digest": {"realm": "com3d"}, "basic": {"realm": "com3b"}, "wsse": {"realm": "com3w", "profile": "token"}}, |
| 179 |
), |
| 180 |
# FIXME |
| 181 |
# multiple syntax figures |
| 182 |
# ({'www-authenticate': |
| 183 |
# 'Digest realm="brig", qop \t=\t"\tauth,auth-int", nonce="(*)&^&$%#",opaque="5ccc"' + |
| 184 |
# ', Basic REAlm="zoo", WSSE realm="very", profile="UsernameToken"'}, |
| 185 |
# {'digest': {'realm': 'brig', 'qop': 'auth,auth-int', 'nonce': '(*)&^&$%#', 'opaque': '5ccc'}, |
| 186 |
# 'basic': {'realm': 'zoo'}, |
| 187 |
# 'wsse': {'realm': 'very', 'profile': 'UsernameToken'}}), |
| 188 |
# more quote combos |
| 189 |
( |
| 190 |
{"www-authenticate": 'Digest realm="myrealm", nonce="KBAA=3", algorithm=MD5, qop="auth", stale=true'}, |
| 191 |
{"digest": {"realm": "myrealm", "nonce": "KBAA=3", "algorithm": "MD5", "qop": "auth", "stale": "true"}}, |
| 192 |
), |
| 193 |
({"www-authenticate": "Basic param='single quote'"}, {"basic": {"param": "'single"}}), |
| 194 |
), |
| 195 |
ids=lambda data: str(data[0]), |
| 196 |
) |
| 197 |
@pytest.mark.parametrize("strict", (True, False), ids=("strict", "relax")) |
| 198 |
def test_parse_www_authenticate_correct(data, strict): |
| 199 |
headers, info = data |
| 200 |
# FIXME: move strict to parse argument |
| 201 |
httplib2.USE_WWW_AUTH_STRICT_PARSING = strict |
| 202 |
try: |
| 203 |
assert httplib2.auth._parse_www_authenticate(headers) == info |
| 204 |
finally: |
| 205 |
httplib2.USE_WWW_AUTH_STRICT_PARSING = 0 |
| 206 |
|
| 207 |
|
| 208 |
@pytest.mark.parametrize( |
| 209 |
"data", |
| 210 |
(({"www-authenticate": 'OAuth "Facebook Platform" "invalid_token" "Invalid OAuth access token."'}, None),), |
| 211 |
ids=lambda data: str(data[0]), |
| 212 |
) |
| 213 |
def test_parse_www_authenticate_malformed(data): |
| 214 |
# TODO: test (and fix) header value 'barbqwnbm-bb...:asd' leads to dead loop |
| 215 |
headers, info = data |
| 216 |
try: |
| 217 |
result = httplib2.auth._parse_www_authenticate(headers) |
| 218 |
except httplib2.error.MalformedHeader: |
| 219 |
assert info is None, "unexpected MalformedHeader" |
| 220 |
else: |
| 221 |
assert result == info |
| 222 |
assert info is not None, "expected parsing error" |
| 223 |
|
| 224 |
|
| 225 |
def test_parse_www_authenticate_complexity(): |
| 226 |
# TODO just use time.process_time() after python2 support is removed |
| 227 |
process_time = getattr(time, "process_time", time.time) |
| 228 |
|
| 229 |
def check(size): |
| 230 |
header = {"www-authenticate": 'scheme {0}key=value,{0}quoted="foo=bar"'.format(" \t" * size)} |
| 231 |
tbegin = process_time() |
| 232 |
result = httplib2.auth._parse_www_authenticate(header) |
| 233 |
tend = process_time() |
| 234 |
assert result == {"scheme": {"key": "value", "quoted": "foo=bar"}} |
| 235 |
elapsed_us = round((tend * 1e6) - (tbegin * 1e6), 0) |
| 236 |
return elapsed_us |
| 237 |
|
| 238 |
n1, n2, repeat = 50, 100, 7 |
| 239 |
time1 = min(check(n1) for _ in range(repeat)) |
| 240 |
time2 = min(check(n2) for _ in range(repeat)) |
| 241 |
speed1 = round(time1 / n1, 1) |
| 242 |
speed2 = round(time2 / n2, 1) |
| 243 |
expect2 = round(speed1 * (float(n2) / n1), 1) |
| 244 |
error = round(speed2 / expect2, 1) |
| 245 |
print("x{}: time={}us speed={} us/op".format(n1, time1, speed1)) |
| 246 |
print("x{}: time={}us speed={} us/op expected={} us/op error={}".format(n2, time2, speed2, expect2, error)) |
| 247 |
assert error < 2, "_parse_www_authenticate scales too fast" |
| 248 |
|
| 249 |
|
| 250 |
@tests.skip_benchmark |
| 251 |
@pytest.mark.parametrize( |
| 252 |
"data", |
| 253 |
( |
| 254 |
'Basic realm="me", algorithm="MD5"', |
| 255 |
'Digest realm="digest1", qop="auth,auth-int", nonce="7102dd2", opaque="e9517f"', |
| 256 |
'Digest realm="2-comma-d", qop="auth-int", nonce="c0c8ff1", Basic realm="2-comma-b"', |
| 257 |
"Bearer 0b79bab50daca910b000d4f1a2b675d604257e42", |
| 258 |
), |
| 259 |
) |
| 260 |
def test_benchmark_parse_www_authenticate(data): |
| 261 |
# TODO just use time.process_time() after python2 support is removed |
| 262 |
process_time = getattr(time, "process_time", time.time) |
| 263 |
|
| 264 |
def measure(header, loop): |
| 265 |
tbegin = process_time() |
| 266 |
for _ in range(loop): |
| 267 |
httplib2.auth._parse_www_authenticate(header) |
| 268 |
tend = process_time() |
| 269 |
elapsed_us = round((tend * 1e6) - (tbegin * 1e6), 0) |
| 270 |
return elapsed_us |
| 271 |
|
| 272 |
header = {"www-authenticate": data} |
| 273 |
min_time = 1e6 |
| 274 |
repeat = 7 |
| 275 |
|
| 276 |
# timeit.autorange |
| 277 |
loop = 1 |
| 278 |
while True: |
| 279 |
elapsed_us = measure(header, loop) |
| 280 |
if elapsed_us >= min_time: |
| 281 |
break |
| 282 |
elif elapsed_us < min_time / 10: |
| 283 |
k = 10.0 |
| 284 |
else: |
| 285 |
k = 1.2 |
| 286 |
loop = int(loop * k) + 1 |
| 287 |
|
| 288 |
fastest = min(measure(header, loop) for _ in range(repeat)) |
| 289 |
speed = int(fastest / loop) |
| 290 |
print("x{} time={}us speed={} us/op".format(loop, fastest, speed)) |
| 291 |
|
| 292 |
|
| 293 |
def test_digest_object(): |
| 294 |
credentials = ("joe", "password") |
| 295 |
host = None |
| 296 |
request_uri = "/test/digest/" |
| 297 |
headers = {} |
| 298 |
response = {"www-authenticate": 'Digest realm="myrealm", nonce="KBAA=35", algorithm=MD5, qop="auth"'} |
| 299 |
content = b"" |
| 300 |
|
| 301 |
d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None) |
| 302 |
d.request("GET", request_uri, headers, content, cnonce="33033375ec278a46") |
| 303 |
our_request = "authorization: " + headers["authorization"] |
| 304 |
working_request = ( |
| 305 |
'authorization: Digest username="joe", realm="myrealm", ' |
| 306 |
'nonce="KBAA=35", uri="/test/digest/"' + ', algorithm=MD5, response="de6d4a123b80801d0e94550411b6283f", ' |
| 307 |
'qop=auth, nc=00000001, cnonce="33033375ec278a46"' |
| 308 |
) |
| 309 |
assert our_request == working_request |
| 310 |
|
| 311 |
|
| 312 |
def test_digest_object_with_opaque(): |
| 313 |
credentials = ("joe", "password") |
| 314 |
host = None |
| 315 |
request_uri = "/digest/opaque/" |
| 316 |
headers = {} |
| 317 |
response = { |
| 318 |
"www-authenticate": 'Digest realm="myrealm", nonce="30352fd", algorithm=MD5, ' 'qop="auth", opaque="atestopaque"' |
| 319 |
} |
| 320 |
content = "" |
| 321 |
|
| 322 |
d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None) |
| 323 |
d.request("GET", request_uri, headers, content, cnonce="5ec2") |
| 324 |
our_request = "authorization: " + headers["authorization"] |
| 325 |
working_request = ( |
| 326 |
'authorization: Digest username="joe", realm="myrealm", ' |
| 327 |
'nonce="30352fd", uri="/digest/opaque/", algorithm=MD5' |
| 328 |
+ ', response="a1fab43041f8f3789a447f48018bee48", qop=auth, nc=00000001, ' |
| 329 |
'cnonce="5ec2", opaque="atestopaque"' |
| 330 |
) |
| 331 |
assert our_request == working_request |
| 332 |
|
| 333 |
|
| 334 |
def test_digest_object_stale(): |
| 335 |
credentials = ("joe", "password") |
| 336 |
host = None |
| 337 |
request_uri = "/digest/stale/" |
| 338 |
headers = {} |
| 339 |
response = httplib2.Response({}) |
| 340 |
response["www-authenticate"] = 'Digest realm="myrealm", nonce="bd669f", ' 'algorithm=MD5, qop="auth", stale=true' |
| 341 |
response.status = 401 |
| 342 |
content = b"" |
| 343 |
d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None) |
| 344 |
# Returns true to force a retry |
| 345 |
assert d.response(response, content) |
| 346 |
|
| 347 |
|
| 348 |
def test_digest_object_auth_info(): |
| 349 |
credentials = ("joe", "password") |
| 350 |
host = None |
| 351 |
request_uri = "/digest/nextnonce/" |
| 352 |
headers = {} |
| 353 |
response = httplib2.Response({}) |
| 354 |
response["www-authenticate"] = 'Digest realm="myrealm", nonce="barney", ' 'algorithm=MD5, qop="auth", stale=true' |
| 355 |
response["authentication-info"] = 'nextnonce="fred"' |
| 356 |
content = b"" |
| 357 |
d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None) |
| 358 |
# Returns true to force a retry |
| 359 |
assert not d.response(response, content) |
| 360 |
assert d.challenge["nonce"] == "fred" |
| 361 |
assert d.challenge["nc"] == 1 |
| 362 |
|
| 363 |
|
| 364 |
def test_wsse_algorithm(): |
| 365 |
digest = httplib2._wsse_username_token("d36e316282959a9ed4c89851497a717f", "2003-12-15T14:43:07Z", "taadtaadpstcsm") |
| 366 |
expected = "quR/EWLAV4xLf9Zqyw4pDmfV9OY=" |
| 367 |
assert expected == digest |
| 368 |
|
| 369 |
|
| 370 |
def test_wsse_invalid(): |
| 371 |
http = httplib2.Http() |
| 372 |
username = "user294" |
| 373 |
password = tests.gen_password() |
| 374 |
grenew_nonce = [None] |
| 375 |
requests = [] |
| 376 |
handler = tests.http_reflect_with_auth( |
| 377 |
allow_scheme="wsse", |
| 378 |
allow_credentials=((username, password),), |
| 379 |
out_renew_nonce=grenew_nonce, |
| 380 |
out_requests=requests, |
| 381 |
) |
| 382 |
http.add_credentials(username, "wrong" + password) |
| 383 |
with tests.server_request(handler, request_count=2) as uri: |
| 384 |
response, _ = http.request(uri) |
| 385 |
assert requests[0][1].status == 401 |
| 386 |
assert requests[1][0].headers["authorization"] == 'WSSE profile="UsernameToken"' |
| 387 |
assert requests[1][1].status == 401 |
| 388 |
assert response.status == 401 |
| 389 |
|
| 390 |
|
| 391 |
def test_wsse_ok(): |
| 392 |
http = httplib2.Http() |
| 393 |
username = "user314" |
| 394 |
password = tests.gen_password() |
| 395 |
grenew_nonce = [None] |
| 396 |
requests = [] |
| 397 |
handler = tests.http_reflect_with_auth( |
| 398 |
allow_scheme="wsse", |
| 399 |
allow_credentials=((username, password),), |
| 400 |
out_renew_nonce=grenew_nonce, |
| 401 |
out_requests=requests, |
| 402 |
) |
| 403 |
http.add_credentials(username, password) |
| 404 |
with tests.server_request(handler, request_count=2) as uri: |
| 405 |
response, _ = http.request(uri) |
| 406 |
assert requests[0][1].status == 401 |
| 407 |
assert requests[1][0].headers["authorization"] == 'WSSE profile="UsernameToken"' |
| 408 |
assert response.status == 200 |