Get rid of this 'help' madness and more generally this three-tuppled containing various tuples and weird stuff ... use a simple dict everywhere instead

This commit is contained in:
Alexandre Aubin 2019-08-20 05:41:11 +02:00
parent cad2cd8006
commit 65fe685a90
5 changed files with 22 additions and 34 deletions

View file

@ -33,23 +33,20 @@ class Authenticator(BaseAuthenticator):
""" """
def __init__(self, name, uri, base_dn, user_rdn=None): def __init__(self, name, vendor, parameters, extra):
self.uri = parameters["uri"]
self.basedn = parameters["base_dn"]
self.userdn = parameters["user_rdn"]
self.extra = extra
logger.debug("initialize authenticator '%s' with: uri='%s', " logger.debug("initialize authenticator '%s' with: uri='%s', "
"base_dn='%s', user_rdn='%s'", name, uri, base_dn, user_rdn) "base_dn='%s', user_rdn='%s'", name, self.uri, self.basedn, self.userdn)
super(Authenticator, self).__init__(name) super(Authenticator, self).__init__(name)
self.uri = uri if self.userdn:
self.basedn = base_dn if 'cn=external,cn=auth' in self.userdn:
if user_rdn:
self.userdn = user_rdn
if 'cn=external,cn=auth' in user_rdn:
self.authenticate(None) self.authenticate(None)
else: else:
self.con = None self.con = None
else:
# Initialize anonymous usage
self.userdn = ''
self.authenticate(None)
def __del__(self): def __del__(self):
"""Disconnect and free ressources""" """Disconnect and free ressources"""

View file

@ -287,7 +287,7 @@ class MoulinetteSignals(object):
"""The list of available signals""" """The list of available signals"""
signals = {'authenticate', 'prompt', 'display'} signals = {'authenticate', 'prompt', 'display'}
def authenticate(self, authenticator, help): def authenticate(self, authenticator):
"""Process the authentication """Process the authentication
Attempt to authenticate to the given authenticator and return Attempt to authenticate to the given authenticator and return
@ -297,7 +297,6 @@ class MoulinetteSignals(object):
Keyword arguments: Keyword arguments:
- authenticator -- The authenticator object to use - authenticator -- The authenticator object to use
- help -- The translation key of the authenticator's help message
Returns: Returns:
The authenticator object The authenticator object
@ -305,7 +304,7 @@ class MoulinetteSignals(object):
""" """
if authenticator.is_authenticated: if authenticator.is_authenticated:
return authenticator return authenticator
return self._authenticate(authenticator, help) return self._authenticate(authenticator)
def prompt(self, message, is_password=False, confirm=False): def prompt(self, message, is_password=False, confirm=False):
"""Prompt for a value """Prompt for a value
@ -397,7 +396,7 @@ def init_interface(name, kwargs={}, actionsmap={}):
return interface(amap, **kwargs) return interface(amap, **kwargs)
def init_authenticator(vendor_and_name, kwargs={}): def init_authenticator(auth_conf):
"""Return a new authenticator instance """Return a new authenticator instance
Retrieve the given authenticator vendor and return a new instance of Retrieve the given authenticator vendor and return a new instance of
@ -409,15 +408,13 @@ def init_authenticator(vendor_and_name, kwargs={}):
- kwargs -- A dict of arguments for the authenticator profile - kwargs -- A dict of arguments for the authenticator profile
""" """
(vendor, name) = vendor_and_name
try: try:
mod = import_module('moulinette.authenticators.%s' % vendor) mod = import_module('moulinette.authenticators.%s' % auth_conf["vendor"])
except ImportError: except ImportError:
logger.exception("unable to load authenticator vendor '%s'", vendor) logger.exception("unable to load authenticator vendor '%s'", auth_conf["vendor"])
raise MoulinetteError('error_see_log') raise MoulinetteError('error_see_log')
else: else:
return mod.Authenticator(name, **kwargs) return mod.Authenticator(**auth_conf)
def clean_session(session_id, profiles=[]): def clean_session(session_id, profiles=[]):
"""Clean a session cache """Clean a session cache

View file

@ -266,17 +266,10 @@ class BaseActionsMapParser(object):
else: else:
auths = {} auths = {}
for auth_name, auth_conf in auth.items(): for auth_name, auth_conf in auth.items():
# Add authenticator profile as a 3-tuple auths[auth_name] = {'name': auth_name,
# (identifier, configuration, parameters) with 'vendor': auth_conf.get('vendor'),
# - identifier: the authenticator vendor and its 'parameters': auth_conf.get('parameters', {}),
# profile name as a 2-tuple 'extra': {'help': auth_conf.get('help', None)}}
# - configuration: a dict of additional global
# configuration (i.e. 'help')
# - parameters: a dict of arguments for the
# authenticator profile
auths[auth_name] = ((auth_conf.get('vendor'), auth_name),
{'help': auth_conf.get('help', None)},
auth_conf.get('parameters', {}))
conf['authenticator'] = auths conf['authenticator'] = auths
else: else:
logger.error("expecting a dict of profile(s) or a profile name " logger.error("expecting a dict of profile(s) or a profile name "

View file

@ -461,7 +461,7 @@ class _ActionsMapPlugin(object):
# Signals handlers # Signals handlers
def _do_authenticate(self, authenticator, help): def _do_authenticate(self, authenticator):
"""Process the authentication """Process the authentication
Handle the core.MoulinetteSignals.authenticate signal. Handle the core.MoulinetteSignals.authenticate signal.

View file

@ -435,7 +435,7 @@ class Interface(BaseInterface):
# Set handler for authentication # Set handler for authentication
if password: if password:
msignals.set_handler('authenticate', msignals.set_handler('authenticate',
lambda a, h: a(password=password)) lambda a: a(password=password))
try: try:
ret = self.actionsmap.process(args, timeout=timeout) ret = self.actionsmap.process(args, timeout=timeout)
@ -460,13 +460,14 @@ class Interface(BaseInterface):
# Signals handlers # Signals handlers
def _do_authenticate(self, authenticator, help): def _do_authenticate(self, authenticator):
"""Process the authentication """Process the authentication
Handle the core.MoulinetteSignals.authenticate signal. Handle the core.MoulinetteSignals.authenticate signal.
""" """
# TODO: Allow token authentication? # TODO: Allow token authentication?
help = authenticator.extra.get("help")
msg = m18n.n(help) if help else m18n.g('password') msg = m18n.n(help) if help else m18n.g('password')
return authenticator(password=self._do_prompt(msg, True, False, return authenticator(password=self._do_prompt(msg, True, False,
color='yellow')) color='yellow'))