1 # Copyright (C) 2013 Simo Sorce <simo@redhat.com>
3 # see file 'COPYING' for use and warranty information
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 from ipsilon.util.log import Log
20 from ipsilon.util.user import UserSession
21 from ipsilon.util.trans import Transaction
22 from urllib import unquote
24 from urlparse import urlparse
25 from urlparse import parse_qs
27 # pylint: disable=no-name-in-module, import-error
28 from urllib.parse import urlparse
29 from urllib.parse import parse_qs
32 def admin_protect(fn):
34 def check(*args, **kwargs):
35 if UserSession().get_user().is_admin:
36 return fn(*args, **kwargs)
38 raise cherrypy.HTTPError(403)
44 def __init__(self, site, form=False):
45 if 'template_env' not in site:
46 raise ValueError('Missing template environment')
48 self.basepath = cherrypy.config.get('base.mount', "")
50 self._is_form_page = form
51 self.default_headers = dict()
52 self.auth_protect = False
54 def _check_referer(self, referer, url):
55 r = urlparse(unquote(referer))
56 u = urlparse(unquote(url))
57 if r.scheme != u.scheme:
59 if r.netloc != u.netloc:
61 if r.path.startswith(self.basepath):
65 def __call__(self, *args, **kwargs):
66 # pylint: disable=star-args
67 cherrypy.response.headers.update(self.default_headers)
69 self.user = UserSession().get_user()
71 if self.auth_protect and self.user.is_anonymous:
72 raise cherrypy.HTTPError(401)
75 op = getattr(self, args[0], None)
76 if callable(op) and getattr(op, 'public_function', None):
77 return op(*args[1:], **kwargs)
79 if self._is_form_page:
80 self._debug("method: %s" % cherrypy.request.method)
81 op = getattr(self, cherrypy.request.method, None)
83 # Basic CSRF protection
84 if cherrypy.request.method != 'GET':
85 url = cherrypy.url(relative=False)
86 if 'referer' not in cherrypy.request.headers:
87 self._debug("Missing referer in %s request to %s"
88 % (cherrypy.request.method, url))
89 raise cherrypy.HTTPError(403)
90 referer = cherrypy.request.headers['referer']
91 if not self._check_referer(referer, url):
92 self._debug("Wrong referer %s in request to %s"
94 raise cherrypy.HTTPError(403)
95 return op(*args, **kwargs)
97 op = getattr(self, 'root', None)
99 return op(*args, **kwargs)
101 return self.default(*args, **kwargs)
103 def _template_model(self):
105 model['basepath'] = self.basepath
106 model['title'] = 'IPSILON'
107 model['user'] = self.user
110 def _template(self, *args, **kwargs):
111 # pylint: disable=star-args
112 t = self._site['template_env'].get_template(args[0])
113 m = self._template_model()
117 def default(self, *args, **kwargs):
118 raise cherrypy.NotFound()
120 def add_subtree(self, name, page):
121 self.__dict__[name] = page
123 def del_subtree(self, name):
124 del self.__dict__[name]
126 def get_valid_transaction(self, provider, **kwargs):
128 t = Transaction(provider)
129 # Try with kwargs first
130 tid = t.find_tid(kwargs)
132 # If no TID yet See if we have it in a referer
133 if 'referer' in cherrypy.request.headers:
134 r = urlparse(unquote(cherrypy.request.headers['referer']))
136 tid = t.find_tid(parse_qs(r.query))
141 msg = 'Transaction expired, or cookies not available'
142 raise cherrypy.HTTPError(401, msg)