Skip to content

Better integration with data exchange between Python and HTTPd #945

@michael-o

Description

@michael-o

Our Django application perform authentication by themselves this means that request_rec.user and request_rec.ap_auth_type aren't populated and access log doesn't print them. I have deployed an ugly solution adapted from my approach running Apache Tomcat behind mod_proxy:

  • Create middleware:
class RemoteUserMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        user = getattr(request, "user", None)
        response = self.get_response(request)
        if user and user.is_authenticated:
            response['Remote-User'] = user.get_username()
            response['Auth-Type'] = 'django'

        return response
  • Hook in middleware:
if RUNNING_WSGI:
    MIDDLEWARE.append("kona.middleware.RemoteUserMiddleware")
  • Config for Apache:
Header note remote-user remote-user
Header note auth-type auth-type
Header unset remote-user
Header unset auth-type
LuaHookLog /usr/local/libexec/apache24/register_remote_user.lua register_remote_user
  • Lua script:
require 'apache2'

function register_remote_user(r)
    local remote_user = r.notes["remote-user"]
    local auth_type = r.notes["auth-type"]
    if remote_user ~= nil then
        r.user = remote_user
        r.ap_auth_type = auth_type
    end
    return apache2.OK
end

Ugly, as said. There are two solutions here (don't know which are technically possible):

  • Expose user and ap_auth_type to response object (does it apply to non-django apps?)
  • At least generically expose notes to requests

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions