osdir.com
mailing list archive F.A.Q. -since 2001!



Subject: req.user always returns None? - msg#00092

List: python.mod_python

Mail Archive Navigation:
by Date: Prev Next Date Index by Thread: Prev Next Thread Index

Now that I'm using req.user instead of req.connection.user, I can get the
Tutorial's example authentication code to run without errors--thanks,
Grisha, for that.

Now, however, the example always returns HTTP_UNAUTHORIZED. I've figured
out that this is because req.user is always None. My browser is sending the
correct base64-encoded string for the Authorization: header. I'll append
additional information.

Thanks for any advice.

Cheers!
--
David Hancock | dhancock-Iw1l/lK2TKoAvxtiuMwx3w@xxxxxxxxxxxxxxxx | 410-266-4384

system
======
Windows 2000
Python 2.2.3
Apache 2.0.47
mod_python 3.0.3

httpd.conf
==========
<Directory "C:\Program Files\Apache Group\Apache2\htdocs\authreq/">
AddHandler python-program .py
PythonHandler mptest
PythonAuthenHandler mptest
AuthType Basic
AuthName "mod_python restricted area"
require valid-user
PythonDebug On
</Directory>

mptest.py
=========
from mod_python import apache

def handler(req):
req.content_type = 'text/plain'
req.send_http_header()
req.write("Hello, world!")
return apache.OK

def authenhandler(req):
user = req.user
pw = req.get_basic_auth_pw()
req.log_error(str(user) + ' ' + str(pw))
if user == "david" and pw == "secret":
return apache.OK
else:
return apache.HTTP_UNAUTHORIZED

error_log (from req.log_error)
==============================
[Sun Oct 19 19:24:38 2003] [error] [client 127.0.0.1] None secret
[Sun Oct 19 19:25:02 2003] [error] [client 127.0.0.1] None secret

HTTP headers to and from (after initial 401)
============================================
GET /authreq/mptest.py HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5b)
Gecko/20030827
Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=
0.8,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Authorization: Basic ZGF2aWQ6c2VjcmV0

checking Authorization header
=============================
C:\Program Files\Apache Group\Apache2\logs>c:\python22\python
Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import base64
>>> print base64.decodestring('ZGF2aWQ6c2VjcmV0')
david:secret
>>>


Thread at a glance:

Previous Message by Date:

RE: req.connection.user generates AttributeError

