[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [stem/master] Providing cookie path with CookieAuthFailed exceptions



commit 0f4fc2ee2a3e442aa3cad3dd5bb28580771570a2
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Apr 22 13:55:04 2012 -0700

    Providing cookie path with CookieAuthFailed exceptions
    
    A common attribute that callers want when authenticate() raises a
    CookieAuthFailed is the path that we tried to read the cookie from. Since the
    authentcate() call does its own PROTOCOLINFO query the caller never sees it
    (except for the exception message). Simple thing to fix.
---
 stem/connection.py                     |   18 +++++++++++-------
 test/unit/connection/authentication.py |    8 ++++----
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/stem/connection.py b/stem/connection.py
index fdb3a09..419dfa3 100644
--- a/stem/connection.py
+++ b/stem/connection.py
@@ -151,6 +151,10 @@ class MissingPassword(PasswordAuthFailed):
 
 class CookieAuthFailed(AuthenticationFailure):
   "Failure to authenticate with an authentication cookie."
+  
+  def __init__(self, message, cookie_path, auth_response = None):
+    AuthenticationFailure.__init__(self, message, auth_response)
+    self.cookie_path = cookie_path
 
 class CookieAuthRejected(CookieAuthFailed):
   "Socket does not support password authentication."
@@ -409,7 +413,7 @@ def authenticate(control_socket, password = None, protocolinfo_response = None):
       auth_exceptions.append(exc)
     except CookieAuthRejected, exc:
       log.debug("The authenticate_cookie method raised a CookieAuthRejected when cookie auth should be available. Stem may need to be corrected to recognize this response: %s" % exc)
-      auth_exceptions.append(IncorrectCookieValue(str(exc)))
+      auth_exceptions.append(IncorrectCookieValue(str(exc), exc.cookie_path))
     except stem.socket.ControllerError, exc:
       auth_exceptions.append(AuthenticationFailure(str(exc)))
   
@@ -561,7 +565,7 @@ def authenticate_cookie(control_socket, cookie_path, suppress_ctl_errors = True)
   """
   
   if not os.path.exists(cookie_path):
-    raise UnreadableCookieFile("Authentication failed: '%s' doesn't exist" % cookie_path)
+    raise UnreadableCookieFile("Authentication failed: '%s' doesn't exist" % cookie_path, cookie_path)
   
   # Abort if the file isn't 32 bytes long. This is to avoid exposing arbitrary
   # file content to the port.
@@ -576,14 +580,14 @@ def authenticate_cookie(control_socket, cookie_path, suppress_ctl_errors = True)
   
   if auth_cookie_size != 32:
     exc_msg = "Authentication failed: authentication cookie '%s' is the wrong size (%i bytes instead of 32)" % (cookie_path, auth_cookie_size)
-    raise IncorrectCookieSize(exc_msg)
+    raise IncorrectCookieSize(exc_msg, cookie_path)
   
   try:
     auth_cookie_file = open(cookie_path, "r")
     auth_cookie_contents = auth_cookie_file.read()
     auth_cookie_file.close()
   except IOError, exc:
-    raise UnreadableCookieFile("Authentication failed: unable to read '%s' (%s)" % (cookie_path, exc)) 
+    raise UnreadableCookieFile("Authentication failed: unable to read '%s' (%s)" % (cookie_path, exc), cookie_path) 
   
   try:
     control_socket.send("AUTHENTICATE %s" % binascii.b2a_hex(auth_cookie_contents))
@@ -600,15 +604,15 @@ def authenticate_cookie(control_socket, cookie_path, suppress_ctl_errors = True)
       
       if "*or* authentication cookie." in str(auth_response) or \
          "Authentication cookie did not match expected value." in str(auth_response):
-        raise IncorrectCookieValue(str(auth_response), auth_response)
+        raise IncorrectCookieValue(str(auth_response), cookie_path, auth_response)
       else:
-        raise CookieAuthRejected(str(auth_response), auth_response)
+        raise CookieAuthRejected(str(auth_response), cookie_path, auth_response)
   except stem.socket.ControllerError, exc:
     try: control_socket.connect()
     except: pass
     
     if not suppress_ctl_errors: raise exc
-    else: raise CookieAuthRejected("Socket failed (%s)" % exc)
+    else: raise CookieAuthRejected("Socket failed (%s)" % exc, cookie_path)
 
 def get_protocolinfo(control_socket):
   """
diff --git a/test/unit/connection/authentication.py b/test/unit/connection/authentication.py
index b3f2a91..0937439 100644
--- a/test/unit/connection/authentication.py
+++ b/test/unit/connection/authentication.py
@@ -87,10 +87,10 @@ class TestAuthenticate(unittest.TestCase):
       stem.connection.IncorrectPassword(None))
     
     all_auth_cookie_exc = (None,
-      stem.connection.IncorrectCookieSize(None),
-      stem.connection.UnreadableCookieFile(None),
-      stem.connection.CookieAuthRejected(None),
-      stem.connection.IncorrectCookieValue(None))
+      stem.connection.IncorrectCookieSize(None, None),
+      stem.connection.UnreadableCookieFile(None, None),
+      stem.connection.CookieAuthRejected(None, None),
+      stem.connection.IncorrectCookieValue(None, None))
     
     # authentication functions might raise a controller error when
     # 'suppress_ctl_errors' is False, so including those



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits