Edgewall Software

Ticket #540: 540-auth-altlinks-r10316.patch

File 540-auth-altlinks-r10316.patch, 10.4 KB (added by rblank, 18 months ago)

Don't create a new trac_auth cookie for every call to /login.

  • trac/ticket/report.py

    diff --git a/trac/ticket/report.py b/trac/ticket/report.py
    a b from trac.util.text import exception_to_ 
    3838from trac.util.translation import _ 
    3939from trac.web.api import IRequestHandler, RequestDone 
    4040from trac.web.chrome import add_ctxtnav, add_link, add_notice, add_script, \ 
    41                             add_stylesheet, add_warning, \ 
     41                            add_stylesheet, add_warning, auth_link, \ 
    4242                            INavigationContributor, Chrome 
    4343from trac.wiki import IWikiSyntaxProvider, WikiParser 
    4444 
    class ReportModule(Component): 
    266266            return req.href.report(sort=req.args.get('sort'), 
    267267                                   asc=asc and '1' or '0', **kwargs) 
    268268 
    269         add_link(req, 'alternate',  
    270                  report_href(format='rss'), 
     269        add_link(req, 'alternate', 
     270                 auth_link(req, report_href(format='rss')), 
    271271                 _('RSS Feed'), 'application/rss+xml', 'rss') 
    272272        add_link(req, 'alternate', report_href(format='csv'), 
    273273                 _('Comma-delimited Text'), 'text/plain') 
    class ReportModule(Component): 
    548548                           filename=filename) 
    549549        else: 
    550550            p = max is not None and page or None 
    551             add_link(req, 'alternate',  
    552                      report_href(format='rss', page=None), 
     551            add_link(req, 'alternate', 
     552                     auth_link(req, report_href(format='rss', page=None)), 
    553553                     _('RSS Feed'), 'application/rss+xml', 'rss') 
    554554            add_link(req, 'alternate', report_href(format='csv', page=p), 
    555555                     _('Comma-delimited Text'), 'text/plain') 
  • trac/ticket/roadmap.py

    diff --git a/trac/ticket/roadmap.py b/trac/ticket/roadmap.py
    a b from trac.ticket.query import QueryModul 
    4242from trac.timeline.api import ITimelineEventProvider 
    4343from trac.web import IRequestHandler, RequestDone 
    4444from trac.web.chrome import add_link, add_notice, add_script, add_stylesheet, \ 
    45                             add_warning, Chrome, INavigationContributor 
     45                            add_warning, auth_link, Chrome, \ 
     46                            INavigationContributor 
    4647from trac.wiki.api import IWikiSyntaxProvider 
    4748from trac.wiki.formatter import format_to 
    4849 
    class RoadmapModule(Component): 
    368369        if req.authname and req.authname != 'anonymous': 
    369370            username = req.authname 
    370371        icshref = req.href.roadmap(show=show, user=username, format='ics') 
    371         add_link(req, 'alternate', icshref, _('iCalendar'), 'text/calendar', 
    372                  'ics') 
     372        add_link(req, 'alternate', auth_link(req, icshref), _('iCalendar'), 
     373                 'text/calendar', 'ics') 
    373374 
    374375        data = { 
    375376            'milestones': milestones, 
  • trac/ticket/web_ui.py

    diff --git a/trac/ticket/web_ui.py b/trac/ticket/web_ui.py
    a b from trac.util.translation import _, tag 
    4646from trac.versioncontrol.diff import get_diff_options, diff_blocks 
    4747from trac.web import arg_list_to_args, parse_arg_list, IRequestHandler 
    4848from trac.web.chrome import add_link, add_notice, add_script, add_stylesheet, \ 
    49                             add_warning, add_ctxtnav, prevnext_nav, Chrome, \ 
    50                             INavigationContributor, ITemplateProvider 
     49                            add_warning, add_ctxtnav, auth_link, \ 
     50                            prevnext_nav, Chrome, INavigationContributor, \ 
     51                            ITemplateProvider 
    5152from trac.wiki.formatter import format_to, format_to_html, format_to_oneliner 
    5253 
    5354 
    class TicketModule(Component): 
    630631            format = conversion[0] 
    631632            conversion_href = get_resource_url(self.env, ticket.resource, 
    632633                                               req.href, format=format) 
     634            if format == 'rss': 
     635                conversion_href = auth_link(req, conversion_href) 
    633636            add_link(req, 'alternate', conversion_href, conversion[1], 
    634637                     conversion[4], format) 
    635638                      
  • trac/timeline/web_ui.py

    diff --git a/trac/timeline/web_ui.py b/trac/timeline/web_ui.py
    a b from trac.util.datefmt import format_dat 
    3434from trac.util.text import exception_to_unicode, to_unicode 
    3535from trac.util.translation import _, tag_ 
    3636from trac.web import IRequestHandler, IRequestFilter 
    37 from trac.web.chrome import add_link, add_stylesheet, prevnext_nav, Chrome, \ 
    38                             INavigationContributor, ITemplateProvider 
     37from trac.web.chrome import add_link, add_stylesheet, auth_link, \ 
     38                            prevnext_nav, Chrome, INavigationContributor, \ 
     39                            ITemplateProvider 
    3940                             
    4041from trac.wiki.api import IWikiSyntaxProvider 
    4142 
    class TimelineModule(Component): 
    225226        rss_href = req.href.timeline([(f, 'on') for f in filters], 
    226227                                     daysback=90, max=50, authors=authors, 
    227228                                     format='rss') 
    228         add_link(req, 'alternate', rss_href, _('RSS Feed'), 
     229        add_link(req, 'alternate', auth_link(req, rss_href), _('RSS Feed'), 
    229230                 'application/rss+xml', 'rss') 
    230231 
    231232        for filter_ in available_filters: 
  • trac/versioncontrol/web_ui/log.py

    diff --git a/trac/versioncontrol/web_ui/log.py b/trac/versioncontrol/web_ui/log.py
    a b from trac.versioncontrol.web_ui.changese 
    3535from trac.versioncontrol.web_ui.util import * 
    3636from trac.web import IRequestHandler 
    3737from trac.web.chrome import add_ctxtnav, add_link, add_stylesheet, \ 
    38                             INavigationContributor, Chrome 
     38                            auth_link, INavigationContributor, Chrome 
    3939from trac.wiki import IWikiSyntaxProvider, WikiParser  
    4040 
    4141class LogModule(Component): 
    class LogModule(Component): 
    294294 
    295295        rss_href = make_log_href(path, format='rss', revs=revs, 
    296296                                 stop_rev=stop_rev) 
    297         add_link(req, 'alternate', rss_href, _('RSS Feed'), 
     297        add_link(req, 'alternate', auth_link(req, rss_href), _('RSS Feed'), 
    298298                 'application/rss+xml', 'rss') 
    299299        changelog_href = make_log_href(path, format='changelog', revs=revs, 
    300300                                       stop_rev=stop_rev) 
  • trac/web/auth.py

    diff --git a/trac/web/auth.py b/trac/web/auth.py
    a b class LoginModule(Component): 
    8484        if req.remote_user: 
    8585            authname = req.remote_user 
    8686        elif req.incookie.has_key('trac_auth'): 
    87             authname = self._get_name_for_cookie(req, req.incookie['trac_auth']) 
     87            authname = self._get_name_for_cookie(req, 
     88                                                 req.incookie['trac_auth']) 
     89            if authname is None: 
     90                # The cookie is invalid (or has been purged from the database), 
     91                # so tell the user agent to drop it as it is invalid 
     92                self._expire_cookie(req) 
    8893 
    8994        if not authname: 
    9095            return None 
    class LoginModule(Component): 
    153158        assert req.authname in ('anonymous', remote_user), \ 
    154159               _('Already logged in as %(user)s.', user=req.authname) 
    155160 
    156         cookie = hex_entropy() 
    157161        with self.env.db_transaction as db: 
    158             # Delete cookies older than 10 days 
    159             db("DELETE FROM auth_cookie WHERE time < %s", 
    160                (int(time.time()) - 86400 * 10,)) 
    161             db(""" 
    162                 INSERT INTO auth_cookie (cookie, name, ipnr, time) 
    163                      VALUES (%s, %s, %s, %s) 
    164                """, (cookie, remote_user, req.remote_addr, int(time.time()))) 
     162            cookie = None 
     163            trac_auth = req.incookie.get('trac_auth') 
     164            if trac_auth is not None: 
     165                name = self._get_name_for_cookie(req, trac_auth) 
     166                cookie = trac_auth.value if name == remote_user else None 
     167            # Only insert a new cookie if we don't already have one 
     168            if cookie is None: 
     169                # Delete cookies older than 10 days 
     170                db("DELETE FROM auth_cookie WHERE time < %s", 
     171                   (int(time.time()) - 86400 * 10,)) 
     172                cookie = hex_entropy() 
     173                db(""" 
     174                    INSERT INTO auth_cookie (cookie, name, ipnr, time) 
     175                         VALUES (%s, %s, %s, %s) 
     176                   """, (cookie, remote_user, req.remote_addr, 
     177                         int(time.time()))) 
    165178        req.authname = remote_user 
    166179        req.outcookie['trac_auth'] = cookie 
    167180        req.outcookie['trac_auth']['path'] = self.auth_cookie_path \ 
    class LoginModule(Component): 
    213226            args = (cookie.value,) 
    214227        for name, in self.env.db_query(sql, args): 
    215228            return name 
    216         # The cookie is invalid (or has been purged from the database), 
    217         # so tell the user agent to drop it as it is invalid 
    218         self._expire_cookie(req) 
    219229 
    220230    def _redirect_back(self, req): 
    221231        """Redirect the user back to the URL she came from.""" 
    222232        referer = self._referer(req) 
    223         if referer and not (referer == req.base_url or \ 
    224                 referer.startswith(req.base_url.rstrip('/') + '/')): 
     233        if referer and referer.startswith(('http://', 'https://')) \ 
     234                and not (referer == req.base_url or \ 
     235                         referer.startswith(req.base_url.rstrip('/') + '/')): 
    225236            # only redirect to referer if it is from the same site 
    226237            referer = None 
    227238        if referer and referer.rstrip('/') == req.base_url.rstrip('/') \ 
  • trac/web/chrome.py

    diff --git a/trac/web/chrome.py b/trac/web/chrome.py
    a b def prevnext_nav(req, prev_label, next_l 
    189189                              class_=not next_link and 'missing' or None)) 
    190190 
    191191 
     192def auth_link(req, link): 
     193    """Return an "authenticated" link to `link` for authenticated users. 
     194     
     195    If the user is anonymous, returns `link` unchanged. For authenticated 
     196    users, returns a link to `/login` that redirects to `link` after 
     197    authentication. 
     198    """ 
     199    if req.authname != 'anonymous': 
     200        return req.href.login(referer=link) 
     201    return link 
     202 
     203 
    192204def _save_messages(req, url, permanent): 
    193205    """Save warnings and notices in case of redirect, so that they can 
    194206    be displayed after the redirect."""