Grisha: Thanks for getting back to me on this. I found req.connection.user in the PDF manual dated 4/19/2002, Section 3.3, page 12. And now that I look at the title page, it's for 2.7.8, NOT for 3.0.3. I downloaded the manual to match my installation, and it shows req.user in that section. My apologies for not having RTFMed better--I read, just the wrong one. Cheers! -- David Hancock | dhancock-Iw1l/lK2TKoAvxtiuMwx3w@xxxxxxxxxxxxxxxx | 410-266-4384 -----Original Message----- From: Gregory (Grisha) Trubetskoy [mailto:grisha-X6cS8wSRu42Ei8DpZVb4nw@xxxxxxxxxxxxxxxx] Sent: Sunday, October 19, 2003 2:21 PM To: Hancock, David (DHANCOCK) Cc: 'mod_python-X6cS8wSRu42Ei8DpZVb4nw@xxxxxxxxxxxxxxxx' Subject: Re: [mod_python] req.connection.user generates AttributeError it's req.user Just curious - where in the manual do you see this? If you're looking at the online version, can you send me the exact url? Thanks, Grisha On Sun, 19 Oct 2003, Hancock, David (DHANCOCK) wrote: > I'm new to mod_python, and I'm stuck already. I'm working through the > examples in the documentation, and even after careful typing (and cutting > and pasting from the manual), I can't get the authentication example to > work. The line: > > user = req.connection.user > > Gives an attribute error ('user'). As shown in the manual, I'm calling > req.get_basic_auth_pw() first, but still no joy. > > If I try/except to trap the attribute error, I avoid the 500 Server Error > message, but the authentication still doesn't work. > > Any ideas? I'm running Windows 2000, Apache 2.0.47, Python 2.2, and > mod_python 3.0.3. (My other computer is a Linux box, but this is what I've > got right going right now). The mod_python is a precompiled binary. > > I'll be grateful for any assistance I can get. I'm trying to recreate a > mod_perl module (AuthCookie) which implements a ticket-based authentication > mechanism. It works well in Perl, but my group standardized on Python and > we'd like to keep using Python for Apache modules, too. > > Cheers! > -- > David Hancock | dhancock-Iw1l/lK2TKoAvxtiuMwx3w@xxxxxxxxxxxxxxxx | > 410-266-4384 > >

Next Message by Date:

RE: req.user always returns None?

Sorry to reply to my own post, but further reading of the list archives got me the solution. I moved the LoadModule directive for mod_python to be first in the list, and voila! it started working right. There was a thread about this in July of this year. Cheers! -- David Hancock | dhancock-Iw1l/lK2TKoAvxtiuMwx3w@xxxxxxxxxxxxxxxx | 410-266-4384 -----Original Message----- From: Hancock, David (DHANCOCK) Sent: Sunday, October 19, 2003 7:58 PM To: 'mod_python-X6cS8wSRu42Ei8DpZVb4nw@xxxxxxxxxxxxxxxx' Subject: req.user always returns None? Now that I'm using req.user instead of req.connection.user, I can get the Tutorial's example authentication code to run without errors--thanks, Grisha, for that. Now, however, the example always returns HTTP_UNAUTHORIZED. I've figured out that this is because req.user is always None. My browser is sending the correct base64-encoded string for the Authorization: header. I'll append additional information. Thanks for any advice. Cheers! -- David Hancock | dhancock-Iw1l/lK2TKoAvxtiuMwx3w@xxxxxxxxxxxxxxxx | 410-266-4384 system ====== Windows 2000 Python 2.2.3 Apache 2.0.47 mod_python 3.0.3 httpd.conf ========== <Directory "C:\Program Files\Apache Group\Apache2\htdocs\authreq/"> AddHandler python-program .py PythonHandler mptest PythonAuthenHandler mptest AuthType Basic AuthName "mod_python restricted area" require valid-user PythonDebug On </Directory> mptest.py ========= from mod_python import apache def handler(req): req.content_type = 'text/plain' req.send_http_header() req.write("Hello, world!") return apache.OK def authenhandler(req): user = req.user pw = req.get_basic_auth_pw() req.log_error(str(user) + ' ' + str(pw)) if user == "david" and pw == "secret": return apache.OK else: return apache.HTTP_UNAUTHORIZED error_log (from req.log_error) ============================== [Sun Oct 19 19:24:38 2003] [error] [client 127.0.0.1] None secret [Sun Oct 19 19:25:02 2003] [error] [client 127.0.0.1] None secret HTTP headers to and from (after initial 401) ============================================ GET /authreq/mptest.py HTTP/1.1 Host: localhost User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.5b) Gecko/20030827 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q= 0.8,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Authorization: Basic ZGF2aWQ6c2VjcmV0 checking Authorization header ============================= C:\Program Files\Apache Group\Apache2\logs>c:\python22\python Python 2.2.3 (#42, May 30 2003, 18:12:08) [MSC 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import base64 >>> print base64.decodestring('ZGF2aWQ6c2VjcmV0') david:secret >>>

Previous Message by Thread:

req.connection.user generates AttributeError

I'm new to mod_python, and I'm stuck already.  I'm working through the examples in the documentation, and even after careful typing (and cutting and pasting from the manual), I can't get the authentication example to work.  The line:         user = req.connection.user Gives an attribute error ('user').  As shown in the manual, I'm calling req.get_basic_auth_pw() first, but still no joy. If I try/except to trap the attribute error, I avoid the 500 Server Error message, but the authentication still doesn't work. Any ideas?  I'm running Windows 2000, Apache 2.0.47, Python 2.2, and mod_python 3.0.3.  (My other computer is a Linux box, but this is what I've got right going right now).  The mod_python is a precompiled binary. I'll be grateful for any assistance I can get.  I'm trying to recreate a mod_perl module (AuthCookie) which implements a ticket-based authentication mechanism.  It works well in Perl, but my group standardized on Python and we'd like to keep using Python for Apache modules, too. Cheers! -- David Hancock | dhancock-Iw1l/lK2TKoAvxtiuMwx3w@xxxxxxxxxxxxxxxx | 410-266-4384 _______________________________________________ Mod_python mailing list Mod_python-X6cS8wSRu42Ei8DpZVb4nw@xxxxxxxxxxxxxxxx http://mailman.modpython.org/mailman/listinfo/mod_python

Next Message by Thread:

Re: req.user always returns None?

On Sun, 19 Oct 2003, Hancock, David (DHANCOCK) wrote: > def authenhandler(req): > user = req.user > pw = req.get_basic_auth_pw() > req.log_error(str(user) + ' ' + str(pw)) > if user == "david" and pw == "secret": > return apache.OK > else: > return apache.HTTP_UNAUTHORIZED The current docs neglect to mention this because at the time it seemed like it was no longer necessary, but the call to get_basic_auth_pw() should always _preceed_ the use of req.user. If the swap the first two lines, it will work (regardless of position of LoadModule within the config file). Grisha
blog comments powered by Disqus

Home | News | Sitemap | FAQ | advertise | OSDir is an Inevitable website. GBiz is too!