def authenticate(self, request, remote_user=None):
        if not remote_user:
            remote_user = request
        if not remote_user:
            return None
        user = None
        username = self.clean_username(remote_user)
        try:
            if self.create_unknown_user:
                defaults = {}
                if isinstance(request, dict):
                    session_data = request
                    if 'full_name' in session_data:
                        first_name, _, last_name = full_name_natural_split(
                            session_data['full_name'])
                        defaults.update({
                            'first_name': first_name,
                            'last_name': last_name
                        })
                    for key in ('email', 'first_name', 'last_name'):
                        if key in session_data:
                            defaults.update({key: session_data[key]})
                user, created = UserModel._default_manager.get_or_create(**{
                    UserModel.USERNAME_FIELD: username,
                    'defaults': defaults,
                })
                if created:
                    LOGGER.debug("created user '%s' in database.", username)
                    user = self.configure_user(user)
            else:
                try:
                    user = UserModel._default_manager.get_by_natural_key(
                        username)
                except UserModel.DoesNotExist:
                    pass
        except DatabaseError as err:
            LOGGER.debug("User table missing from database? (err:%s)", err)
            for user in six.itervalues(self.users):
                LOGGER.debug("match %s with User(id=%d, username=%s)",
                    username, user.id, user.username)
                if user.username == username:
                    LOGGER.debug("found %d %s", user.id, user.username)
                    return user
            user = UserModel(
                id=random.randint(1, (1 << 32) - 1), username=username)
            LOGGER.debug("add User(id=%d, username=%s) to cache.",
                user.id, user.username)
            self.users[user.id] = user
        return user if self.user_can_authenticate(user) else None