Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WSGI/Django: pass username back to Apache for access log

My Django app, deployed in mod_wsgi under Apache using Django's standard WSGIHandler, authenticates users via form login on the Django side. So to Apache, the user is anonymous. This makes the Apache access log less useful.

Is there a way to pass the username back through the WSGI wrapper to Apache after handling the request, so that it appears in the Apache access log?

(Versions: Django 1.1.1, mod_wsgi 2.5, Apache 2.2.9)

like image 888
Gunnlaugur Briem Avatar asked Feb 11 '10 12:02

Gunnlaugur Briem


2 Answers

You can only do it if using embedded mode and only if you use a separate package called apswigpy, which provides a Python binding for original Apache request object. The mod_wsgi package provides an optional mechanism for allowing original Apache request object to be passed as Python CObject reference in WSGI environment. You use that in conjunction with apswigpy something like:

from apache.httpd import request_rec
r = request_rec(environ['apache.request_rec'])
r.user = user

At least I think that will setup the appropriate information which access logging can then use.

You should really take this discussion over to the mod_wsgi mailing list.

like image 197
Graham Dumpleton Avatar answered Nov 15 '22 17:11

Graham Dumpleton


You could use mod_auth_tkt. An auth_tkt is a signed cookie with the user id that Apache can understand. Your web application would have to set the cookie when the user logs in and out. Apache can derive a REMOTE_USER from the cookie, pass it to your web app or a non-Django web application running on the same server, include it in logs, whatever.

like image 33
joeforker Avatar answered Nov 15 '22 15:11

joeforker