ldap: small fixes and improvements over ldap authentication

This commit is contained in:
RhodeCode Admin 2023-08-21 09:40:30 +02:00
parent 6a2f266d53
commit c8b35a769a
4 changed files with 19 additions and 11 deletions

View file

@ -477,7 +477,7 @@ class RhodeCodeAuthPluginBase(object):
if isinstance(self.AUTH_CACHE_TTL, int):
# plugin cache set inside is more important than the settings value
cache_ttl = self.AUTH_CACHE_TTL
elif plugin_settings.get('cache_ttl'):
elif 'cache_ttl' in plugin_settings:
cache_ttl = safe_int(plugin_settings.get('cache_ttl'), 0)
plugin_cache_active = bool(cache_ttl and cache_ttl > 0)
@ -637,7 +637,7 @@ class AuthLdapBase(object):
@classmethod
def _get_server_list(cls, servers):
return map(string.strip, servers.split(','))
return [s.strip() for s in servers.split(',')]
@classmethod
def get_uid(cls, username, server_addresses):

View file

@ -196,7 +196,7 @@ class AuthLdap(AuthLdapBase):
ldap_conn = None
try:
ldap_conn = self._get_ldap_conn()
filter_ = '(&%s(%s=%s))' % (
filter_ = '(&{}({}={}))'.format(
self.LDAP_FILTER, self.attr_login, username)
log.debug("Authenticating %r filter %s and scope: %s",
self.BASE_DN, filter_, scope_label)
@ -446,8 +446,8 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
org_bind = current_args['bind_dn']
passwd = current_args['bind_pass']
def has_bind_marker(username):
if self.DYNAMIC_BIND_VAR in username:
def has_bind_marker(_username):
if self.DYNAMIC_BIND_VAR in _username:
return True
# we only passed in user with "special" variable
@ -499,12 +499,12 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
log.debug('Checking for ldap authentication.')
try:
aldap = AuthLdap(**ldap_args)
(user_dn, ldap_attrs) = aldap.authenticate_ldap(username, password)
auth_ldap = AuthLdap(**ldap_args)
(user_dn, ldap_attrs) = auth_ldap.authenticate_ldap(username, password)
log.debug('Got ldap DN response %s', user_dn)
def get_ldap_attr(k):
return ldap_attrs.get(settings.get(k), [''])[0]
def get_ldap_attr(k) -> str:
return safe_str(ldap_attrs.get(settings.get(k), [b''])[0])
# old attrs fetched from RhodeCode database
admin = getattr(userobj, 'admin', False)
@ -519,8 +519,8 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
user_attrs = {
'username': username,
'firstname': safe_str(get_ldap_attr('attr_firstname') or firstname),
'lastname': safe_str(get_ldap_attr('attr_lastname') or lastname),
'firstname': get_ldap_attr('attr_firstname') or firstname,
'lastname': get_ldap_attr('attr_lastname') or lastname,
'groups': groups,
'user_group_sync': False,
'email': get_ldap_attr('attr_email') or email,

View file

@ -99,6 +99,9 @@ class AuthnPluginViewBase(BaseAppView):
Session().commit()
SettingsModel().invalidate_settings_cache()
authn_registry = get_authn_registry(self.request.registry)
authn_registry.invalidate_auth_plugins_cache()
# Display success message and redirect.
h.flash(_('Auth settings updated successfully.'), category='success')
redirect_to = self.request.resource_path(self.context, route_name='auth_home')
@ -173,6 +176,8 @@ class AuthSettingsView(BaseAppView):
h.flash(_('Error occurred during update of auth settings.'),
category='error')
authn_registry = get_authn_registry(self.request.registry)
authn_registry.invalidate_auth_plugins_cache()
redirect_to = self.request.resource_path(self.context, route_name='auth_home')
return HTTPFound(redirect_to)

View file

@ -158,6 +158,9 @@ def detect_vcs_request(environ, backends):
# e.g /_file_store/download
'_file_store++',
# login
"_admin/login",
# _admin/api is safe too
'_admin/api',