aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron LI <aaronly.me@outlook.com>2016-11-15 16:27:26 +0800
committerAaron LI <aaronly.me@outlook.com>2016-11-15 16:27:26 +0800
commit94ce3f24262e948e0b3275552c9ed4550bf41248 (patch)
treed858ac41694003e0f34e1fcf5e2120bdf0b63ea1
parentf8d8335199aa86a8fe19da6e20829a1b9ded3cf9 (diff)
downloadfg21sim-94ce3f24262e948e0b3275552c9ed4550bf41248.tar.bz2
webui: To support password authentication
Move the "IndexHandler" to separate module located at the "handlers/" directory.
-rwxr-xr-xbin/fg21sim-webui3
-rw-r--r--fg21sim/webui/app.py20
-rw-r--r--fg21sim/webui/handlers/index.py19
3 files changed, 34 insertions, 8 deletions
diff --git a/bin/fg21sim-webui b/bin/fg21sim-webui
index a0c0668..e2ecff5 100755
--- a/bin/fg21sim-webui
+++ b/bin/fg21sim-webui
@@ -45,6 +45,9 @@ define("hosts_allowed", default="any", type=str,
"'192.168.0.0/24'. "
"Specify 'any' to allow any hosts. "
"Note that the localhost/127.0.0.1 is always allowed."))
+define("password", type=str,
+ help=("Password authentication to access the Web UI. "
+ "If not specified, then all accesses are allowed."))
def main():
diff --git a/fg21sim/webui/app.py b/fg21sim/webui/app.py
index 004be8d..3793fda 100644
--- a/fg21sim/webui/app.py
+++ b/fg21sim/webui/app.py
@@ -14,23 +14,21 @@ using the WebSocket_ protocol.
import os
import tornado.web
+from tornado.web import url
-from .websocket import FG21simWSHandler
+from .handlers import IndexHandler, LoginHandler, FG21simWSHandler
+from .utils import gen_cookie_secret
from ..configs import ConfigManager
-class IndexHandler(tornado.web.RequestHandler):
- def get(self):
- self.render("index.html")
-
-
class Application(tornado.web.Application):
configmanager = ConfigManager()
def __init__(self, **kwargs):
handlers = [
- (r"/", IndexHandler),
- (r"/ws", FG21simWSHandler),
+ url(r"/", IndexHandler, name="index"),
+ url(r"/login", LoginHandler, name="login"),
+ url(r"/ws", FG21simWSHandler),
]
settings = {
# The static files will be served from the default "/static/" URI.
@@ -39,6 +37,12 @@ class Application(tornado.web.Application):
"static"),
"template_path": os.path.join(os.path.dirname(__file__),
"templates"),
+ # URL to be redirected to if the user is not logged in
+ "login_url": r"/login",
+ # Secret key used to sign the cookies
+ "cookie_secret": gen_cookie_secret(),
+ # Enable "cross-site request forgery" (XSRF)
+ "xsrf_cookies": True,
}
settings.update(kwargs)
super().__init__(handlers, **settings)
diff --git a/fg21sim/webui/handlers/index.py b/fg21sim/webui/handlers/index.py
new file mode 100644
index 0000000..e95c310
--- /dev/null
+++ b/fg21sim/webui/handlers/index.py
@@ -0,0 +1,19 @@
+# Copyright (c) 2016 Weitian LI <liweitianux@live.com>
+# MIT license
+
+"""
+Login handler
+"""
+
+import tornado.web
+
+from .base import BaseRequestHandler
+
+
+class IndexHandler(BaseRequestHandler):
+ """
+ Index page handler of the Web UI.
+ """
+ @tornado.web.authenticated
+ def get(self):
+ self.render("index.html")