Changeset - 03afa7766ac7
[Not reviewed]
default
0 7 0
Mads Kiilerich - 10 years ago 2015-07-31 15:44:07
madski@unity3d.com
auth: consistently use the name 'user_data' for the dict with data for the authenticated user
7 files changed with 50 insertions and 50 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/auth_modules/__init__.py
Show inline comments
 
@@ -231,18 +231,18 @@ class KallitheaAuthPluginBase(object):
 
        :param passwd: plaintext password
 
        :param settings: plugin settings
 
        """
 
        auth = self.auth(userobj, username, passwd, settings, **kwargs)
 
        if auth is not None:
 
            return self._validate_auth_return(auth)
 
        user_data = self.auth(userobj, username, passwd, settings, **kwargs)
 
        if user_data is not None:
 
            return self._validate_auth_return(user_data)
 
        return None
 

	
 
    def _validate_auth_return(self, ret):
 
        if not isinstance(ret, dict):
 
    def _validate_auth_return(self, user_data):
 
        if not isinstance(user_data, dict):
 
            raise Exception('returned value from auth must be a dict')
 
        for k in self.auth_func_attrs:
 
            if k not in ret:
 
            if k not in user_data:
 
                raise Exception('Missing %s attribute from returned data' % k)
 
        return ret
 
        return user_data
 

	
 

	
 
class KallitheaExternalAuthPlugin(KallitheaAuthPluginBase):
 
@@ -257,17 +257,17 @@ class KallitheaExternalAuthPlugin(Kallit
 
        raise NotImplementedError("Not implemented in base class")
 

	
 
    def _authenticate(self, userobj, username, passwd, settings, **kwargs):
 
        auth = super(KallitheaExternalAuthPlugin, self)._authenticate(
 
        user_data = super(KallitheaExternalAuthPlugin, self)._authenticate(
 
            userobj, username, passwd, settings, **kwargs)
 
        if auth is not None:
 
        if user_data is not None:
 
            # maybe plugin will clean the username ?
 
            # we should use the return value
 
            username = auth['username']
 
            username = user_data['username']
 
            # if user is not active from our extern type we should fail to auth
 
            # this can prevent from creating users in Kallithea when using
 
            # external authentication, but if it's inactive user we shouldn't
 
            # create that user anyway
 
            if auth['active_from_extern'] is False:
 
            if user_data['active_from_extern'] is False:
 
                log.warning("User %s authenticated against %s, but is inactive"
 
                            % (username, self.__module__))
 
                return None
 
@@ -282,21 +282,21 @@ class KallitheaExternalAuthPlugin(Kallit
 
            user = UserModel().create_or_update(
 
                username=username,
 
                password=passwd,
 
                email=auth["email"],
 
                firstname=auth["firstname"],
 
                lastname=auth["lastname"],
 
                active=auth["active"],
 
                admin=auth["admin"],
 
                extern_name=auth["extern_name"],
 
                email=user_data["email"],
 
                firstname=user_data["firstname"],
 
                lastname=user_data["lastname"],
 
                active=user_data["active"],
 
                admin=user_data["admin"],
 
                extern_name=user_data["extern_name"],
 
                extern_type=self.name
 
            )
 
            Session().flush()
 
            # enforce user is just in given groups, all of them has to be ones
 
            # created from plugins. We store this info in _group_data JSON field
 
            groups = auth['groups'] or []
 
            groups = user_data['groups'] or []
 
            UserGroupModel().enforce_groups(user, groups, self.name)
 
            Session().commit()
 
        return auth
 
        return user_data
 

	
 

	
 
def importplugin(plugin):
 
@@ -359,7 +359,7 @@ def authenticate(username, password, env
 
    :param username: username can be empty for container auth
 
    :param password: password can be empty for container auth
 
    :param environ: environ headers passed for container auth
 
    :returns: None if auth failed, plugin_user dict if auth is correct
 
    :returns: None if auth failed, user_data dict if auth is correct
 
    """
 

	
 
    auth_plugins = Setting.get_auth_plugins()
 
