##// END OF EJS Templates
enh: added authentication ability for webapp
Satrajit Ghosh -
Show More
@@ -35,13 +35,36 b' except ImportError:'
35 # Top-level handlers
35 # Top-level handlers
36 #-----------------------------------------------------------------------------
36 #-----------------------------------------------------------------------------
37
37
38
38 class BaseHandler(web.RequestHandler):
39 class NBBrowserHandler(web.RequestHandler):
39 def get_current_user(self):
40 user_id = self.get_secure_cookie("user")
41 keyword = self.get_secure_cookie("keyword")
42 if self.application.keyword and self.application.keyword != keyword:
43 return None
44 if not user_id:
45 user_id = 'anonymous'
46 return user_id
47
48 class NBBrowserHandler(BaseHandler):
49 @web.authenticated
40 def get(self):
50 def get(self):
41 nbm = self.application.notebook_manager
51 nbm = self.application.notebook_manager
42 project = nbm.notebook_dir
52 project = nbm.notebook_dir
43 self.render('nbbrowser.html', project=project)
53 self.render('nbbrowser.html', project=project)
44
54
55 class LoginHandler(BaseHandler):
56 def get(self):
57 user_id = self.get_secure_cookie("user")
58 self.write('<html><body><form action="/login" method="post">'
59 'Name: <input type="text" name="name" value=%s>'
60 'Keyword: <input type="text" name="keyword">'
61 '<input type="submit" value="Sign in">'
62 '</form></body></html>'%user_id)
63
64 def post(self):
65 self.set_secure_cookie("user", self.get_argument("name", default=u''))
66 self.set_secure_cookie("keyword", self.get_argument("keyword", default=u''))
67 self.redirect("/")
45
68
46 class NewHandler(web.RequestHandler):
69 class NewHandler(web.RequestHandler):
47 def get(self):
70 def get(self):
@@ -35,7 +35,7 b' from tornado import httpserver'
35 from tornado import web
35 from tornado import web
36
36
37 from .kernelmanager import MappingKernelManager
37 from .kernelmanager import MappingKernelManager
38 from .handlers import (
38 from .handlers import (LoginHandler,
39 NBBrowserHandler, NewHandler, NamedNotebookHandler,
39 NBBrowserHandler, NewHandler, NamedNotebookHandler,
40 MainKernelHandler, KernelHandler, KernelActionHandler, IOPubHandler,
40 MainKernelHandler, KernelHandler, KernelActionHandler, IOPubHandler,
41 ShellHandler, NotebookRootHandler, NotebookHandler, RSTHandler
41 ShellHandler, NotebookRootHandler, NotebookHandler, RSTHandler
@@ -80,6 +80,7 b' class NotebookWebApplication(web.Application):'
80 def __init__(self, ipython_app, kernel_manager, notebook_manager, log):
80 def __init__(self, ipython_app, kernel_manager, notebook_manager, log):
81 handlers = [
81 handlers = [
82 (r"/", NBBrowserHandler),
82 (r"/", NBBrowserHandler),
83 (r"/login", LoginHandler),
83 (r"/new", NewHandler),
84 (r"/new", NewHandler),
84 (r"/%s" % _notebook_id_regex, NamedNotebookHandler),
85 (r"/%s" % _notebook_id_regex, NamedNotebookHandler),
85 (r"/kernels", MainKernelHandler),
86 (r"/kernels", MainKernelHandler),
@@ -94,6 +95,8 b' class NotebookWebApplication(web.Application):'
94 settings = dict(
95 settings = dict(
95 template_path=os.path.join(os.path.dirname(__file__), "templates"),
96 template_path=os.path.join(os.path.dirname(__file__), "templates"),
96 static_path=os.path.join(os.path.dirname(__file__), "static"),
97 static_path=os.path.join(os.path.dirname(__file__), "static"),
98 cookie_secret="61oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=",
99 login_url="/login",
97 )
100 )
98 web.Application.__init__(self, handlers, **settings)
101 web.Application.__init__(self, handlers, **settings)
99
102
@@ -122,7 +125,8 b' aliases.update({'
122 'keyfile': 'IPythonNotebookApp.keyfile',
125 'keyfile': 'IPythonNotebookApp.keyfile',
123 'certfile': 'IPythonNotebookApp.certfile',
126 'certfile': 'IPythonNotebookApp.certfile',
124 'ws-hostname': 'IPythonNotebookApp.ws_hostname',
127 'ws-hostname': 'IPythonNotebookApp.ws_hostname',
125 'notebook-dir': 'NotebookManager.notebook_dir'
128 'notebook-dir': 'NotebookManager.notebook_dir',
129 'keyword' : 'IPythonNotebookApp.keyword'
126 })
130 })
127
131
128 notebook_aliases = [u'port', u'ip', u'keyfile', u'certfile', u'ws-hostname',
132 notebook_aliases = [u'port', u'ip', u'keyfile', u'certfile', u'ws-hostname',
@@ -185,6 +189,10 b' class IPythonNotebookApp(BaseIPythonApplication):'
185 help="""The full path to a private key file for usage with SSL/TLS."""
189 help="""The full path to a private key file for usage with SSL/TLS."""
186 )
190 )
187
191
192 keyword = Unicode(u'', config=True,
193 help="""Keyword to use for web authentication"""
194 )
195
188 def get_ws_url(self):
196 def get_ws_url(self):
189 """Return the WebSocket URL for this server."""
197 """Return the WebSocket URL for this server."""
190 if self.certfile:
198 if self.certfile:
@@ -241,6 +249,7 b' class IPythonNotebookApp(BaseIPythonApplication):'
241 ssl_options['keyfile'] = self.keyfile
249 ssl_options['keyfile'] = self.keyfile
242 else:
250 else:
243 ssl_options = None
251 ssl_options = None
252 self.web_app.keyword = self.keyword
244 self.http_server = httpserver.HTTPServer(self.web_app, ssl_options=ssl_options)
253 self.http_server = httpserver.HTTPServer(self.web_app, ssl_options=ssl_options)
245 if ssl_options is None and not self.ip:
254 if ssl_options is None and not self.ip:
246 self.log.critical('WARNING: the notebook server is listening on all IP addresses '
255 self.log.critical('WARNING: the notebook server is listening on all IP addresses '
General Comments 0
You need to be logged in to leave comments. Login now