Rev 2912: Since all schemes query AuthenticationConfig then prompt user, make that in http://code.launchpad.net/%7Ev-ladeuil/bzr/auth.ring
Vincent Ladeuil
v.ladeuil+lp at free.fr
Sat Oct 20 17:49:32 BST 2007
At http://code.launchpad.net/%7Ev-ladeuil/bzr/auth.ring
------------------------------------------------------------
revno: 2912
revision-id: v.ladeuil+lp at free.fr-20071020164914-zr8s4gimxnbmvecd
parent: v.ladeuil+lp at free.fr-20071018205418-mj1g30cn3n4u1ge1
committer: Vincent Ladeuil <v.ladeuil+lp at free.fr>
branch nick: auth.ring
timestamp: Sat 2007-10-20 18:49:14 +0200
message:
Since all schemes query AuthenticationConfig then prompt user, make that
a method.
* bzrlib/transport/ssh.py:
(_paramiko_auth): Simplified.
* bzrlib/transport/http/_urllib2_wrappers.py:
Add FIXME about https never mentioned in prompts.
(AbstractAuthHandler.get_password): Simplified.
* bzrlib/transport/ftp.py:
(FtpTransport._create_connection): Simplified.
* bzrlib/tests/test_smtp_connection.py:
(TestSMTPConnection.test_smtp_password_from_user): Moved from
TestSMTPConnectionWithUI and given a proper stdout for ui_factory.
(TestSMTPConnectionWithUI): Deleted.
* bzrlib/tests/test_http.py:
(TestAuth.setUp): Mother class already install a SilentUIFactory
and restore the proper one, so there is no need to do that
ourselves.
(TestAuth.restoreUIFactory): Deleted.
(TestAuth.test_prompt_for_password,
TestAuth.test_no_prompt_for_password_when_using_auth_config):
Provides a suitable stdout to TestUIFactory.
* bzrlib/tests/test_ftp_transport.py:
(TestFTPServerUI.setUp, TestFTPServerUI.restoreUIFactory):
Deleted. Mother class already install a SilentUIFactory and
restore the proper one, so there is no need to do that ourselves.
(TestFTPServerUI.test_prompt_for_password,
TestFTPServerUI.test_no_prompt_for_password_when_using_auth_config):
Provides a suitable stdout to TestUIFactory.
* bzrlib/smtp_connection.py:
(SMTPConnection._authenticate): Simplified.
* bzrlib/config.py:
(AuthenticationConfig.get_password): Encapsulate pattern common to
all schemes actually used.
modified:
bzrlib/config.py config.py-20051011043216-070c74f4e9e338e8
bzrlib/smtp_connection.py smtp_connection.py-20070618204456-nu6wag1ste4biuk2-1
bzrlib/tests/test_ftp_transport.py test_aftp_transport.-20060823221619-98mwjzxtwtkt527k-1
bzrlib/tests/test_http.py testhttp.py-20051018020158-b2eef6e867c514d9
bzrlib/tests/test_smtp_connection.py test_smtp_connection-20070618204509-wuyxc0r0ztrecv7e-1
bzrlib/transport/ftp.py ftp.py-20051116161804-58dc9506548c2a53
bzrlib/transport/http/_urllib2_wrappers.py _urllib2_wrappers.py-20060913231729-ha9ugi48ktx481ao-1
bzrlib/transport/ssh.py ssh.py-20060824042150-0s9787kng6zv1nwq-1
-------------- next part --------------
=== modified file 'bzrlib/config.py'
--- a/bzrlib/config.py 2007-10-18 10:33:11 +0000
+++ b/bzrlib/config.py 2007-10-20 16:49:14 +0000
@@ -70,6 +70,7 @@
import errno
from fnmatch import fnmatch
import re
+# FIXME: Why not CStringIO ? -- vila 20071019
from StringIO import StringIO
import bzrlib
@@ -80,6 +81,7 @@
osutils,
symbol_versioning,
trace,
+ ui,
urlutils,
win32utils,
)
@@ -237,6 +239,8 @@
return v.decode(bzrlib.user_encoding)
v = os.environ.get('BZREMAIL')
if v:
+ # FIXME: Seems to have been deprecated since more than a year now,
+ # time to delete ? -- vila 20071019
trace.warning('BZREMAIL is deprecated in favor of BZR_EMAIL.'
' Please update your configuration.')
return v.decode(bzrlib.user_encoding)
@@ -460,10 +464,10 @@
def __init__(self, location):
name_generator = locations_config_filename
- if (not os.path.exists(name_generator()) and
+ if (not os.path.exists(name_generator()) and
os.path.exists(branches_config_filename())):
if sys.platform == 'win32':
- trace.warning('Please rename %s to %s'
+ trace.warning('Please rename %s to %s'
% (branches_config_filename(),
locations_config_filename()))
else:
@@ -1055,5 +1059,46 @@
return credentials
+ def get_password(self, scheme, host, user, port=None,
+ realm=None, path=None, prompt=None):
+ """Get a password from authentication file or prompt the user for one.
+
+ :param scheme: protocol
+
+ :param host: the server address
+
+ :param port: the associated port (optional)
+
+ :param user: login
+
+ :param realm: the realm sent by the server (optional)
+
+ :param path: the absolute path on the server (optional)
+
+ :return: The found password or the one entered by the user.
+ """
+ credentials = self.get_credentials(scheme, host, port, user, path)
+ if credentials is not None:
+ password = credentials['password']
+ else:
+ # Prompt user only if we could't find a password
+ print 'couco'
+ if prompt is None:
+ prompt = ('%s' % scheme.upper()
+ + ' %(user)s@%(host)s%(realm)s password')
+ # Special handling for optional fields in the prompt
+ if port is not None:
+ prompt_host = '%s:%d' % (host, port)
+ else:
+ prompt_host = host
+ if realm is not None:
+ prompt_realm = ", Realm: '%s'" % realm
+ else:
+ prompt_realm = ''
+ password = ui.ui_factory.get_password(prompt, host=prompt_host,
+ user=user,
+ realm=prompt_realm)
+ return password
+
def decode_password(self, password, encoding):
return password
=== modified file 'bzrlib/smtp_connection.py'
--- a/bzrlib/smtp_connection.py 2007-10-18 20:54:18 +0000
+++ b/bzrlib/smtp_connection.py 2007-10-20 16:49:14 +0000
@@ -92,20 +92,10 @@
if self._smtp_username is None:
return
- password = self._smtp_password
- if password is None:
+ if self._smtp_password is None:
auth = config.AuthenticationConfig()
- config_credentials = auth.get_credentials('smtp', self._smtp_server,
- user=self._smtp_username)
- if config_credentials is not None:
- password = config_credentials['password']
- else:
- password = ui.ui_factory.get_password(
- 'Please enter the SMTP password: %(user)s@%(host)s',
- user=self._smtp_username,
- host=self._smtp_server)
-
- self._smtp_password = password
+ self._smtp_password = auth.get_password(
+ 'smtp', self._smtp_server, self._smtp_username)
self._connection.login(self._smtp_username, self._smtp_password)
=== modified file 'bzrlib/tests/test_ftp_transport.py'
--- a/bzrlib/tests/test_ftp_transport.py 2007-10-17 15:36:20 +0000
+++ b/bzrlib/tests/test_ftp_transport.py 2007-10-20 16:49:14 +0000
@@ -82,21 +82,6 @@
class TestFTPServerUI(TestCaseWithFTPServer):
- def setUp(self):
- super(TestFTPServerUI, self).setUp()
- self.old_factory = ui.ui_factory
- # The following has the unfortunate side-effect of hiding any ouput
- # during the tests (including pdb prompts). Feel free to comment them
- # for debugging purposes but leave them in place, there are needed to
- # run the tests without any console
- self.old_stdout = sys.stdout
- sys.stdout = tests.StringIOWrapper()
- self.addCleanup(self.restoreUIFactory)
-
- def restoreUIFactory(self):
- ui.ui_factory = self.old_factory
- sys.stdout = self.old_stdout
-
def test_prompt_for_password(self):
t = self.get_transport()
# Ensure that the test framework set the password
@@ -105,7 +90,8 @@
# reset it to None in the transport before the connection).
password = t._password
t._password = None
- ui.ui_factory = tests.TestUIFactory(stdin=password+'\n')
+ ui.ui_factory = tests.TestUIFactory(stdin=password+'\n',
+ stdout=tests.StringIOWrapper())
# Ask the server to check the password
server = self.get_server()
# FIXME: There should be a better way to declare authorized users and
@@ -124,7 +110,8 @@
# reset it to None in the transport before the connection).
password = t._password
t._password = None
- ui.ui_factory = tests.TestUIFactory(stdin='precious\n')
+ ui.ui_factory = tests.TestUIFactory(stdin='precious\n',
+ stdout=tests.StringIOWrapper())
# Ask the server to check the password
server = self.get_server()
# FIXME: There should be a better way to declare authorized users and
=== modified file 'bzrlib/tests/test_http.py'
--- a/bzrlib/tests/test_http.py 2007-10-17 17:22:26 +0000
+++ b/bzrlib/tests/test_http.py 2007-10-20 16:49:14 +0000
@@ -1220,18 +1220,6 @@
"""
self.build_tree_contents([('a', 'contents of a\n'),
('b', 'contents of b\n'),])
- self.old_factory = ui.ui_factory
- # The following has the unfortunate side-effect of hiding any ouput
- # during the tests (including pdb prompts). Feel free to comment them
- # for debugging purposes but leave them in place, there are needed to
- # run the tests without any console
- self.old_stdout = sys.stdout
- sys.stdout = StringIOWrapper()
- self.addCleanup(self.restoreUIFactory)
-
- def restoreUIFactory(self):
- ui.ui_factory = self.old_factory
- sys.stdout = self.old_stdout
def get_user_url(self, user=None, password=None):
"""Build an url embedding user and password"""
@@ -1285,7 +1273,7 @@
def test_prompt_for_password(self):
self.server.add_user('joe', 'foo')
t = self.get_user_transport('joe', None)
- ui.ui_factory = TestUIFactory(stdin='foo\n')
+ ui.ui_factory = TestUIFactory(stdin='foo\n', stdout=StringIOWrapper())
self.assertEqual('contents of a\n',t.get('a').read())
# stdin should be empty
self.assertEqual('', ui.ui_factory.stdin.readline())
@@ -1304,7 +1292,8 @@
stdin_content = 'bar\n' # Not the right password
self.server.add_user(user, password)
t = self.get_user_transport(user, None)
- ui.ui_factory = TestUIFactory(stdin=stdin_content)
+ ui.ui_factory = TestUIFactory(stdin=stdin_content,
+ stdout=StringIOWrapper())
# Create a minimal config file with the right password
conf = config.AuthenticationConfig()
conf._get_config().update(
=== modified file 'bzrlib/tests/test_smtp_connection.py'
--- a/bzrlib/tests/test_smtp_connection.py 2007-10-18 20:54:18 +0000
+++ b/bzrlib/tests/test_smtp_connection.py 2007-10-20 16:49:14 +0000
@@ -101,6 +101,20 @@
conn = self.get_connection('[DEFAULT]\nsmtp_password=mypass\n')
self.assertEqual(u'mypass', conn._smtp_password)
+ def test_smtp_password_from_user(self):
+ user = 'joe'
+ password = 'hispass'
+ conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
+ smtp_factory=everybody_is_welcome)
+ self.assertIs(None, conn._smtp_password)
+
+ ui.ui_factory = tests.TestUIFactory(stdin=password + '\n',
+ stdout=tests.StringIOWrapper())
+ conn._connect()
+ self.assertEqual(password, conn._smtp_password)
+ # stdin should be empty (the provided password have been consumed)
+ self.assertEqual('', ui.ui_factory.stdin.readline())
+
def test_smtp_password_from_auth_config(self):
user = 'joe'
password = 'hispass'
@@ -146,6 +160,7 @@
self.assertEqual('jrandom at example.com', from_)
self.assertEqual(sorted(['john at doe.com', 'jane at doe.com',
'pperez at ejemplo.com', 'user at localhost']), sorted(to))
+
def test_destination_address_required(self):
class FakeConfig:
def get_user_option(self, option):
@@ -166,38 +181,3 @@
self.assertRaises(
errors.NoDestinationAddress,
smtp_connection.SMTPConnection(FakeConfig()).send_email, msg)
-
-
-class TestSMTPConnectionWithUI(tests.TestCaseInTempDir):
-
- def setUp(self):
- super(TestSMTPConnectionWithUI, self).setUp()
- self.old_factory = ui.ui_factory
- # The following has the unfortunate side-effect of hiding any ouput
- # during the tests (including pdb prompts). Feel free to comment them
- # for debugging purposes but leave them in place, there are needed to
- # run the tests without any console
- self.old_stdout = sys.stdout
- sys.stdout = tests.StringIOWrapper()
- self.addCleanup(self.restoreUIFactory)
-
- def restoreUIFactory(self):
- ui.ui_factory = self.old_factory
- sys.stdout = self.old_stdout
-
- def get_connection(self, text, smtp_factory=None):
- return _get_connection(text, smtp_factory)
-
- def test_smtp_password_from_user(self):
- user = 'joe'
- password = 'hispass'
- conn = self.get_connection('[DEFAULT]\nsmtp_username=%s\n' % user,
- smtp_factory=everybody_is_welcome)
- self.assertIs(None, conn._smtp_password)
-
- ui.ui_factory = tests.TestUIFactory(stdin=password + '\n')
- conn._connect()
- self.assertEqual(password, conn._smtp_password)
- # stdin should be empty (the provided password have been consumed)
- self.assertEqual('', ui.ui_factory.stdin.readline())
-
=== modified file 'bzrlib/transport/ftp.py'
--- a/bzrlib/transport/ftp.py 2007-10-17 17:25:17 +0000
+++ b/bzrlib/transport/ftp.py 2007-10-20 16:49:14 +0000
@@ -133,15 +133,8 @@
if self._user and self._user != 'anonymous' and \
password is None: # '' is a valid password
auth = config.AuthenticationConfig()
- config_credentials = auth.get_credentials(
- 'ftp', self._host, self._port, user=self._user)
- if config_credentials is not None:
- password = config_credentials['password']
- else:
- get_password = bzrlib.ui.ui_factory.get_password
- password = get_password(
- prompt='FTP %(user)s@%(host)s password',
- user=self._user, host=self._host)
+ password = auth.get_password('ftp', self._host, self._user,
+ port=self._port)
connection.login(user=self._user, passwd=password)
connection.set_pasv(not self.is_active)
except ftplib.error_perm, e:
=== modified file 'bzrlib/transport/http/_urllib2_wrappers.py'
--- a/bzrlib/transport/http/_urllib2_wrappers.py 2007-10-17 17:22:26 +0000
+++ b/bzrlib/transport/http/_urllib2_wrappers.py 2007-10-20 16:49:14 +0000
@@ -1017,19 +1017,9 @@
host = netloc
port = None
auth = config.AuthenticationConfig()
- config_credentials = auth.get_credentials(
- scheme, host, port, user=user, path=path)
- if config_credentials is not None:
- password = config_credentials['password']
- else:
- # Prompt user only if we can't find a password
- if realm:
- realm_prompt = " Realm: '%s'" % realm
- else:
- realm_prompt = ''
- password = ui.ui_factory.get_password(
- prompt=self.password_prompt, user=user, host=netloc,
- realm=realm_prompt)
+ password = auth.get_password(
+ scheme, host, user, port=port, path=path, realm=realm,
+ prompt=self.password_prompt)
if password is not None:
self.add_password(realm, authuri, user, password)
return password
@@ -1203,6 +1193,7 @@
the auth request attribute.
"""
+ # FIXME: should mention http/https as part of the prompt
password_prompt = 'HTTP %(user)s@%(host)s%(realm)s password'
auth_required_header = 'www-authenticate'
auth_header = 'Authorization'
@@ -1227,6 +1218,7 @@
the proxy_auth request attribute..
"""
+ # FIXME: should mention http/https as part of the prompt
password_prompt = 'Proxy %(user)s@%(host)s%(realm)s password'
auth_required_header = 'proxy-authenticate'
# FIXME: the correct capitalization is Proxy-Authorization,
=== modified file 'bzrlib/transport/ssh.py'
--- a/bzrlib/transport/ssh.py 2007-10-17 20:47:40 +0000
+++ b/bzrlib/transport/ssh.py 2007-10-20 16:49:14 +0000
@@ -488,14 +488,7 @@
# give up and ask for a password
auth = config.AuthenticationConfig()
- config_credentials = auth.get_credentials('ssh', host, port=port,
- user=username)
- if config_credentials is not None:
- password = config_credentials['password']
- else:
- password = bzrlib.ui.ui_factory.get_password(
- prompt='SSH %(user)s@%(host)s password',
- user=username, host=host)
+ password = auth.get_password('ssh', host, username, port=port)
try:
paramiko_transport.auth_password(username, password)
except paramiko.SSHException, e:
More information about the bazaar-commits
mailing list