Created
November 26, 2011 02:36
-
-
Save stuntgoat/1394856 to your computer and use it in GitHub Desktop.
Login view that sends request for credentials to the client via WWW-Authenticate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def auth_landing_page(request): | |
try: | |
if request.META['HTTP_AUTHORIZATION']: | |
# in httpd.conf set 'WSGIPassAuthorization On' | |
# WSGIPassAuthorization will pass the login information sent by the client to the Django view, | |
# as shown below. | |
http_authorization = request.META['HTTP_AUTHORIZATION'] | |
import base64 | |
_user, _password = base64.b64decode(http_authorization[6:]).split(':') | |
user = authenticate(username=_user, password=_password) | |
if user != None: | |
login(request, user) | |
if user.is_authenticated(): | |
# return request.GET['next'] contains the initially requested URL | |
# a typical redirected URL will look like this: /login/?next=/user/home/ | |
return HttpResponseRedirect(request.GET['next']) | |
else: | |
pass | |
else: | |
return HttpResponse("Username or password was incorrect") | |
except KeyError: | |
# if we did not acquire user and password from the client, | |
# present the client with a WWW-Authenticate challenge. | |
client_auth_challenge = HttpResponse("") | |
client_auth_challenge.status_code = 401 | |
client_auth_challenge['WWW-Authenticate'] = r'Basic realm="Secure Area"' | |
return client_auth_challenge | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment