'Authenticate a user based on email address as the user name.'

def authenticate(self, username=None, password=None, realm_subdomain=None, return_data=None):

    if ((username is None) or (password is None)):
        return None
    user_profile = common_get_active_user_by_email(username, return_data=return_data)
    if (user_profile is None):
        return None
    if (not password_auth_enabled(user_profile.realm)):
        if (return_data is not None):
            return_data['password_auth_disabled'] = True
        return None
    if (not email_auth_enabled(user_profile.realm)):
        if (return_data is not None):
            return_data['email_auth_disabled'] = True
        return None
    if user_profile.check_password(password):
        if (not check_subdomain(realm_subdomain, user_profile.realm.subdomain)):
            if (return_data is not None):
                return_data['invalid_subdomain'] = True
            return None
        return user_profile
    return None
