HttpLoginViaRedirectPatch for Trac

Description:

This patch allows you to make Trac ask for an http authentication on any Trac page, simply by adding '/login' before the path of the page that you would like to request...

Example:

If you are asking for the RSS feed of a non-public project, you can call for "http://trac.something.com/aproject/login/timeline?...&format;=rss" and you'll be asked to type in your http authentication data (unless you are already logged on) before you'll be (re)directed to the RSS feed provided by "http://trac.something.com/aproject/timeline?...&format;=rss"...

The following changes have been made to Trac version 0.9.6. (PS: Be careful about the indentation level of each statement because Python's blocking scheme is based on indentation!!)

How to add http login functionality to every Trac page

Add to .../python/site-packages/trac/web/auth.py the following method:

    def _redirect(self, req):
        """Redirects the user to the path mentioned after '/login'.
        Example: "/login/timeline" redirects to "/timeline". """

        # (re)build query_string from req.args to get an urlencodable dictionary 
        query_string = {}
        for key in req.args.keys():
            query_string[key] = req.args.get(key)
	
        # (re)build target path (with query_string, if applicable)
        from urllib import urlencode	
        if len(query_string)==0:
            req.redirect(self.env.abs_href() + req.path_info.replace('/login',''))
        else:
            req.redirect(self.env.abs_href() + req.path_info.replace('/login','') + '?' + urlencode(query_string))

and replace the following line (in method def process_request(self, req):)

        self._redirect_back(req)

with

        if req.path_info.startswith('/login/'):
            self._redirect(req)
        else:
            self._redirect_back(req)

Or just patch auth.diff to your Trac installation (applicable to Trac version 0.9.6):

  1. Copy the auth.diff file into the .../python/site-packages/trac/web/ directory of your Trac installation (version 0.9.6!)
  2. At your shell prompt, type in "patch .../python/site-packages/trac/web/auth.py .../python/site-packages/trac/web/auth.diff"
  3. Done!

DON'T FORGET: You need to tell your http server to ask for an http authentication for every location starting with '/login'! Read next, how to do that...


How to tell your http server to ask for an http authentication for every location starting with '/login'

When using CGI, FastCGI, or mod_python…

...change the Trac login location of your http server (probably specified in .../etc/trac.conf) from

<Location ".../login">

to

<LocationMatch ".../login.*">

When using tracd…

...you need to modify .../python/site-packages/trac/web/standalone.py as follows:

Replace line

        if path_info == '/login':

with

        if path_info.startswith('/login'):

(somewhere in the middle of method def _do_trac_req(self):).

Or just patch standalone.diff to your Trac installation (applicable to Trac version 0.9.6):

  1. Copy the standalone.diff file into the .../python/site-packages/trac/web/ directory of your Trac installation (version 0.9.6!)
  2. At your shell prompt, type in "patch .../python/site-packages/trac/web/standalone.py .../python/site-packages/trac/web/standalone.diff"
  3. Done!