@@ -403,14 +403,14 @@ def authenticate(username, password, env
 
        # it also maps users to Database and maps the attributes returned
 
        # from .auth() to Kallithea database. If this function returns data
 
        # then auth is correct.
 
        plugin_user = plugin._authenticate(user, username, password,
 
        user_data = plugin._authenticate(user, username, password,
 
                                           plugin_settings,
 
                                           environ=environ or {})
 
        log.debug('PLUGIN USER DATA: %s' % plugin_user)
 
        log.debug('PLUGIN USER DATA: %s' % user_data)
 

	
 
        if plugin_user is not None:
 
        if user_data is not None:
 
            log.debug('Plugin returned proper authentication data')
 
            return plugin_user
 
            return user_data
 

	
 
        # we failed to Auth because .auth() method didn't return the user
 
        if username:
kallithea/lib/auth_modules/auth_container.py
Show inline comments
 
@@ -177,7 +177,7 @@ class KallitheaAuthPlugin(auth_modules.K
 
        lastname = getattr(userobj, 'lastname', '')
 
        extern_type = getattr(userobj, 'extern_type', '')
 

	
 
        user_attrs = {
 
        user_data = {
 
            'username': username,
 
            'firstname': safe_unicode(firstname or username),
 
            'lastname': safe_unicode(lastname or ''),
 
@@ -190,5 +190,5 @@ class KallitheaAuthPlugin(auth_modules.K
 
            'extern_type': extern_type,
 
        }
 

	
 
        log.info('user `%s` authenticated correctly' % user_attrs['username'])
 
        return user_attrs
 
        log.info('user `%s` authenticated correctly' % user_data['username'])
 
        return user_data
kallithea/lib/auth_modules/auth_crowd.py
Show inline comments
 
@@ -222,7 +222,7 @@ class KallitheaAuthPlugin(auth_modules.K
 
        lastname = getattr(userobj, 'lastname', '')
 
        extern_type = getattr(userobj, 'extern_type', '')
 

	
 
        user_attrs = {
 
        user_data = {
 
            'username': username,
 
            'firstname': crowd_user["first-name"] or firstname,
 
            'lastname': crowd_user["last-name"] or lastname,
 
@@ -237,8 +237,8 @@ class KallitheaAuthPlugin(auth_modules.K
 

	
 
        # set an admin if we're in admin_groups of crowd
 
        for group in settings["admin_groups"].split(","):
 
            if group in user_attrs["groups"]:
 
                user_attrs["admin"] = True
 
        log.debug("Final crowd user object: \n%s" % (formatted_json(user_attrs)))
 
        log.info('user %s authenticated correctly' % user_attrs['username'])
 
        return user_attrs
 
            if group in user_data["groups"]:
 
                user_data["admin"] = True
 
        log.debug("Final crowd user object: \n%s" % (formatted_json(user_data)))
 
        log.info('user %s authenticated correctly' % user_data['username'])
 
        return user_data
kallithea/lib/auth_modules/auth_internal.py
Show inline comments
 
@@ -68,7 +68,7 @@ class KallitheaAuthPlugin(auth_modules.K
 
                     % (userobj, userobj.extern_type, self.name))
 
            return None
 

	
 
        user_attrs = {
 
        user_data = {
 
            "username": userobj.username,
 
            "firstname": userobj.firstname,
 
            "lastname": userobj.lastname,
 
@@ -81,18 +81,18 @@ class KallitheaAuthPlugin(auth_modules.K
 
            'extern_type': userobj.extern_type,
 
        }
 

	
 
        log.debug(formatted_json(user_attrs))
 
        log.debug(formatted_json(user_data))
 
        if userobj.active:
 
            from kallithea.lib import auth
 
            password_match = auth.KallitheaCrypto.hash_check(password, userobj.password)
 
            if userobj.username == User.DEFAULT_USER and userobj.active:
 
                log.info('user %s authenticated correctly as anonymous user' %
 
                         username)
 
                return user_attrs
 
                return user_data
 

	
 
            elif userobj.username == username and password_match:
 
                log.info('user %s authenticated correctly' % user_attrs['username'])
 
                return user_attrs
 
                log.info('user %s authenticated correctly' % user_data['username'])
 
                return user_data
 
            log.error("user %s had a bad password" % username)
 
            return None
 
        else:
kallithea/lib/auth_modules/auth_ldap.py
Show inline comments
 
@@ -340,7 +340,7 @@ class KallitheaAuthPlugin(auth_modules.K
 
            lastname = getattr(userobj, 'lastname', '')
 
            extern_type = getattr(userobj, 'extern_type', '')
 

	
 
            user_attrs = {
 
            user_data = {
 
                'username': username,
 
                'firstname': safe_unicode(get_ldap_attr('attr_firstname') or firstname),
 
                'lastname': safe_unicode(get_ldap_attr('attr_lastname') or lastname),
 
@@ -352,8 +352,8 @@ class KallitheaAuthPlugin(auth_modules.K
 
                'extern_name': user_dn,
 
                'extern_type': extern_type,
 
            }
 
            log.info('user %s authenticated correctly' % user_attrs['username'])
 
            return user_attrs
 
            log.info('user %s authenticated correctly' % user_data['username'])
 
            return user_data
 

	
 
        except (LdapUsernameError, LdapPasswordError, LdapImportError):
 
            log.error(traceback.format_exc())
kallithea/lib/auth_modules/auth_pam.py
Show inline comments
 
@@ -111,7 +111,7 @@ class KallitheaAuthPlugin(auth_modules.K
 
        lastname = getattr(userobj, 'lastname', '')
 
        extern_type = getattr(userobj, 'extern_type', '')
 

	
 
        user_attrs = {
 
        user_data = {
 
            'username': username,
 
            'firstname': firstname,
 
            'lastname': lastname,
 
@@ -129,12 +129,12 @@ class KallitheaAuthPlugin(auth_modules.K
 
            regex = settings["gecos"]
 
            match = re.search(regex, user_data.pw_gecos)
 
            if match:
 
                user_attrs["firstname"] = match.group('first_name')
 
                user_attrs["lastname"] = match.group('last_name')
 
                user_data["firstname"] = match.group('first_name')
 
                user_data["lastname"] = match.group('last_name')
 
        except Exception:
 
            log.warning("Cannot extract additional info for PAM user %s", username)
 
            pass
 

	
 
        log.debug("pamuser: \n%s" % formatted_json(user_attrs))
 
        log.info('user %s authenticated correctly' % user_attrs['username'])
 
        return user_attrs
 
        log.debug("pamuser: \n%s" % formatted_json(user_data))
 
        log.info('user %s authenticated correctly' % user_data['username'])
 
        return user_data
kallithea/lib/base.py
Show inline comments
 
@@ -408,13 +408,13 @@ class BaseController(WSGIController):
 
            for name in Setting.get_auth_plugins()
 
        ):
 
            try:
 
                auth_info = auth_modules.authenticate('', '', request.environ)
 
                user_info = auth_modules.authenticate('', '', request.environ)
 
            except UserCreationError as e:
 
                from kallithea.lib import helpers as h
 
                h.flash(e, 'error', logf=log.error)
 
            else:
 
                if auth_info is not None:
 
                    username = auth_info['username']
 
                if user_info is not None:
 
                    username = user_info['username']
 
                    user = User.get_by_username(username, case_insensitive=True)
 
                    return log_in_user(user, remember=False,
 
                                       is_external_auth=True)
0 comments (0 inline, 0 general)