def authenticate(self, request, remote_user=None):<EOL>
if not remote_user:<EOL><INDENT>remote_user = request<EOL><DEDENT>if not remote_user:<EOL><INDENT>return None<EOL><DEDENT>user = None<EOL>username = self.clean_username(remote_user)<EOL>try:<EOL><INDENT>if self.create_unknown_user:<EOL><INDENT>defaults = {}<EOL>if isinstance(request, dict):<EOL><INDENT>session_data = request<EOL>if '<STR_LIT>' in session_data:<EOL><INDENT>first_name, _, last_name = full_name_natural_split(<EOL>session_data['<STR_LIT>'])<EOL>defaults.update({<EOL>'<STR_LIT>': first_name,<EOL>'<STR_LIT>': last_name<EOL>})<EOL><DEDENT>for key in ('<STR_LIT:email>', '<STR_LIT>', '<STR_LIT>'):<EOL><INDENT>if key in session_data:<EOL><INDENT>defaults.update({key: session_data[key]})<EOL><DEDENT><DEDENT><DEDENT>user, created = UserModel._default_manager.get_or_create(**{<EOL>UserModel.USERNAME_FIELD: username,<EOL>'<STR_LIT>': defaults,<EOL>})<EOL>if created:<EOL><INDENT>LOGGER.debug("<STR_LIT>", username)<EOL>user = self.configure_user(user)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>try:<EOL><INDENT>user = UserModel._default_manager.get_by_natural_key(<EOL>username)<EOL><DEDENT>except UserModel.DoesNotExist:<EOL><INDENT>pass<EOL><DEDENT><DEDENT><DEDENT>except DatabaseError as err:<EOL><INDENT>LOGGER.debug("<STR_LIT>", err)<EOL>for user in six.itervalues(self.users):<EOL><INDENT>LOGGER.debug("<STR_LIT>",<EOL>username, user.id, user.username)<EOL>if user.username == username:<EOL><INDENT>LOGGER.debug("<STR_LIT>", user.id, user.username)<EOL>return user<EOL><DEDENT><DEDENT>user = UserModel(<EOL>id=random.randint(<NUM_LIT:1>, (<NUM_LIT:1> << <NUM_LIT:32>) - <NUM_LIT:1>), username=username)<EOL>LOGGER.debug("<STR_LIT>",<EOL>user.id, user.username)<EOL>self.users[user.id] = user<EOL><DEDENT>return user if self.user_can_authenticate(user) else None<EOL>
The ``username`` passed here is considered trusted.  This
method simply returns the ``User`` object with the given username.

In order to support older Django versions
(before commit 4b9330ccc04575f9e5126529ec355a450d12e77c), if username
is None, we will assume request is the ``remote_user`` parameter.