]> git.ipfire.org Git - ipfire.org.git/blame - src/web/handlers_base.py
Remove overloading of static_url
[ipfire.org.git] / src / web / handlers_base.py
CommitLineData
940227cb
MT
1#!/usr/bin/python
2
60024cc8
MT
3from __future__ import division
4
cc3b928d 5import datetime
66862195
MT
6import dateutil.parser
7import httplib
8import ipaddr
47ed77ed 9import logging
940227cb
MT
10import time
11import tornado.locale
12import tornado.web
13
a95c2f97 14from .. import util
60024cc8 15
940227cb 16class BaseHandler(tornado.web.RequestHandler):
de683d7c
MT
17 rss_url = None
18
940227cb
MT
19 def get_account(self, uid):
20 # Find the name of the author
21 return self.accounts.find(uid)
22
4ca1a601
MT
23 @property
24 def hostname(self):
25 # Remove the development prefix
26 return self.request.host.replace(".dev.", ".")
27
47d47c2e
MT
28 @property
29 def ssl_params(self):
30 return {
31 "ssl_cipher" : self.request.headers.get("X-Https-Cipher", None),
32 "ssl_protocol" : self.request.headers.get("X-Https-Protocol", None),
33 }
34
940227cb
MT
35 @property
36 def render_args(self):
cc3b928d
MT
37 today = datetime.date.today()
38
47d47c2e 39 ret = {
a95c2f97
MT
40 "format_size" : util.format_size,
41 "format_time" : util.format_time,
4ca1a601 42 "hostname" : self.hostname,
de683d7c 43 "rss_url" : self.rss_url,
cc3b928d 44 "year" : today.year,
940227cb 45 }
47d47c2e
MT
46 ret.update(self.ssl_params)
47
48 return ret
940227cb 49
bcc3ed4d
MT
50 def render(self, *args, **_kwargs):
51 kwargs = self.render_args
52 kwargs.update(_kwargs)
940227cb
MT
53 tornado.web.RequestHandler.render(self, *args, **kwargs)
54
bcc3ed4d
MT
55 def render_string(self, *args, **_kwargs):
56 kwargs = self.render_args
57 kwargs.update(_kwargs)
940227cb
MT
58 return tornado.web.RequestHandler.render_string(self, *args, **kwargs)
59
02f2d7fe 60 def write_error(self, status_code, **kwargs):
940227cb
MT
61 if status_code in (404, 500):
62 render_args = ({
63 "code" : status_code,
64 "exception" : kwargs.get("exception", None),
65 "message" : httplib.responses[status_code],
66 })
02f2d7fe 67 self.render("error-%s.html" % status_code, **render_args)
940227cb 68 else:
02f2d7fe 69 return tornado.web.RequestHandler.write_error(self, status_code, **kwargs)
940227cb 70
9068dba1
MT
71 def get_remote_ip(self):
72 # Fix for clients behind a proxy that sends "X-Forwarded-For".
66862195 73 remote_ips = self.request.remote_ip.split(", ")
494d80e6 74
66862195
MT
75 for remote_ip in remote_ips:
76 try:
77 addr = ipaddr.IPAddress(remote_ip)
78 except ValueError:
79 # Skip invalid IP addresses.
80 continue
9068dba1 81
66862195
MT
82 # Check if the given IP address is from a
83 # private network.
84 if addr.is_private:
85 continue
9068dba1 86
66862195 87 return remote_ip
9068dba1 88
494d80e6
MT
89 # Return the last IP if nothing else worked
90 return remote_ips.pop()
91
9068dba1
MT
92 def get_remote_location(self):
93 if not hasattr(self, "__remote_location"):
94 remote_ip = self.get_remote_ip()
95
96 self.__remote_location = self.geoip.get_location(remote_ip)
97
98 return self.__remote_location
99
66862195
MT
100 def get_argument_date(self, arg, *args, **kwargs):
101 value = self.get_argument(arg, *args, **kwargs)
102 if value is None:
103 return
104
105 try:
106 return dateutil.parser.parse(value)
107 except ValueError:
108 raise tornado.web.HTTPError(400)
109
110 # Login stuff
111
112 def get_current_user(self):
113 session_id = self.get_cookie("session_id")
114 if not session_id:
115 return
116
117 # Get account from the session object
118 account = self.backend.accounts.get_by_session(session_id, self.request.host)
119
120 # If the account was not found or the session was not valid
121 # any more, we will remove the cookie.
122 if not account:
123 self.clear_cookie("session_id")
124
125 return account
126
127 def login(self, username, password):
128 # Find account
129 account = self.backend.accounts.find_account(username)
130 if not account:
131 logging.warning(401, "unknown account: %s" % username)
132 return False
133
134 # Check credentials
135 if not account.check_password(password):
136 logging.warning("invalid password for %s" % account)
137 return False
138
139 # User has logged in, create a session
140 session_id, session_expires = self.backend.accounts.create_session(account,
141 self.request.host)
142
143 # Check if a new session was created
144 if not session_id:
145 logging.warning("Could not create session")
146 return False
147
148 # Send session cookie to the client
149 self.set_cookie("session_id", session_id,
150 domain=self.request.host, expires=session_expires)
151
152 return True
153
154 def logout(self):
155 session_id = self.get_cookie("session_id")
156
157 if not session_id:
158 return
159
160 success = self.backend.accounts.destroy_session(session_id, self.request.host)
161 if success:
162 self.clear_cookie("session_id")
163
a6dc0bad
MT
164 @property
165 def backend(self):
166 return self.application.backend
167
9068dba1
MT
168 @property
169 def db(self):
170 return self.backend.db
171
a0048e66
MT
172 @property
173 def advertisements(self):
9068dba1 174 return self.backend.advertisements
a0048e66 175
940227cb
MT
176 @property
177 def accounts(self):
a6dc0bad 178 return self.backend.accounts
940227cb
MT
179
180 @property
9068dba1
MT
181 def downloads(self):
182 return self.backend.downloads
183
66862195
MT
184 @property
185 def fireinfo(self):
186 return self.backend.fireinfo
187
9068dba1
MT
188 @property
189 def iuse(self):
190 return self.backend.iuse
940227cb 191
b3250465
MT
192 @property
193 def memcached(self):
9068dba1 194 return self.backend.memcache
b3250465 195
940227cb
MT
196 @property
197 def mirrors(self):
9068dba1
MT
198 return self.backend.mirrors
199
200 @property
201 def netboot(self):
202 return self.backend.netboot
940227cb
MT
203
204 @property
205 def news(self):
9068dba1 206 return self.backend.news
940227cb
MT
207
208 @property
209 def config(self):
9068dba1 210 return self.backend.settings
940227cb
MT
211
212 @property
213 def releases(self):
9068dba1 214 return self.backend.releases
940227cb
MT
215
216 @property
9068dba1
MT
217 def geoip(self):
218 return self.backend.geoip
940227cb
MT
219
220 @property
66862195
MT
221 def talk(self):
222 return self.backend.talk
940227cb
MT
223
224 @property
225 def tracker(self):
9068dba1 226 return self.backend.tracker
bcc3ed4d
MT
227
228 @property
229 def planet(self):
a6dc0bad 230 return self.backend.planet
7771acea 231
66862195
MT
232
233class LoginHandler(BaseHandler):
234 def get(self):
235 self.render("auth/login.html")
236
237 def post(self):
238 username = self.get_argument("username")
239 password = self.get_argument("password")
240
241 if not self.login(username, password):
242 raise tornado.web.HTTPError(401)
243
244 next = self.get_argument("next", "/")
245 return self.redirect(next)
246
247
248class LogoutHandler(BaseHandler):
249 def get(self):
250 self.logout()
251
252 # Get back to the start page
253 self.redirect("/")