X-Git-Url: http://git.cascardo.info/?p=cascardo%2Fipsilon.git;a=blobdiff_plain;f=ipsilon%2Futil%2Fpage.py;h=50585ca8e242deb2abe0c3b1a77266683fa2aed9;hp=eeb9ca343111b515b5bc300c122f77219108a4ed;hb=e0aa4f23846fa9f6bb0fb9eb021e930b035100eb;hpb=5ebec54b30ae7cfeef98761894732f52b30d2441 diff --git a/ipsilon/util/page.py b/ipsilon/util/page.py old mode 100755 new mode 100644 index eeb9ca3..50585ca --- a/ipsilon/util/page.py +++ b/ipsilon/util/page.py @@ -1,5 +1,3 @@ -#!/usr/bin/python -# # Copyright (C) 2013 Simo Sorce # # see file 'COPYING' for use and warranty information @@ -18,15 +16,17 @@ # along with this program. If not, see . import cherrypy -from ipsilon.util.log import Log +from ipsilon.util.endpoint import Endpoint from ipsilon.util.user import UserSession from ipsilon.util.trans import Transaction from urllib import unquote try: from urlparse import urlparse + from urlparse import parse_qs except ImportError: # pylint: disable=no-name-in-module, import-error from urllib.parse import urlparse + from urllib.parse import parse_qs def admin_protect(fn): @@ -40,8 +40,9 @@ def admin_protect(fn): return check -class Page(Log): +class Page(Endpoint): def __init__(self, site, form=False): + super(Page, self).__init__(site) if 'template_env' not in site: raise ValueError('Missing template environment') self._site = site @@ -51,6 +52,14 @@ class Page(Log): self.default_headers = dict() self.auth_protect = False + def get_url(self): + return cherrypy.url(relative=False) + + def instance_base_url(self): + url = self.get_url() + s = urlparse(unquote(url)) + return '%s://%s%s' % (s.scheme, s.netloc, self.basepath) + def _check_referer(self, referer, url): r = urlparse(unquote(referer)) u = urlparse(unquote(url)) @@ -82,7 +91,7 @@ class Page(Log): if callable(op): # Basic CSRF protection if cherrypy.request.method != 'GET': - url = cherrypy.url(relative=False) + url = self.get_url() if 'referer' not in cherrypy.request.headers: self._debug("Missing referer in %s request to %s" % (cherrypy.request.method, url)) @@ -125,7 +134,24 @@ class Page(Log): def get_valid_transaction(self, provider, **kwargs): try: - return Transaction(provider, **kwargs) + t = Transaction(provider) + # Try with kwargs first + tid = t.find_tid(kwargs) + if not tid: + # If no TID yet See if we have it in a referer or in the + # environment in the REDIRECT_URL + url = None + if 'referer' in cherrypy.request.headers: + url = cherrypy.request.headers['referer'] + elif 'REQUEST_URI' in cherrypy.request.wsgi_environ: + url = cherrypy.request.wsgi_environ['REQUEST_URI'] + if url: + r = urlparse(unquote(url)) + if r.query: + tid = t.find_tid(parse_qs(r.query)) + if not tid: + t.create_tid() + return t except ValueError: msg = 'Transaction expired, or cookies not available' raise cherrypy.HTTPError(401, msg)