Sunday, September 26, 2010

Need access to http basic authentication from request object inside view

Dear reader

I have been fighting this really simple problem for 12 straight hours
now.
I just really can't seem to gain access to the simple http
authorization string from my view-functions.

e.g: "Basic bWF4bTpwYXNzd29yZA=="

Here is my simple test function inside my views.py:

def authenticate(request):
if 'HTTP_AUTHORIZATION' in request.META:
return HttpResponse(request.META['HTTP_AUTHORIZATION'])
elif 'AUTHORIZATION' in request.META:
return HttpResponse(request.META['AUTHORIZATION'])
elif 'REMOTE_USER' in request.META:
return HttpResponse(request.META['REMOTE_USER'])
else:
return HttpResponse(request) #Output is appended below

All of these keys do not exist... and the complete request object does
not contain the string either.

Many experience this problem using apache+mod_wsgi because
"WSGIPassAuthorization on" has to be set.
However i am experiencing the problem both with the built-in-django-
deleopment-server and apache+mod_wsgi+"WSGIPassAuthorization on".

Client: Safari and CocoaRestClient - so i know the authentication get
sent

I would greatly appreciate any help in making an simple django example
app, that prints out the basic_http_auth_string.

Best wished
Morten Pedersen


The full request object print out like so:
<WSGIRequest
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
COOKIES:{'sessionid': '26efb99003df54f49b468faac87cf523'},
META:{'CONTENT_TYPE': 'text/plain',
'DOCUMENT_ROOT': '/var/www',
'GATEWAY_INTERFACE': 'CGI/1.1',
'HTTP_ACCEPT': '*/*',
'HTTP_ACCEPT_ENCODING': 'gzip, deflate',
'HTTP_ACCEPT_LANGUAGE': 'en-us',
'HTTP_CONNECTION': 'keep-alive',
'HTTP_COOKIE': 'sessionid=26efb99003df54f49b468faac87cf523',
'HTTP_HOST': '172.16.116.130',
'HTTP_USER_AGENT': 'CocoaRestClient/1 CFNetwork/454.9.8 Darwin/10.4.0
(i386) (MacBookPro5%2C5)',
'PATH_INFO': u'/auth/authenticate/',
'PATH_TRANSLATED': '/usr/local/rhk/var/rhkest/apache/django.wsgi/auth/
authenticate/',
'QUERY_STRING': '',
'REMOTE_ADDR': '172.16.116.1',
'REMOTE_PORT': '55399',
'REQUEST_METHOD': 'GET',
'REQUEST_URI': '/auth/authenticate/',
'SCRIPT_FILENAME': '/usr/local/rhk/var/rhkest/apache/django.wsgi',
'SCRIPT_NAME': u'',
'SERVER_ADDR': '172.16.116.130',
'SERVER_ADMIN': '(REMOVED)',
'SERVER_NAME': '172.16.116.130',
'SERVER_PORT': '80',
'SERVER_PROTOCOL': 'HTTP/1.1',
'SERVER_SIGNATURE': '<address>Apache/2.2.16 (Debian) Server at
172.16.116.130 Port 80</address>\n',
'SERVER_SOFTWARE': 'Apache/2.2.16 (Debian)',
'mod_wsgi.application_group': '(REMOVED)|',
'mod_wsgi.callable_object': 'application',
'mod_wsgi.handler_script': '',
'mod_wsgi.input_chunked': '0',
'mod_wsgi.listener_host': '',
'mod_wsgi.listener_port': '80',
'mod_wsgi.process_group': '(REMOVED)',
'mod_wsgi.request_handler': 'wsgi-script',
'mod_wsgi.script_reloading': '1',
'mod_wsgi.version': (3, 3),
'wsgi.errors': <mod_wsgi.Log object at 0xb8e7fb60>,
'wsgi.file_wrapper': <built-in method file_wrapper of
mod_wsgi.Adapter object at 0xb6f0a770>,
'wsgi.input': <mod_wsgi.Input object at 0xb8e7fb38>,
'wsgi.multiprocess': True,
'wsgi.multithread': False,
'wsgi.run_once': False,
'wsgi.url_scheme': 'http',
'wsgi.version': (1, 1)}>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment