store the multi-user state in a database

via SQLAlchemy ORM
This commit is contained in:
MinRK
2014-08-18 15:20:43 -07:00
parent a2d1481cd5
commit ec52f67cea
7 changed files with 671 additions and 275 deletions

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env python
"""Extend regular notebook server to be aware of multi-user things."""
"""Extend regular notebook server to be aware of multiuser things."""
import os
@@ -24,12 +24,12 @@ def verify_token(self, token):
# we've seen this token before, don't ask upstream again
return token_cache[token]
multiuser_api_url = self.settings['multiuser_api_url']
multiuser_api_key = self.settings['multiuser_api_key']
hub_api_url = self.settings['hub_api_url']
hub_api_key = self.settings['hub_api_key']
r = requests.get(utils.url_path_join(
multiuser_api_url, "authorizations", token,
hub_api_url, "authorizations", token,
),
headers = {'Authorization' : 'token %s' % multiuser_api_key}
headers = {'Authorization' : 'token %s' % hub_api_key}
)
if r.status_code == 404:
data = {'user' : ''}
@@ -53,48 +53,52 @@ def get_current_user(self):
if user == my_user:
return user
else:
raise web.HTTPError(403, "User %s does not have access to %s" % (user, my_user))
# import IPython
# IPython.embed()
return None
# imoprt
# raise web.HTTPError(403, "User %s does not have access to %s" % (user, my_user))
else:
self.log.debug("No token cookie")
return None
# register new multi-user related command-line aliases
# register new hub related command-line aliases
aliases = NotebookApp.aliases.get_default_value()
aliases.update({
'user' : 'SingleUserNotebookApp.user',
'cookie-name': 'SingleUserNotebookApp.cookie_name',
'multiuser-prefix': 'SingleUserNotebookApp.multiuser_prefix',
'multiuser-api-url': 'SingleUserNotebookApp.multiuser_api_url',
'hub-prefix': 'SingleUserNotebookApp.hub_prefix',
'hub-api-url': 'SingleUserNotebookApp.hub_api_url',
'base-url': 'SingleUserNotebookApp.base_url',
})
class SingleUserNotebookApp(NotebookApp):
"""A Subclass of the regular NotebookApp that is aware of the parent multi-user context."""
"""A Subclass of the regular NotebookApp that is aware of the parent multiuser context."""
user = Unicode(config=True)
cookie_name = Unicode(config=True)
multiuser_prefix = Unicode(config=True)
multiuser_api_url = Unicode(config=True)
hub_prefix = Unicode(config=True)
hub_api_url = Unicode(config=True)
aliases = aliases
browser = False
def init_webapp(self):
# monkeypatch authentication to use the multi-user
# monkeypatch authentication to use the hub
from IPython.html.base.handlers import AuthenticatedHandler
AuthenticatedHandler.verify_token = verify_token
AuthenticatedHandler.get_current_user = get_current_user
# load the multi-user related settings into the tornado settings dict
# load the hub related settings into the tornado settings dict
env = os.environ
s = self.webapp_settings
s['token_cache'] = {}
s['user'] = self.user
s['multiuser_api_key'] = env.get('IPY_API_TOKEN', '')
s['hub_api_key'] = env.get('IPY_API_TOKEN', '')
s['cookie_secret'] = env.get('IPY_COOKIE_SECRET', '')
s['cookie_name'] = self.cookie_name
s['login_url'] = utils.url_path_join(self.multiuser_prefix, 'login')
s['multiuser_api_url'] = self.multiuser_api_url
s['login_url'] = utils.url_path_join(self.hub_prefix, 'login')
s['hub_api_url'] = self.hub_api_url
super(SingleUserNotebookApp, self).init_webapp()