diff easyhg.py @ 459:ee06f9275c99

Use cached password directly, without asking the user, if it's available and the requested realm/uri are not the same as an immediately preceding one
author Chris Cannam
date Thu, 30 Jun 2011 00:04:33 +0100
parents c22cef015d73
children 9f738c3ae9ba
line wrap: on
line diff
--- a/easyhg.py	Wed Jun 29 23:48:11 2011 +0100
+++ b/easyhg.py	Thu Jun 30 00:04:33 2011 +0100
@@ -168,7 +168,7 @@
 
     def remote_key(self, url, user):
         # generate a "safe-for-config-file" key representing uri+user
-        self.ui.write('generating remote_key for url %s and user %s\n' % (url, user))
+#        self.ui.write('generating remote_key for url %s and user %s\n' % (url, user))
         s = '%s@@%s' % (url, user)
         h = hashlib.sha1()
         h.update(self.auth_key)
@@ -207,7 +207,7 @@
     
         self.set_to_config('preferences', 'remember', self.remember)
 
-        self.ui.write('aiming to store details for user %s\n' % self.user)
+#        self.ui.write('aiming to store details for user %s\n' % self.user)
     
         if self.remember and self.user:
             d = self.encrypt(self.user)
@@ -230,7 +230,12 @@
     def __init__(self, ui, url, user, passwd):
         self.auth_store = EasyHgAuthStore(ui, url, user, passwd)
 
-    def ask(self):
+    def ask(self, force_dialog):
+
+        if self.auth_store.user and self.auth_store.passwd:
+            if not force_dialog:
+                return (self.auth_store.user, self.auth_store.passwd)
+
         dialog = QtGui.QDialog()
         layout = QtGui.QGridLayout()
         dialog.setLayout(layout)
@@ -317,6 +322,9 @@
 @monkeypatch_method(passwordmgr)
 def find_user_password(self, realm, authuri):
 
+    if not hasattr(self, '__easyhg_last'):
+        self.__easyhg_last = None
+
     if not self.ui.interactive():
         return orig_find(self, realm, authuri)
     if not easyhg_pyqt_ok:
@@ -327,16 +335,33 @@
     user, passwd = authinfo
 
     if user and passwd:
-        self.ui.write("note: user and passwd both provided\n")
+#        self.ui.write("note: user and passwd both provided\n")
         return orig_find(self, realm, authuri)
 
-    self.ui.write("want username and/or password for %s\n" % authuri)
+#    self.ui.write("want username and/or password for %s\n" % authuri)
+
+    force_dialog = False
+
+#    if self.__easyhg_last:
+#        self.ui.write("last = realm '%s' authuri '%s'\n" % self.__easyhg_last)
+#        self.ui.write("me   = realm '%s' authuri '%s'\n" % (realm, authuri))
+
+    if (realm, authuri) == self.__easyhg_last:
+        # If we are called again just after identical previous
+        # request, then the previously returned auth must have been
+        # wrong. So we note this to force password prompt (and avoid
+        # reusing bad password indefinitely). Thanks to
+        # mercurial_keyring (Marcin Kasperski) for this logic
+#        self.ui.write("same again!\n")
+        force_dialog = True
 
     dialog = EasyHgAuthDialog(self.ui, authuri, user, passwd)
 
-    (user, passwd) = dialog.ask()
+    (user, passwd) = dialog.ask(force_dialog)
 
-    self.add_password(realm, authuri, user, passwd)
+#    self.add_password(realm, authuri, user, passwd)
+    self.__easyhg_last = (realm, authuri)
+#    self.ui.write("done = realm '%s' authuri '%s'\n" % self.__easyhg_last)
     return (user, passwd)