177
174
"""Get a generic option - no special process, no default."""
178
175
return self._get_user_option(option_name)
180
def get_user_option_as_bool(self, option_name):
181
"""Get a generic option as a boolean - no special process, no default.
183
:return None if the option doesn't exist or its value can't be
184
interpreted as a boolean. Returns True or False ortherwise.
186
s = self._get_user_option(option_name)
187
return ui.bool_from_string(s)
189
177
def gpg_signing_command(self):
190
178
"""What program should be used to sign signatures?"""
191
179
result = self._gpg_signing_command()
1087
1079
trace.mutter("Using authentication section: %r", auth_def_name)
1090
if credentials is None:
1091
# No credentials were found in authentication.conf, try the fallback
1092
# credentials stores.
1093
credentials = credential_store_registry.get_fallback_credentials(
1094
scheme, host, port, user, path, realm)
1096
1082
return credentials
1098
1084
def set_credentials(self, name, host, user, scheme=None, password=None,
1140
1126
config.update({name: values})
1143
def get_user(self, scheme, host, port=None, realm=None, path=None,
1144
prompt=None, ask=False, default=None):
1129
def get_user(self, scheme, host, port=None,
1130
realm=None, path=None, prompt=None):
1145
1131
"""Get a user from authentication file.
1147
1133
:param scheme: protocol
1167
1148
user = credentials['user']
1173
# Create a default prompt suitable for most cases
1174
prompt = scheme.upper() + ' %(host)s username'
1175
# Special handling for optional fields in the prompt
1176
if port is not None:
1177
prompt_host = '%s:%d' % (host, port)
1180
user = ui.ui_factory.get_username(prompt, host=prompt_host)
1185
1153
def get_password(self, scheme, host, user, port=None,
1240
1208
A credential store provides access to credentials via the password_encoding
1241
1209
field in authentication.conf sections.
1243
Except for stores provided by bzr itself, most stores are expected to be
1211
Except for stores provided by bzr itself,most stores are expected to be
1244
1212
provided by plugins that will therefore use
1245
1213
register_lazy(password_encoding, module_name, member_name, help=help,
1246
fallback=fallback) to install themselves.
1248
A fallback credential store is one that is queried if no credentials can be
1249
found via authentication.conf.
1214
info=info) to install themselves.
1252
1217
def get_credential_store(self, encoding=None):
1258
def is_fallback(self, name):
1259
"""Check if the named credentials store should be used as fallback."""
1260
return self.get_info(name)
1262
def get_fallback_credentials(self, scheme, host, port=None, user=None,
1263
path=None, realm=None):
1264
"""Request credentials from all fallback credentials stores.
1266
The first credentials store that can provide credentials wins.
1269
for name in self.keys():
1270
if not self.is_fallback(name):
1272
cs = self.get_credential_store(name)
1273
credentials = cs.get_credentials(scheme, host, port, user,
1275
if credentials is not None:
1276
# We found some credentials
1280
def register(self, key, obj, help=None, override_existing=False,
1282
"""Register a new object to a name.
1284
:param key: This is the key to use to request the object later.
1285
:param obj: The object to register.
1286
:param help: Help text for this entry. This may be a string or
1287
a callable. If it is a callable, it should take two
1288
parameters (registry, key): this registry and the key that
1289
the help was registered under.
1290
:param override_existing: Raise KeyErorr if False and something has
1291
already been registered for that key. If True, ignore if there
1292
is an existing key (always register the new value).
1293
:param fallback: Whether this credential store should be
1296
return super(CredentialStoreRegistry,
1297
self).register(key, obj, help, info=fallback,
1298
override_existing=override_existing)
1300
def register_lazy(self, key, module_name, member_name,
1301
help=None, override_existing=False,
1303
"""Register a new credential store to be loaded on request.
1305
:param module_name: The python path to the module. Such as 'os.path'.
1306
:param member_name: The member of the module to return. If empty or
1307
None, get() will return the module itself.
1308
:param help: Help text for this entry. This may be a string or
1310
:param override_existing: If True, replace the existing object
1311
with the new one. If False, if there is already something
1312
registered with the same key, raise a KeyError
1313
:param fallback: Whether this credential store should be
1316
return super(CredentialStoreRegistry, self).register_lazy(
1317
key, module_name, member_name, help,
1318
info=fallback, override_existing=override_existing)
1321
1224
credential_store_registry = CredentialStoreRegistry()
1325
1228
"""An abstract class to implement storage for credentials"""
1327
1230
def decode_password(self, credentials):
1328
"""Returns a clear text password for the provided credentials."""
1231
"""Returns a password for the provided credentials in clear text."""
1329
1232
raise NotImplementedError(self.decode_password)
1331
def get_credentials(self, scheme, host, port=None, user=None, path=None,
1333
"""Return the matching credentials from this credential store.
1335
This method is only called on fallback credential stores.
1337
raise NotImplementedError(self.get_credentials)
1341
1235
class PlainTextCredentialStore(CredentialStore):
1342
1236
"""Plain text credential store for the authentication.conf file."""
1354
1248
class BzrDirConfig(object):
1356
def __init__(self, bzrdir):
1357
self._bzrdir = bzrdir
1358
self._config = bzrdir._get_config()
1250
def __init__(self, transport):
1251
self._config = TransportConfig(transport, 'control.conf')
1360
1253
def set_default_stack_on(self, value):
1361
1254
"""Set the default stacking location.
1432
1321
configobj.setdefault(section, {})[name] = value
1433
1322
self._set_configobj(configobj)
1435
def _get_config_file(self):
1324
def _get_configobj(self):
1437
return self._transport.get(self._filename)
1326
return ConfigObj(self._transport.get(self._filename),
1438
1328
except errors.NoSuchFile:
1441
def _get_configobj(self):
1442
return ConfigObj(self._get_config_file(), encoding='utf-8')
1329
return ConfigObj(encoding='utf-8')
1444
1331
def _set_configobj(self, configobj):
1445
1332
out_file = StringIO()