authentication: run modernize for python3

This commit is contained in:
RhodeCode Admin 2023-07-18 14:26:37 +02:00
parent 4026ee36ee
commit a6e9ccf9e0
17 changed files with 34 additions and 65 deletions

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -162,7 +162,7 @@ class RhodeCodeAuthPluginBase(object):
# TODO: johbo: Using the name here is problematic. It would be good to
# introduce either new models in the database to hold Plugin and
# PluginSetting or to use the plugin id here.
return 'auth_{}_{}'.format(self.name, name)
return f'auth_{self.name}_{name}'
def _get_setting_type(self, name):
"""
@ -175,7 +175,7 @@ class RhodeCodeAuthPluginBase(object):
db_type = self._settings_type_map.get(
type(schema_node.typ), 'unicode')
if name in self._settings_encrypted:
db_type = '{}.encrypted'.format(db_type)
db_type = f'{db_type}.encrypted'
return db_type
@classmethod
@ -249,7 +249,7 @@ class RhodeCodeAuthPluginBase(object):
"""
Returns a plugin setting by name.
"""
full_name = 'rhodecode_{}'.format(self._get_setting_full_name(name))
full_name = f'rhodecode_{self._get_setting_full_name(name)}'
if plugin_cached_settings:
plugin_settings = plugin_cached_settings
else:
@ -504,7 +504,7 @@ class RhodeCodeExternalAuthPlugin(RhodeCodeAuthPluginBase):
def _authenticate(self, userobj, username, passwd, settings, **kwargs):
# at this point _authenticate calls plugin's `auth()` function
auth = super(RhodeCodeExternalAuthPlugin, self)._authenticate(
auth = super()._authenticate(
userobj, username, passwd, settings, **kwargs)
if auth:
@ -602,14 +602,14 @@ class AuthLdapBase(object):
"""
host = host.strip()
if not full_resolve:
return '{}:{}'.format(host, port)
return f'{host}:{port}'
log.debug('LDAP: Resolving IP for LDAP host `%s`', host)
try:
ip = socket.gethostbyname(host)
log.debug('LDAP: Got LDAP host `%s` ip %s', host, ip)
except Exception:
raise LdapConnectionError('Failed to resolve host: `{}`'.format(host))
raise LdapConnectionError(f'Failed to resolve host: `{host}`')
log.debug('LDAP: Checking if IP %s is accessible', ip)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -619,9 +619,9 @@ class AuthLdapBase(object):
log.debug('LDAP: connection to %s successful', ip)
except Exception:
raise LdapConnectionError(
'Failed to connect to host: `{}:{}`'.format(host, port))
f'Failed to connect to host: `{host}:{port}`')
return '{}:{}'.format(host, port)
return f'{host}:{port}'
if len(ldap_server) == 1:
# in case of single server use resolver to detect potential
@ -651,7 +651,7 @@ class AuthLdapBase(object):
def validate_username(cls, username):
if "," in username:
raise LdapUsernameError(
"invalid character `,` in username: `{}`".format(username))
f"invalid character `,` in username: `{username}`")
@classmethod
def validate_password(cls, username, password):
@ -751,7 +751,7 @@ def authenticate(username, password, environ=None, auth_type=None,
user_id = user.user_id if user else 'no-user'
# don't cache for empty users
plugin_cache_active = plugin_cache_active and user_id
cache_namespace_uid = 'cache_user_auth.{}'.format(user_id)
cache_namespace_uid = f'cache_user_auth.{user_id}'
region = rc_cache.get_or_create_region('cache_perms', cache_namespace_uid)
@region.conditional_cache_on_arguments(namespace=cache_namespace_uid,
@ -793,7 +793,7 @@ def authenticate(username, password, environ=None, auth_type=None,
elapsed_time_ms = round(1000.0 * auth_time) # use ms only
statsd.incr('rhodecode_login_success_total')
statsd.timing("rhodecode_login_timing.histogram", elapsed_time_ms,
tags=["plugin:{}".format(plugin.get_id())],
tags=[f"plugin:{plugin.get_id()}"],
use_decimals=False
)
return plugin_user

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -141,7 +139,7 @@ class CrowdServer(object):
_headers = {"Content-type": "application/json",
"Accept": "application/json"}
if self.user and self.passwd:
authstring = base64.b64encode("%s:%s" % (self.user, self.passwd))
authstring = base64.b64encode("{}:{}".format(self.user, self.passwd))
_headers["Authorization"] = "Basic %s" % authstring
if headers:
_headers.update(headers)
@ -170,7 +168,7 @@ class CrowdServer(object):
if not noformat:
ret_val = {"status": False,
"body": body,
"error": "{}\n{}".format(e, msg)}
"error": f"{e}\n{msg}"}
else:
ret_val = None
return ret_val
@ -224,7 +222,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
@hybrid_property
def name(self):
return u"crowd"
return "crowd"
def use_fake_password(self):
return True
@ -293,5 +291,5 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
def includeme(config):
plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid)
plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}'
plugin_factory(plugin_id).includeme(config)

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -104,7 +102,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
@hybrid_property
def name(self):
return u"headers"
return "headers"
@property
def is_headers_auth(self):
@ -159,7 +157,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
settings = kwargs.get('settings') or {}
username = self._get_username(environ, settings)
# we got the username, so use default method now
return super(RhodeCodeAuthPlugin, self).get_user(username)
return super().get_user(username)
def auth(self, userobj, username, password, settings, **kwargs):
"""
@ -229,5 +227,5 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
def includeme(config):
plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid)
plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}'
plugin_factory(plugin_id).includeme(config)

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -96,7 +94,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
@hybrid_property
def name(self):
return u"jasig-cas"
return "jasig-cas"
@property
def is_headers_auth(self):
@ -171,5 +169,5 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
def includeme(config):
plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid)
plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}'
plugin_factory(plugin_id).includeme(config)

View file

@ -1,4 +1,3 @@
# Copyright (C) 2010-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -223,7 +222,7 @@ class AuthLdap(AuthLdapBase):
break
else:
raise LdapPasswordError(
'Failed to authenticate user `{}` with given password'.format(username))
f'Failed to authenticate user `{username}` with given password')
except ldap.NO_SUCH_OBJECT:
log.debug("LDAP says no such user '%s' (%s), org_exc:",
@ -429,7 +428,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
@hybrid_property
def name(self):
return u"ldap"
return "ldap"
def use_fake_password(self):
return True
@ -547,5 +546,5 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
def includeme(config):
plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid)
plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}'
plugin_factory(plugin_id).includeme(config)

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -104,7 +102,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
@hybrid_property
def name(self):
return u"pam"
return "pam"
def get_settings_schema(self):
return PamSettingsSchema()
@ -126,7 +124,7 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
log.debug('Got PAM response %s', auth_result)
# old attrs fetched from RhodeCode database
default_email = "%s@%s" % (username, socket.gethostname())
default_email = "{}@{}".format(username, socket.gethostname())
admin = getattr(userobj, 'admin', False)
active = getattr(userobj, 'active', True)
email = getattr(userobj, 'email', '') or default_email
@ -168,5 +166,5 @@ class RhodeCodeAuthPlugin(RhodeCodeExternalAuthPlugin):
def includeme(config):
plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid)
plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}'
plugin_factory(plugin_id).includeme(config)

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -84,7 +82,7 @@ class RhodeCodeAuthPlugin(RhodeCodeAuthPluginBase):
@hybrid_property
def name(self):
return u"rhodecode"
return "rhodecode"
def user_activation_state(self):
def_user_perms = User.get_default_user().AuthUser().permissions['global']
@ -98,7 +96,7 @@ class RhodeCodeAuthPlugin(RhodeCodeAuthPluginBase):
We know that user exists in our database.
"""
allows_non_existing_user = False
return super(RhodeCodeAuthPlugin, self).allows_authentication_from(
return super().allows_authentication_from(
user, allows_non_existing_user=allows_non_existing_user)
def auth(self, userobj, username, password, settings, **kwargs):
@ -218,5 +216,5 @@ class RhodeCodeSettingsSchema(AuthnPluginSettingsSchemaBase):
def includeme(config):
plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid)
plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}'
plugin_factory(plugin_id).includeme(config)

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -82,7 +80,7 @@ class RhodeCodeAuthPlugin(RhodeCodeAuthPluginBase):
@hybrid_property
def name(self):
return u"authtoken"
return "authtoken"
def user_activation_state(self):
def_user_perms = User.get_default_user().AuthUser().permissions['global']
@ -107,7 +105,7 @@ class RhodeCodeAuthPlugin(RhodeCodeAuthPluginBase):
# only for vcs operations
allowed_auth_sources = [VCS_TYPE]
return super(RhodeCodeAuthPlugin, self).allows_authentication_from(
return super().allows_authentication_from(
user, allows_non_existing_user=False,
allowed_auth_plugins=allowed_auth_plugins,
allowed_auth_sources=allowed_auth_sources)
@ -157,7 +155,7 @@ class RhodeCodeAuthPlugin(RhodeCodeAuthPluginBase):
def includeme(config):
plugin_id = 'egg:rhodecode-enterprise-ce#{}'.format(RhodeCodeAuthPlugin.uid)
plugin_id = f'egg:rhodecode-enterprise-ce#{RhodeCodeAuthPlugin.uid}'
plugin_factory(plugin_id).includeme(config)

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -121,7 +119,7 @@ class AuthnRootResource(AuthnResourceBase):
counter = 1
current = name
while current in self._store.keys():
current = '{}{}'.format(name, counter)
current = f'{name}{counter}'
counter += 1
if counter > limit:
raise ConfigurationError(

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify

View file

@ -1,5 +1,3 @@
# Copyright (C) 2016-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
@ -30,7 +28,7 @@ from rhodecode.apps._base import ADMIN_PREFIX
class TestAuthenticationSettings:
def test_auth_settings_global_view_get(self, app):
url = '{prefix}/auth/'.format(prefix=ADMIN_PREFIX)
url = f'{ADMIN_PREFIX}/auth/'
response = app.get(url)
assert response.status_code == 200

View file

@ -1,5 +1,3 @@
# Copyright (C) 2012-2023 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify