Skip to content

Instantly share code, notes, and snippets.

@tomchristie
Created January 29, 2013 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomchristie/4664015 to your computer and use it in GitHub Desktop.
Save tomchristie/4664015 to your computer and use it in GitHub Desktop.
class NoCSRFSessionAuthentication(BaseAuthentication):
"""
Use Django's session framework for authentication.
"""
def authenticate(self, request):
"""
Returns a `User` if the request session currently has a logged in user.
Otherwise returns `None`.
"""
# Get the underlying HttpRequest object
http_request = request._request
user = getattr(http_request, 'user', None)
# Unauthenticated, CSRF validation not required
if not user or not user.is_active:
return None
# CSRF passed with authenticated user
return (user, None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment