Changeset - 2c3941817a8e
[Not reviewed]
default
0 1 0
Andrew Shadura - 10 years ago 2016-01-30 16:36:26
andrew@shadura.me
auth: authenticate using either username or email address

Use User.get_by_username_or_email() in get_user.
In authenticate(), update username if get_user succeeds.

The point of this change is that the web login is a complex thing that
includes, apart the authentication itself, form validation and a bunch of
other things.

This change on its own makes it possible to authenticate a user using its email
address, but that on its own isn't enough for web login or git/hg auth.
1 file changed with 9 insertions and 2 deletions:
0 comments (0 inline, 0 general)
kallithea/lib/auth_modules/__init__.py
Show inline comments
 
@@ -136,14 +136,14 @@ class KallitheaAuthPluginBase(object):
 
        :param kwargs: extra arguments needed for user fetching.
 
        """
 
        user = None
 
        log.debug('Trying to fetch user `%s` from Kallithea database',
 
                  username)
 
        if username:
 
            user = User.get_by_username(username)
 
            if not user:
 
            user = User.get_by_username_or_email(username)
 
            if user is None:
 
                log.debug('Fallback to fetch user in case insensitive mode')
 
                user = User.get_by_username(username, case_insensitive=True)
 
        else:
 
            log.debug('provided username:`%s` is empty skipping...', username)
 
        return user
 

	
 
@@ -392,14 +392,21 @@ def authenticate(username, password, env
 
            log.debug('Plugin %s does not accept user `%s` for authentication',
 
                      module, user)
 
            continue
 
        else:
 
            log.debug('Plugin %s accepted user `%s` for authentication',
 
                      module, user)
 
            # The user might have tried to authenticate using their email address,
 
            # then the username variable wouldn't contain a valid username.
 
            # But as the plugin has accepted the user, .username field should
 
            # have a valid username, so use it for authentication purposes.
 
            if user is not None:
 
                username = user.username
 

	
 
        log.info('Authenticating user using %s plugin', plugin.__module__)
 

	
 
        # _authenticate is a wrapper for .auth() method of plugin.
 
        # it checks if .auth() sends proper data. For KallitheaExternalAuthPlugin
 
        # 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.
 
        user_data = plugin._authenticate(user, username, password,
0 comments (0 inline, 0 general)