-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""An helper service to redirect users to the right server option. | ||
It expects that the users is already authenticated. | ||
""" | ||
|
||
import os | ||
import os.path | ||
from urllib.parse import urlparse | ||
|
||
from jupyterhub.services.auth import HubOAuthCallbackHandler, HubOAuthenticated | ||
from jupyterhub.utils import url_path_join | ||
from tornado.httpserver import HTTPServer | ||
from tornado.ioloop import IOLoop | ||
from tornado.web import Application, RequestHandler, authenticated | ||
|
||
|
||
class D4ScienceHandler(HubOAuthenticated, RequestHandler): | ||
@authenticated | ||
def get(self): | ||
user = self.get_current_user() | ||
rname = self.request.path.split("/")[-1] | ||
if rname: | ||
rname = f"rname-{rname}" | ||
dest_url = url_path_join("/hub/spawn/", user["name"], rname) | ||
self.redirect( | ||
dest_url, | ||
permanent=False, | ||
) | ||
return | ||
|
||
|
||
def main(): | ||
app = Application( | ||
[ | ||
( | ||
url_path_join( | ||
os.environ["JUPYTERHUB_SERVICE_PREFIX"], "oauth_callback" | ||
), | ||
HubOAuthCallbackHandler, | ||
), | ||
( | ||
r"%s/[^/]*" % os.environ["JUPYTERHUB_SERVICE_PREFIX"], | ||
D4ScienceHandler, | ||
), | ||
], | ||
cookie_secret=os.urandom(32), | ||
) | ||
http_server = HTTPServer(app) | ||
url = urlparse(os.environ["JUPYTERHUB_SERVICE_URL"]) | ||
http_server.listen(url.port) | ||
IOLoop.current().start() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |