Fix setting cookie for services

and exercise it in tests
This commit is contained in:
Min RK
2016-09-26 14:30:00 +02:00
parent eb3252da28
commit 729b608eff
3 changed files with 26 additions and 2 deletions

View File

@@ -241,7 +241,10 @@ class BaseHandler(RequestHandler):
def set_service_cookie(self, user): def set_service_cookie(self, user):
"""set the login cookie for services""" """set the login cookie for services"""
self._set_user_cookie(user, self.service_server) self._set_user_cookie(user, orm.Server(
cookie_name='jupyterhub-services',
base_url=url_path_join(self.base_url, 'services')
))
def set_server_cookie(self, user): def set_server_cookie(self, user):
"""set the login cookie for the single-user server""" """set the login cookie for the single-user server"""
@@ -262,7 +265,7 @@ class BaseHandler(RequestHandler):
self.set_server_cookie(user) self.set_server_cookie(user)
# set single cookie for services # set single cookie for services
if self.db.query(orm.Service).first(): if self.db.query(orm.Service).filter(orm.Service.server != None).first():
self.set_service_cookie(user) self.set_service_cookie(user)
# create and set a new cookie token for the hub # create and set a new cookie token for the hub

View File

@@ -13,6 +13,7 @@ from urllib.parse import urlparse
import requests import requests
from tornado import web, httpserver, ioloop from tornado import web, httpserver, ioloop
from jupyterhub.services.auth import HubAuthenticated
class EchoHandler(web.RequestHandler): class EchoHandler(web.RequestHandler):
def get(self): def get(self):
@@ -36,6 +37,12 @@ class APIHandler(web.RequestHandler):
self.set_header('Content-Type', 'application/json') self.set_header('Content-Type', 'application/json')
self.write(r.text) self.write(r.text)
class WhoAmIHandler(HubAuthenticated, web.RequestHandler):
@web.authenticated
def get(self):
self.write(self.get_current_user())
def main(): def main():
if os.environ['JUPYTERHUB_SERVICE_URL']: if os.environ['JUPYTERHUB_SERVICE_URL']:
@@ -43,6 +50,7 @@ def main():
app = web.Application([ app = web.Application([
(r'.*/env', EnvHandler), (r'.*/env', EnvHandler),
(r'.*/api/(.*)', APIHandler), (r'.*/api/(.*)', APIHandler),
(r'.*/whoami/?', WhoAmIHandler),
(r'.*', EchoHandler), (r'.*', EchoHandler),
]) ])

View File

@@ -15,6 +15,7 @@ from tornado.web import RequestHandler, Application, authenticated, HTTPError
from ..services.auth import _ExpiringDict, HubAuth, HubAuthenticated from ..services.auth import _ExpiringDict, HubAuth, HubAuthenticated
from ..utils import url_path_join from ..utils import url_path_join
from .mocking import public_url
# mock for sending monotonic counter way into the future # mock for sending monotonic counter way into the future
monotonic_future = mock.patch('time.monotonic', lambda : sys.maxsize) monotonic_future = mock.patch('time.monotonic', lambda : sys.maxsize)
@@ -194,3 +195,15 @@ def test_hub_authenticated(request):
assert auth.login_url in r.headers['Location'] assert auth.login_url in r.headers['Location']
def test_service_cookie_auth(app, mockservice_url):
cookies = app.login_user('badger')
r = requests.get(public_url(app, mockservice_url) + '/whoami/', cookies=cookies)
r.raise_for_status()
print(r.text)
reply = r.json()
sub_reply = { key: reply.get(key, 'missing') for key in ['name', 'admin']}
assert sub_reply == {
'name': 'badger',
'admin': False,
}