Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions | Private Attributes
test.test_urllib2_localnet.DigestAuthHandler Class Reference

List of all members.

Public Member Functions

def __init__
def set_qop
def set_users
def set_realm
def handle_request

Private Member Functions

def _generate_nonce
def _create_auth_dict
def _validate_auth
def _return_auth_challenge

Private Attributes

 _request_num
 _nonces
 _users
 _realm_name
 _qop

Detailed Description

Handler for performing digest authentication.

Definition at line 78 of file test_urllib2_localnet.py.


Constructor & Destructor Documentation

Definition at line 81 of file test_urllib2_localnet.py.

00081 
00082     def __init__(self):
00083         self._request_num = 0
00084         self._nonces = []
00085         self._users = {}
00086         self._realm_name = "Test Realm"
00087         self._qop = "auth"

Here is the caller graph for this function:


Member Function Documentation

def test.test_urllib2_localnet.DigestAuthHandler._create_auth_dict (   self,
  auth_str 
) [private]

Definition at line 104 of file test_urllib2_localnet.py.

00104 
00105     def _create_auth_dict(self, auth_str):
00106         first_space_index = auth_str.find(" ")
00107         auth_str = auth_str[first_space_index+1:]
00108 
00109         parts = auth_str.split(",")
00110 
00111         auth_dict = {}
00112         for part in parts:
00113             name, value = part.split("=")
00114             name = name.strip()
00115             if value[0] == '"' and value[-1] == '"':
00116                 value = value[1:-1]
00117             else:
00118                 value = value.strip()
00119             auth_dict[name] = value
00120         return auth_dict

Here is the caller graph for this function:

Definition at line 98 of file test_urllib2_localnet.py.

00098 
00099     def _generate_nonce(self):
00100         self._request_num += 1
00101         nonce = hashlib.md5(str(self._request_num).encode("ascii")).hexdigest()
00102         self._nonces.append(nonce)
00103         return nonce

Here is the caller graph for this function:

def test.test_urllib2_localnet.DigestAuthHandler._return_auth_challenge (   self,
  request_handler 
) [private]

Definition at line 139 of file test_urllib2_localnet.py.

00139 
00140     def _return_auth_challenge(self, request_handler):
00141         request_handler.send_response(407, "Proxy Authentication Required")
00142         request_handler.send_header("Content-Type", "text/html")
00143         request_handler.send_header(
00144             'Proxy-Authenticate', 'Digest realm="%s", '
00145             'qop="%s",'
00146             'nonce="%s", ' % \
00147             (self._realm_name, self._qop, self._generate_nonce()))
00148         # XXX: Not sure if we're supposed to add this next header or
00149         # not.
00150         #request_handler.send_header('Connection', 'close')
00151         request_handler.end_headers()
00152         request_handler.wfile.write(b"Proxy Authentication Required.")
00153         return False

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_urllib2_localnet.DigestAuthHandler._validate_auth (   self,
  auth_dict,
  password,
  method,
  uri 
) [private]

Definition at line 121 of file test_urllib2_localnet.py.

00121 
00122     def _validate_auth(self, auth_dict, password, method, uri):
00123         final_dict = {}
00124         final_dict.update(auth_dict)
00125         final_dict["password"] = password
00126         final_dict["method"] = method
00127         final_dict["uri"] = uri
00128         HA1_str = "%(username)s:%(realm)s:%(password)s" % final_dict
00129         HA1 = hashlib.md5(HA1_str.encode("ascii")).hexdigest()
00130         HA2_str = "%(method)s:%(uri)s" % final_dict
00131         HA2 = hashlib.md5(HA2_str.encode("ascii")).hexdigest()
00132         final_dict["HA1"] = HA1
00133         final_dict["HA2"] = HA2
00134         response_str = "%(HA1)s:%(nonce)s:%(nc)s:" \
00135                        "%(cnonce)s:%(qop)s:%(HA2)s" % final_dict
00136         response = hashlib.md5(response_str.encode("ascii")).hexdigest()
00137 
00138         return response == auth_dict["response"]

Here is the caller graph for this function:

def test.test_urllib2_localnet.DigestAuthHandler.handle_request (   self,
  request_handler 
)
Performs digest authentication on the given HTTP request
handler.  Returns True if authentication was successful, False
otherwise.

If no users have been set, then digest auth is effectively
disabled and this method will always return True.

Definition at line 154 of file test_urllib2_localnet.py.

00154 
00155     def handle_request(self, request_handler):
00156         """Performs digest authentication on the given HTTP request
00157         handler.  Returns True if authentication was successful, False
00158         otherwise.
00159 
00160         If no users have been set, then digest auth is effectively
00161         disabled and this method will always return True.
00162         """
00163 
00164         if len(self._users) == 0:
00165             return True
00166 
00167         if "Proxy-Authorization" not in request_handler.headers:
00168             return self._return_auth_challenge(request_handler)
00169         else:
00170             auth_dict = self._create_auth_dict(
00171                 request_handler.headers["Proxy-Authorization"]
00172                 )
00173             if auth_dict["username"] in self._users:
00174                 password = self._users[ auth_dict["username"] ]
00175             else:
00176                 return self._return_auth_challenge(request_handler)
00177             if not auth_dict.get("nonce") in self._nonces:
00178                 return self._return_auth_challenge(request_handler)
00179             else:
00180                 self._nonces.remove(auth_dict["nonce"])
00181 
00182             auth_validated = False
00183 
00184             # MSIE uses short_path in its validation, but Python's
00185             # urllib.request uses the full path, so we're going to see if
00186             # either of them works here.
00187 
00188             for path in [request_handler.path, request_handler.short_path]:
00189                 if self._validate_auth(auth_dict,
00190                                        password,
00191                                        request_handler.command,
00192                                        path):
00193                     auth_validated = True
00194 
00195             if not auth_validated:
00196                 return self._return_auth_challenge(request_handler)
00197             return True
00198 
00199 # Proxy test infrastructure

Here is the call graph for this function:

Definition at line 88 of file test_urllib2_localnet.py.

00088 
00089     def set_qop(self, qop):
00090         self._qop = qop

Definition at line 95 of file test_urllib2_localnet.py.

00095 
00096     def set_realm(self, realm):
00097         self._realm_name = realm

Definition at line 91 of file test_urllib2_localnet.py.

00091 
00092     def set_users(self, users):
00093         assert isinstance(users, dict)
00094         self._users = users


Member Data Documentation

Definition at line 83 of file test_urllib2_localnet.py.

Definition at line 86 of file test_urllib2_localnet.py.

Definition at line 85 of file test_urllib2_localnet.py.

Definition at line 82 of file test_urllib2_localnet.py.

Definition at line 84 of file test_urllib2_localnet.py.


The documentation for this class was generated from the following file: