MCPcopy
hub / github.com/django/django / process_request

Method process_request

django/contrib/auth/middleware.py:134–170  ·  view source on GitHub ↗
(self, request)

Source from the content-addressed store, hash-verified

132 return self.get_response(request)
133
134 def process_request(self, request):
135 # AuthenticationMiddleware is required so that request.user exists.
136 if not hasattr(request, "user"):
137 raise ImproperlyConfigured(
138 "The Django remote user auth middleware requires the"
139 " authentication middleware to be installed. Edit your"
140 " MIDDLEWARE setting to insert"
141 " 'django.contrib.auth.middleware.AuthenticationMiddleware'"
142 f" before the {self.__class__.__name__} class."
143 )
144 try:
145 username = self.get_username(request)
146 except KeyError:
147 # If specified header doesn't exist then remove any existing
148 # authenticated remote-user, or return (leaving request.user set to
149 # AnonymousUser by the AuthenticationMiddleware).
150 if self.force_logout_if_no_header and request.user.is_authenticated:
151 self._remove_invalid_user(request)
152 return
153 # If the user is already authenticated and that user is the user we are
154 # getting passed in the headers, then the correct user is already
155 # persisted in the session and we don't need to continue.
156 if request.user.is_authenticated:
157 if request.user.get_username() == self.clean_username(username, request):
158 return
159 else:
160 # An authenticated user is associated with the request, but
161 # it does not match the authorized user in the header.
162 self._remove_invalid_user(request)
163
164 # We are seeing this user for the first time in this session, attempt
165 # to authenticate the user.
166 user = auth.authenticate(request, remote_user=username)
167 if user:
168 # User is valid. Persist the user in the session by logging the
169 # user in.
170 auth.login(request, user)
171
172 async def __acall__(self, request):
173 await self.aprocess_request(request)

Callers 1

__call__Method · 0.95

Calls 6

get_usernameMethod · 0.95
_remove_invalid_userMethod · 0.95
clean_usernameMethod · 0.95
authenticateMethod · 0.45
loginMethod · 0.45

Tested by

no test coverage detected