309
309
self.old_server = self.get_secondary_server()
312
class BasicAuthRequestHandler(TestingHTTPRequestHandler):
312
class AbstractBasicAuthRequestHandler(TestingHTTPRequestHandler):
313
313
"""Requires a basic authentication to process requests.
315
315
This is intended to be used with a server that always and
316
316
only use basic authentication.
319
# The following attribute should be set dy daughter classes
320
_auth_header_sent = None
321
_auth_header_recv = None
322
_auth_error_code = None
319
324
def do_GET(self):
320
325
tcs = self.server.test_case_server
321
326
if tcs.auth_scheme == 'basic':
322
auth_header = self.headers.get('Authorization')
327
auth_header = self.headers.get(self._auth_header_recv)
323
328
authorized = False
324
329
if auth_header and auth_header.lower().startswith('basic '):
325
330
coded_auth = auth_header[len('Basic '):]
326
331
user, password = coded_auth.decode('base64').split(':')
327
332
authorized = tcs.authorized(user, password)
328
333
if not authorized:
329
self.send_response(401)
330
self.send_header('www-authenticate',
334
self.send_response(self._auth_error_code)
335
self.send_header(self._auth_header_sent,
331
336
'Basic realm="Thou should not pass"')
332
337
self.end_headers()
362
367
return expected_password is not None and password == expected_password
370
class BasicAuthRequestHandler(AbstractBasicAuthRequestHandler,
371
FakeProxyRequestHandler):
372
"""Requires a basic authentication to process requests.
374
Note: Each of the inherited request handler overrides
375
different parts of processing in a compatible way, so it is
376
okay to inherit from both.
379
_auth_header_sent = 'WWW-Authenticate'
380
_auth_header_recv = 'Authorization'
381
_auth_error_code = 401
365
384
class BasicAuthHTTPServer(AuthHTTPServer):
366
385
"""An HTTP server requiring basic authentication"""
368
387
def __init__(self):
369
388
AuthHTTPServer.__init__(self, BasicAuthRequestHandler, 'basic')
391
class ProxyBasicAuthRequestHandler(AbstractBasicAuthRequestHandler,
392
FakeProxyRequestHandler):
393
"""Requires a basic authentication to proxy requests.
395
Note: Each of the inherited request handler overrides
396
different parts of processing in a compatible way, so it is
397
okay to inherit from both.
400
_auth_header_sent = 'Proxy-Authenticate'
401
_auth_header_recv = 'Proxy-Authorization'
402
_auth_error_code = 407
405
class ProxyBasicAuthHTTPServer(AuthHTTPServer):
406
"""An HTTP server requiring basic authentication"""
409
AuthHTTPServer.__init__(self, ProxyBasicAuthRequestHandler, 'basic')