##// END OF EJS Templates
update custom auth per review...
Min RK -
Show More
@@ -1,40 +1,21 b''
1 """Tornado handlers logging into the notebook.
1 """Tornado handlers for logging into the notebook."""
2
2
3 Authors:
3 # Copyright (c) IPython Development Team.
4
4 # Distributed under the terms of the Modified BSD License.
5 * Brian Granger
6 * Phil Elson
7 """
8
9 #-----------------------------------------------------------------------------
10 # Copyright (C) 2014 The IPython Development Team
11 #
12 # Distributed under the terms of the BSD License. The full license is in
13 # the file COPYING, distributed as part of this software.
14 #-----------------------------------------------------------------------------
15
16 #-----------------------------------------------------------------------------
17 # Imports
18 #-----------------------------------------------------------------------------
19
5
20 import uuid
6 import uuid
21
7
22 from tornado.escape import url_escape
8 from tornado.escape import url_escape
23 from tornado import web
24
9
25 from IPython.config.configurable import Configurable
26 from IPython.lib.security import passwd_check
10 from IPython.lib.security import passwd_check
27
11
28 from ..base.handlers import IPythonHandler
12 from ..base.handlers import IPythonHandler
29
13
30 #-----------------------------------------------------------------------------
31 # Handler
32 #-----------------------------------------------------------------------------
33
14
34 class LoginHandler(IPythonHandler):
15 class LoginHandler(IPythonHandler):
35 """ The basic IPythonWebApplication login handler which authenticates with a
16 """The basic tornado login handler
36 hashed password from the configuration.
37
17
18 authenticates with a hashed password from the configuration.
38 """
19 """
39 def _render(self, message=None):
20 def _render(self, message=None):
40 self.write(self.render_template('login.html',
21 self.write(self.render_template('login.html',
@@ -47,12 +28,15 b' class LoginHandler(IPythonHandler):'
47 self.redirect(self.get_argument('next', default=self.base_url))
28 self.redirect(self.get_argument('next', default=self.base_url))
48 else:
29 else:
49 self._render()
30 self._render()
31
32 @property
33 def hashed_password(self):
34 return self.password_from_settings(self.settings)
50
35
51 def post(self):
36 def post(self):
52 hashed_password = self.password_from_configuration(self.application)
53 typed_password = self.get_argument('password', default=u'')
37 typed_password = self.get_argument('password', default=u'')
54 if self.login_available(self.application):
38 if self.login_available(self.settings):
55 if passwd_check(hashed_password, typed_password):
39 if passwd_check(self.hashed_password, typed_password):
56 self.set_secure_cookie(self.cookie_name, str(uuid.uuid4()))
40 self.set_secure_cookie(self.cookie_name, str(uuid.uuid4()))
57 else:
41 else:
58 self._render(message={'error': 'Invalid password'})
42 self._render(message={'error': 'Invalid password'})
@@ -67,20 +51,20 b' class LoginHandler(IPythonHandler):'
67 if ssl_options is None:
51 if ssl_options is None:
68 notebook_app.log.critical(warning + " and not using encryption. This "
52 notebook_app.log.critical(warning + " and not using encryption. This "
69 "is not recommended.")
53 "is not recommended.")
70 if not self.password_from_configuration(notebook_app):
54 if not notebook_app.password:
71 notebook_app.log.critical(warning + " and not using authentication. "
55 notebook_app.log.critical(warning + " and not using authentication. "
72 "This is highly insecure and not recommended.")
56 "This is highly insecure and not recommended.")
73
57
74 @staticmethod
58 @staticmethod
75 def password_from_configuration(webapp):
59 def password_from_settings(settings):
76 """ Return the hashed password from the given NotebookWebApplication's configuration.
60 """Return the hashed password from the tornado settings.
77
78 If there is no configured password, None will be returned.
79
61
62 If there is no configured password, an empty string will be returned.
80 """
63 """
81 return webapp.settings['config']['NotebookApp'].get('password', None)
64 return settings.get('password', u'')
82
65
83 @classmethod
66 @classmethod
84 def login_available(cls, webapp):
67 def login_available(cls, settings):
85 """Whether this LoginHandler is needed - and therefore whether the login page should be displayed."""
68 """Whether this LoginHandler is needed - and therefore whether the login page should be displayed."""
86 return bool(cls.password_from_configuration(webapp))
69 return bool(cls.password_from_settings(settings))
70
@@ -88,14 +88,12 b' class AuthenticatedHandler(web.RequestHandler):'
88
88
89 @property
89 @property
90 def logged_in(self):
90 def logged_in(self):
91 """Is a user currently logged in?
91 """Is a user currently logged in?"""
92
93 """
94 user = self.get_current_user()
92 user = self.get_current_user()
95 return (user and not user == 'anonymous')
93 return (user and not user == 'anonymous')
96
94
97 @property
95 @property
98 def _login_handler(self):
96 def login_handler(self):
99 """Return the login handler for this application."""
97 """Return the login handler for this application."""
100 return self.settings['login_handler_class']
98 return self.settings['login_handler_class']
101
99
@@ -107,7 +105,7 b' class AuthenticatedHandler(web.RequestHandler):'
107 whether the user is already logged in or not.
105 whether the user is already logged in or not.
108
106
109 """
107 """
110 return bool(self._login_handler.login_available(self.application))
108 return bool(self.login_handler.login_available(self.settings))
111
109
112
110
113 class IPythonHandler(AuthenticatedHandler):
111 class IPythonHandler(AuthenticatedHandler):
General Comments 0
You need to be logged in to leave comments. Login now