Challenge - 5 Problems
Authentication Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this Django authentication middleware do?
Consider this Django middleware snippet that checks user authentication status. What will be the behavior when an unauthenticated user accesses a protected view?
Django
class SimpleAuthMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): if not request.user.is_authenticated: from django.http import HttpResponseForbidden return HttpResponseForbidden('Access denied') response = self.get_response(request) return response
Attempts:
2 left
💡 Hint
Look at the response returned when the user is not authenticated.
✗ Incorrect
The middleware checks if the user is authenticated. If not, it returns an HttpResponseForbidden, which sends a 403 status code blocking access.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this Django middleware snippet
Which option correctly fixes the syntax error in this middleware code?
Django
class AuthCheckMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): if not request.user.is_authenticated: from django.http import HttpResponseRedirect return HttpResponseRedirect('/login/') return self.get_response(request)
Attempts:
2 left
💡 Hint
Check the if statement syntax carefully.
✗ Incorrect
The if statement is missing a colon at the end, which is required in Python syntax.
❓ state_output
advanced2:00remaining
What is the value of request.user after this middleware runs?
Given this middleware that modifies the request user attribute, what will be the value of request.user in the view after middleware processing?
Django
class FakeUserMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): class FakeUser: is_authenticated = True username = 'guest' request.user = FakeUser() response = self.get_response(request) return response
Attempts:
2 left
💡 Hint
Look at how request.user is assigned inside the middleware.
✗ Incorrect
The middleware replaces request.user with a new FakeUser object having is_authenticated True and username 'guest'.
🔧 Debug
advanced2:00remaining
Why does this authentication middleware cause a server error?
This middleware is intended to block unauthenticated users but causes a server error. What is the cause?
Django
class AuthMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): if not request.user.is_authenticated: return redirect('/login/') return self.get_response(request)
Attempts:
2 left
💡 Hint
Check if all functions used are properly imported.
✗ Incorrect
The redirect function is used but not imported from django.shortcuts, causing a NameError at runtime.
🧠 Conceptual
expert2:00remaining
Which statement best describes Django authentication middleware behavior?
Select the most accurate description of how Django's built-in AuthenticationMiddleware works.
Attempts:
2 left
💡 Hint
Think about what request.user contains after AuthenticationMiddleware runs.
✗ Incorrect
AuthenticationMiddleware adds a user attribute to the request, which is either the logged-in user or an anonymous user object. It does not redirect or encrypt credentials.