Edgewall Software

Ticket #540: 540-altlink-rewrite-r10175.patch

File 540-altlink-rewrite-r10175.patch, 3.4 KB (added by rblank, 20 months ago)

Rewrite selected alternative format links to redirect through /login.

  • setup.py

    diff --git a/setup.py b/setup.py
    a b facilities. 
    131131        tracopt.perm.config_perm_provider = tracopt.perm.config_perm_provider 
    132132        tracopt.ticket.commit_updater = tracopt.ticket.commit_updater 
    133133        tracopt.ticket.deleter = tracopt.ticket.deleter 
     134        tracopt.web.altlinks = tracopt.web.altlinks 
    134135    """, 
    135136 
    136137    **extra 
  • trac/web/auth.py

    diff --git a/trac/web/auth.py b/trac/web/auth.py
    a b class LoginModule(Component): 
    225225    def _redirect_back(self, req): 
    226226        """Redirect the user back to the URL she came from.""" 
    227227        referer = self._referer(req) 
    228         if referer and not (referer == req.base_url or \ 
    229                 referer.startswith(req.base_url.rstrip('/')+'/')): 
     228        if referer and referer.startswith(('http://', 'https://')) \ 
     229                and not (referer == req.base_url or \ 
     230                         referer.startswith(req.base_url.rstrip('/') + '/')): 
    230231            # only redirect to referer if it is from the same site 
    231232            referer = None 
    232233        req.redirect(referer or req.abs_href()) 
  • new file tracopt/web/altlinks.py

    diff --git a/tracopt/web/__init__.py b/tracopt/web/__init__.py
    new file mode 100644
    diff --git a/tracopt/web/altlinks.py b/tracopt/web/altlinks.py
    new file mode 100644
    - +  
     1# -*- coding: utf-8 -*- 
     2# 
     3# Copyright (C) 2010 Edgewall Software 
     4# All rights reserved. 
     5# 
     6# This software is licensed as described in the file COPYING, which 
     7# you should have received as part of this distribution. The terms 
     8# are also available at http://trac.edgewall.org/wiki/TracLicense. 
     9# 
     10# This software consists of voluntary contributions made by many 
     11# individuals. For the exact contribution history, see the revision 
     12# history and logs, available at http://trac.edgewall.org/log/. 
     13 
     14import re 
     15 
     16from trac.config import Option 
     17from trac.core import Component, implements 
     18from trac.web.api import IRequestFilter 
     19 
     20 
     21class AltLinkLoginRewriter(Component): 
     22    """Rewrite selected alternative format links to require authentication. 
     23     
     24    This component rewrites alternative format links matching the regexp in 
     25    `[web] altlink_rewrite` for authenticated users to redirect through 
     26    `/login`, therefore requiring authentication. The default regexp matches 
     27    all links containing a `format=rss` or `format=ics` query argument, 
     28    therefore ensuring that e.g. RSS readers log in before requesting a feed. 
     29     
     30    This component doesn't work with form-based login. 
     31    """ 
     32 
     33    implements(IRequestFilter) 
     34 
     35    altlink_login_rewrite = Option('web', 'altlink_login_rewrite', 
     36        r'.*\?(.*&)?format=(rss|ics)(&.*)?$', 
     37        """Regexp pattern defining the alternative format links that should be 
     38        rewritten to require authentication.""") 
     39 
     40    _pattern = None 
     41 
     42    # IRequestFilter methods 
     43 
     44    def pre_process_request(self, req, handler): 
     45        return handler 
     46 
     47    def post_process_request(self, req, template, data, content_type): 
     48        if req.authname and req.authname != 'anonymous': 
     49            if self._pattern is None: 
     50                self._pattern = re.compile(self.altlink_login_rewrite) 
     51            for link in req.chrome.get('links', {}).get('alternate', []): 
     52                href = link.get('href') 
     53                if href is not None and self._pattern.match(href): 
     54                    link['href'] = req.href.login(referer=href) 
     55        return template, data, content_type