Show More
@@ -51,12 +51,17 b' backend (by default keyring guesses, usu' | |||||
51 | you get KDE Wallet under KDE, and Gnome Keyring under Gnome or Unity). |
|
51 | you get KDE Wallet under KDE, and Gnome Keyring under Gnome or Unity). | |
52 | ''' |
|
52 | ''' | |
53 |
|
53 | |||
54 | import urllib2 |
|
|||
55 | import smtplib |
|
|||
56 | import socket |
|
54 | import socket | |
57 | import os |
|
55 | import os | |
58 | import sys |
|
56 | import sys | |
59 | import re |
|
57 | import re | |
|
58 | if sys.version_info[0] < 3: | |||
|
59 | from urllib2 import ( | |||
|
60 | HTTPPasswordMgrWithDefaultRealm, AbstractBasicAuthHandler, AbstractDigestAuthHandler) | |||
|
61 | else: | |||
|
62 | from urllib.request import ( | |||
|
63 | HTTPPasswordMgrWithDefaultRealm, AbstractBasicAuthHandler, AbstractDigestAuthHandler) | |||
|
64 | import smtplib | |||
60 |
|
65 | |||
61 | from mercurial import util, sslutil, error |
|
66 | from mercurial import util, sslutil, error | |
62 | from mercurial.i18n import _ |
|
67 | from mercurial.i18n import _ | |
@@ -215,7 +220,8 b' class PasswordStore(object):' | |||||
215 | password = keyring.get_password(KEYRING_SERVICE, pwdkey) |
|
220 | password = keyring.get_password(KEYRING_SERVICE, pwdkey) | |
216 | except Exception as err: |
|
221 | except Exception as err: | |
217 | ui = uimod.ui() |
|
222 | ui = uimod.ui() | |
218 |
ui.warn(_("keyring: keyring backend doesn't seem to work, password can not be restored. Falling back to prompts. Error details: %s\n") |
|
223 | ui.warn(_("keyring: keyring backend doesn't seem to work, password can not be restored. Falling back to prompts. Error details: %s\n"), | |
|
224 | err) | |||
219 | return '' |
|
225 | return '' | |
220 | # Reverse recoding from next routine |
|
226 | # Reverse recoding from next routine | |
221 | if isinstance(password, unicode): |
|
227 | if isinstance(password, unicode): | |
@@ -234,7 +240,8 b' class PasswordStore(object):' | |||||
234 | KEYRING_SERVICE, pwdkey, password) |
|
240 | KEYRING_SERVICE, pwdkey, password) | |
235 | except Exception as err: |
|
241 | except Exception as err: | |
236 | ui = uimod.ui() |
|
242 | ui = uimod.ui() | |
237 |
ui.warn(_("keyring: keyring backend doesn't seem to work, password was not saved. Error details: %s\n") |
|
243 | ui.warn(_("keyring: keyring backend doesn't seem to work, password was not saved. Error details: %s\n"), | |
|
244 | err) | |||
238 |
|
245 | |||
239 |
|
246 | |||
240 | password_store = PasswordStore() |
|
247 | password_store = PasswordStore() | |
@@ -322,17 +329,17 b' class HTTPPasswordHandler(object):' | |||||
322 |
|
329 | |||
323 | parsed_url, url_user, url_passwd = self.unpack_url(authuri) |
|
330 | parsed_url, url_user, url_passwd = self.unpack_url(authuri) | |
324 | base_url = str(parsed_url) |
|
331 | base_url = str(parsed_url) | |
325 |
ui.debug(_('keyring: base url: %s, url user: %s, url pwd: %s\n') |
|
332 | ui.debug(_('keyring: base url: %s, url user: %s, url pwd: %s\n'), | |
326 |
|
|
333 | base_url, url_user or '', url_passwd and '******' or '') | |
327 |
|
334 | |||
328 | # Extract username (or password) stored directly in url |
|
335 | # Extract username (or password) stored directly in url | |
329 | if url_user and url_passwd: |
|
336 | if url_user and url_passwd: | |
330 | return url_user, url_passwd, self.SRC_URL, base_url |
|
337 | return url_user, url_passwd, self.SRC_URL, base_url | |
331 |
|
338 | |||
332 | # Extract data from urllib (in case it was already stored) |
|
339 | # Extract data from urllib (in case it was already stored) | |
333 |
if isinstance(pwmgr, |
|
340 | if isinstance(pwmgr, HTTPPasswordMgrWithDefaultRealm): | |
334 | urllib_user, urllib_pwd = \ |
|
341 | urllib_user, urllib_pwd = \ | |
335 |
|
|
342 | HTTPPasswordMgrWithDefaultRealm.find_user_password( | |
336 | pwmgr, realm, authuri) |
|
343 | pwmgr, realm, authuri) | |
337 | else: |
|
344 | else: | |
338 | urllib_user, urllib_pwd = pwmgr.passwddb.find_user_password( |
|
345 | urllib_user, urllib_pwd = pwmgr.passwddb.find_user_password( | |
@@ -364,7 +371,8 b' class HTTPPasswordHandler(object):' | |||||
364 |
|
371 | |||
365 | # Load from keyring. |
|
372 | # Load from keyring. | |
366 | if actual_user: |
|
373 | if actual_user: | |
367 |
ui.debug(_("keyring: looking for password (user %s, url %s)\n") |
|
374 | ui.debug(_("keyring: looking for password (user %s, url %s)\n"), | |
|
375 | actual_user, keyring_url) | |||
368 | keyring_pwd = password_store.get_http_password(keyring_url, actual_user) |
|
376 | keyring_pwd = password_store.get_http_password(keyring_url, actual_user) | |
369 | if keyring_pwd: |
|
377 | if keyring_pwd: | |
370 | return actual_user, keyring_pwd, self.SRC_KEYRING, keyring_url |
|
378 | return actual_user, keyring_pwd, self.SRC_KEYRING, keyring_url | |
@@ -381,10 +389,10 b' class HTTPPasswordHandler(object):' | |||||
381 | ui.status(_("keyring: username not specified in hgrc (or in url). Password will not be saved.\n")) |
|
389 | ui.status(_("keyring: username not specified in hgrc (or in url). Password will not be saved.\n")) | |
382 |
|
390 | |||
383 | ui.write(_("http authorization required\n")) |
|
391 | ui.write(_("http authorization required\n")) | |
384 |
ui.status(_("realm: %s\n") |
|
392 | ui.status(_("realm: %s\n"), realm) | |
385 |
ui.status(_("url: %s\n") |
|
393 | ui.status(_("url: %s\n"), url) | |
386 | if user: |
|
394 | if user: | |
387 |
ui.write(_("user: %s (fixed in hgrc or url)\n" |
|
395 | ui.write(_("user: %s (fixed in hgrc or url)\n", user)) | |
388 | else: |
|
396 | else: | |
389 | user = ui.prompt(_("user:"), default=None) |
|
397 | user = ui.prompt(_("user:"), default=None) | |
390 | pwd = ui.getpass(_("password: ")) |
|
398 | pwd = ui.getpass(_("password: ")) | |
@@ -418,7 +426,7 b' class HTTPPasswordHandler(object):' | |||||
418 | # It is done only if username is permanently set. |
|
426 | # It is done only if username is permanently set. | |
419 | # Otherwise we won't be able to find the password so it |
|
427 | # Otherwise we won't be able to find the password so it | |
420 | # does not make much sense to preserve it |
|
428 | # does not make much sense to preserve it | |
421 |
_debug(ui, _("Saving password for %s to keyring") |
|
429 | _debug(ui, _("Saving password for %s to keyring"), user) | |
422 | try: |
|
430 | try: | |
423 | password_store.set_http_password(final_url, user, pwd) |
|
431 | password_store.set_http_password(final_url, user, pwd) | |
424 | except Exception as e: |
|
432 | except Exception as e: | |
@@ -575,7 +583,7 b' def find_user_password(self, realm, auth' | |||||
575 | return self._pwd_handler.find_auth(self, realm, authuri, req) |
|
583 | return self._pwd_handler.find_auth(self, realm, authuri, req) | |
576 |
|
584 | |||
577 |
|
585 | |||
578 |
@monkeypatch_method( |
|
586 | @monkeypatch_method(AbstractBasicAuthHandler, "http_error_auth_reqed") | |
579 | def basic_http_error_auth_reqed(self, authreq, host, req, headers): |
|
587 | def basic_http_error_auth_reqed(self, authreq, host, req, headers): | |
580 | """Preserves current HTTP request so it can be consulted |
|
588 | """Preserves current HTTP request so it can be consulted | |
581 | in find_user_password above""" |
|
589 | in find_user_password above""" | |
@@ -586,7 +594,7 b' def basic_http_error_auth_reqed(self, au' | |||||
586 | self.passwd._http_req = None |
|
594 | self.passwd._http_req = None | |
587 |
|
595 | |||
588 |
|
596 | |||
589 |
@monkeypatch_method( |
|
597 | @monkeypatch_method(AbstractDigestAuthHandler, "http_error_auth_reqed") | |
590 | def digest_http_error_auth_reqed(self, authreq, host, req, headers): |
|
598 | def digest_http_error_auth_reqed(self, authreq, host, req, headers): | |
591 | """Preserves current HTTP request so it can be consulted |
|
599 | """Preserves current HTTP request so it can be consulted | |
592 | in find_user_password above""" |
|
600 | in find_user_password above""" | |
@@ -617,13 +625,14 b' def try_smtp_login(ui, smtp_obj, usernam' | |||||
617 | if not password: |
|
625 | if not password: | |
618 | return False |
|
626 | return False | |
619 | try: |
|
627 | try: | |
620 |
ui.note(_('(authenticating to mail server as %s)\n') |
|
628 | ui.note(_('(authenticating to mail server as %s)\n'), | |
621 |
|
|
629 | username) | |
622 | smtp_obj.login(username, password) |
|
630 | smtp_obj.login(username, password) | |
623 | return True |
|
631 | return True | |
624 | except smtplib.SMTPException as inst: |
|
632 | except smtplib.SMTPException as inst: | |
625 | if inst.smtp_code == 535: |
|
633 | if inst.smtp_code == 535: | |
626 |
ui.status(_("SMTP login failed: %s\n\n") |
|
634 | ui.status(_("SMTP login failed: %s\n\n"), | |
|
635 | inst.smtp_error) | |||
627 | return False |
|
636 | return False | |
628 | else: |
|
637 | else: | |
629 | raise error.Abort(inst) |
|
638 | raise error.Abort(inst) | |
@@ -685,8 +694,8 b' def keyring_supported_smtp(ui, username)' | |||||
685 | else: |
|
694 | else: | |
686 | defaultport = 25 |
|
695 | defaultport = 25 | |
687 | mailport = util.getport(ui.config('smtp', 'port', defaultport)) |
|
696 | mailport = util.getport(ui.config('smtp', 'port', defaultport)) | |
688 |
ui.note(_('sending mail: smtp host %s, port %s\n') |
|
697 | ui.note(_('sending mail: smtp host %s, port %s\n'), | |
689 |
|
|
698 | mailhost, mailport) | |
690 | s.connect(host=mailhost, port=mailport) |
|
699 | s.connect(host=mailhost, port=mailport) | |
691 | if starttls: |
|
700 | if starttls: | |
692 | ui.note(_('(using starttls)\n')) |
|
701 | ui.note(_('(using starttls)\n')) | |
@@ -753,7 +762,7 b' cmdtable = {}' | |||||
753 | command = meu.command(cmdtable) |
|
762 | command = meu.command(cmdtable) | |
754 |
|
763 | |||
755 |
|
764 | |||
756 | @command('keyring_check', |
|
765 | @command(b'keyring_check', | |
757 | [], |
|
766 | [], | |
758 | _("keyring_check [PATH]"), |
|
767 | _("keyring_check [PATH]"), | |
759 | optionalrepo=True) |
|
768 | optionalrepo=True) | |
@@ -788,22 +797,23 b' def cmd_keyring_check(ui, repo, *path_ar' | |||||
788 | for name, url in paths: |
|
797 | for name, url in paths: | |
789 | if not is_http_path(url): |
|
798 | if not is_http_path(url): | |
790 | if path_args: |
|
799 | if path_args: | |
791 |
ui.status(_(" %s: non-http path (%s)\n") |
|
800 | ui.status(_(" %s: non-http path (%s)\n"), | |
|
801 | name, url) | |||
792 | continue |
|
802 | continue | |
793 | user, pwd, source, final_url = handler.get_credentials( |
|
803 | user, pwd, source, final_url = handler.get_credentials( | |
794 | make_passwordmgr(ui), name, url) |
|
804 | make_passwordmgr(ui), name, url) | |
795 | if pwd: |
|
805 | if pwd: | |
796 |
ui.status(_(" %s: password available, source: %s, bound to user %s, url %s\n") |
|
806 | ui.status(_(" %s: password available, source: %s, bound to user %s, url %s\n"), | |
797 |
name, source, user, final_url) |
|
807 | name, source, user, final_url) | |
798 | elif user: |
|
808 | elif user: | |
799 |
ui.status(_(" %s: password not available, once entered, will be bound to user %s, url %s\n") |
|
809 | ui.status(_(" %s: password not available, once entered, will be bound to user %s, url %s\n"), | |
800 |
name, user, final_url) |
|
810 | name, user, final_url) | |
801 | else: |
|
811 | else: | |
802 |
ui.status(_(" %s: password not available, user unknown, url %s\n") |
|
812 | ui.status(_(" %s: password not available, user unknown, url %s\n"), | |
803 |
name, final_url) |
|
813 | name, final_url) | |
804 |
|
814 | |||
805 |
|
815 | |||
806 | @command('keyring_clear', |
|
816 | @command(b'keyring_clear', | |
807 | [], |
|
817 | [], | |
808 | _('hg keyring_clear PATH-OR-ALIAS'), |
|
818 | _('hg keyring_clear PATH-OR-ALIAS'), | |
809 | optionalrepo=True) |
|
819 | optionalrepo=True) | |
@@ -820,7 +830,8 b' def cmd_keyring_clear(ui, repo, path, **' | |||||
820 | path_url = url |
|
830 | path_url = url | |
821 | break |
|
831 | break | |
822 | if not is_http_path(path_url): |
|
832 | if not is_http_path(path_url): | |
823 |
ui.status(_("%s is not a http path (and %s can't be resolved as path alias)\n") |
|
833 | ui.status(_("%s is not a http path (and %s can't be resolved as path alias)\n"), | |
|
834 | path, path_url) | |||
824 | return |
|
835 | return | |
825 |
|
836 | |||
826 | handler = HTTPPasswordHandler() |
|
837 | handler = HTTPPasswordHandler() | |
@@ -828,20 +839,21 b' def cmd_keyring_clear(ui, repo, path, **' | |||||
828 | user, pwd, source, final_url = handler.get_credentials( |
|
839 | user, pwd, source, final_url = handler.get_credentials( | |
829 | make_passwordmgr(ui), path, path_url) |
|
840 | make_passwordmgr(ui), path, path_url) | |
830 | if not user: |
|
841 | if not user: | |
831 |
ui.status(_("Username not configured for url %s\n") |
|
842 | ui.status(_("Username not configured for url %s\n"), | |
|
843 | final_url) | |||
832 | return |
|
844 | return | |
833 | if not pwd: |
|
845 | if not pwd: | |
834 |
ui.status(_("No password is saved for user %s, url %s\n") |
|
846 | ui.status(_("No password is saved for user %s, url %s\n"), | |
835 |
user, final_url) |
|
847 | user, final_url) | |
836 | return |
|
848 | return | |
837 |
|
849 | |||
838 | if source != handler.SRC_KEYRING: |
|
850 | if source != handler.SRC_KEYRING: | |
839 |
ui.status(_("Password for user %s, url %s is saved in %s, not in keyring\n") |
|
851 | ui.status(_("Password for user %s, url %s is saved in %s, not in keyring\n"), | |
840 |
user, final_url, source) |
|
852 | user, final_url, source) | |
841 |
|
853 | |||
842 | password_store.clear_http_password(final_url, user) |
|
854 | password_store.clear_http_password(final_url, user) | |
843 |
ui.status(_("Password removed for user %s, url %s\n") |
|
855 | ui.status(_("Password removed for user %s, url %s\n"), | |
844 |
user, final_url) |
|
856 | user, final_url) | |
845 |
|
857 | |||
846 |
|
858 | |||
847 | buglink = 'https://bitbucket.org/Mekk/mercurial_keyring/issues' |
|
859 | buglink = 'https://bitbucket.org/Mekk/mercurial_keyring/issues' |
General Comments 0
You need to be logged in to leave comments.
Login now