From 2bb9f4f444053d68e996a8c69eb4b559ca70476b Mon Sep 17 00:00:00 2001 From: Min RK Date: Wed, 30 Aug 2017 11:53:37 +0200 Subject: [PATCH 1/4] implement null authenticator --- jupyterhub/nullauthenticator.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 jupyterhub/nullauthenticator.py diff --git a/jupyterhub/nullauthenticator.py b/jupyterhub/nullauthenticator.py new file mode 100644 index 00000000..63f31ba0 --- /dev/null +++ b/jupyterhub/nullauthenticator.py @@ -0,0 +1,30 @@ +"""Null Authenticator for JupyterHub + +For cases where authentication should be disabled, +e.g. only allowing access via API tokens. +""" + +# Copyright (c) Jupyter Development Team. +# Distributed under the terms of the Modified BSD License. + +from jupyterhub.auth import Authenticator +from jupyterhub.handlers.base import BaseHandler + +__version__ = '1.0.0.dev' + + +class NullLoginHandler(BaseHandler): + def get(self): + raise web.HTTPError(403, "Login is not supported") + + +class NullAuthenticator(Authenticator): + + # auto_login skips 'Login with...' page on Hub 0.8 + auto_login = True + + # for Hub 0.7, show 'login with...' + login_service = 'null' + + def get_handlers(self, app): + return [('/nologin', NullLoginHandler)] From 2ade7328d1653b06c2675690e52ce6926b2430bf Mon Sep 17 00:00:00 2001 From: Simon Li Date: Thu, 23 Sep 2021 20:28:47 +0100 Subject: [PATCH 2/4] nullauthenticator: relative imports, entrypoint, doc --- jupyterhub/nullauthenticator.py | 20 +++++++++----------- setup.py | 1 + 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/jupyterhub/nullauthenticator.py b/jupyterhub/nullauthenticator.py index 63f31ba0..63f23b3b 100644 --- a/jupyterhub/nullauthenticator.py +++ b/jupyterhub/nullauthenticator.py @@ -1,16 +1,7 @@ -"""Null Authenticator for JupyterHub - -For cases where authentication should be disabled, -e.g. only allowing access via API tokens. -""" - # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. - -from jupyterhub.auth import Authenticator -from jupyterhub.handlers.base import BaseHandler - -__version__ = '1.0.0.dev' +from .auth import Authenticator +from .handlers.base import BaseHandler class NullLoginHandler(BaseHandler): @@ -19,6 +10,13 @@ class NullLoginHandler(BaseHandler): class NullAuthenticator(Authenticator): + """Null Authenticator for JupyterHub + + For cases where authentication should be disabled, + e.g. only allowing access via API tokens. + + .. versionadded:: 2.0 + """ # auto_login skips 'Login with...' page on Hub 0.8 auto_login = True diff --git a/setup.py b/setup.py index 2c189fa6..5995080d 100755 --- a/setup.py +++ b/setup.py @@ -100,6 +100,7 @@ setup_args = dict( 'default = jupyterhub.auth:PAMAuthenticator', 'pam = jupyterhub.auth:PAMAuthenticator', 'dummy = jupyterhub.auth:DummyAuthenticator', + "null = jupyterhub.nullauthenticator:NullAuthenticator", ], 'jupyterhub.proxies': [ 'default = jupyterhub.proxy:ConfigurableHTTPProxy', From 8a6b364ca5d69b04eb38e8ac06ca4b9cfc1c5b8a Mon Sep 17 00:00:00 2001 From: Simon Li Date: Thu, 23 Sep 2021 20:29:31 +0100 Subject: [PATCH 3/4] nullauthenticator: missing import --- jupyterhub/nullauthenticator.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/jupyterhub/nullauthenticator.py b/jupyterhub/nullauthenticator.py index 63f23b3b..1e950066 100644 --- a/jupyterhub/nullauthenticator.py +++ b/jupyterhub/nullauthenticator.py @@ -1,5 +1,7 @@ # Copyright (c) Jupyter Development Team. # Distributed under the terms of the Modified BSD License. +from tornado import web + from .auth import Authenticator from .handlers.base import BaseHandler From 28f56ba5104e7971c18914e3cb342bd1c72677cb Mon Sep 17 00:00:00 2001 From: Simon Li Date: Mon, 27 Sep 2021 23:05:53 +0100 Subject: [PATCH 4/4] Simplify NullAuthenticator, add test --- jupyterhub/auth.py | 19 +++++++++++++++++++ jupyterhub/nullauthenticator.py | 30 ------------------------------ jupyterhub/tests/test_auth.py | 12 ++++++++++++ setup.py | 2 +- 4 files changed, 32 insertions(+), 31 deletions(-) delete mode 100644 jupyterhub/nullauthenticator.py diff --git a/jupyterhub/auth.py b/jupyterhub/auth.py index bd4270e6..41ad1058 100644 --- a/jupyterhub/auth.py +++ b/jupyterhub/auth.py @@ -1173,3 +1173,22 @@ class DummyAuthenticator(Authenticator): return data['username'] return None return data['username'] + + +class NullAuthenticator(Authenticator): + """Null Authenticator for JupyterHub + + For cases where authentication should be disabled, + e.g. only allowing access via API tokens. + + .. versionadded:: 2.0 + """ + + # auto_login skips 'Login with...' page on Hub 0.8 + auto_login = True + + # for Hub 0.7, show 'login with...' + login_service = 'null' + + def get_handlers(self, app): + return [] diff --git a/jupyterhub/nullauthenticator.py b/jupyterhub/nullauthenticator.py deleted file mode 100644 index 1e950066..00000000 --- a/jupyterhub/nullauthenticator.py +++ /dev/null @@ -1,30 +0,0 @@ -# Copyright (c) Jupyter Development Team. -# Distributed under the terms of the Modified BSD License. -from tornado import web - -from .auth import Authenticator -from .handlers.base import BaseHandler - - -class NullLoginHandler(BaseHandler): - def get(self): - raise web.HTTPError(403, "Login is not supported") - - -class NullAuthenticator(Authenticator): - """Null Authenticator for JupyterHub - - For cases where authentication should be disabled, - e.g. only allowing access via API tokens. - - .. versionadded:: 2.0 - """ - - # auto_login skips 'Login with...' page on Hub 0.8 - auto_login = True - - # for Hub 0.7, show 'login with...' - login_service = 'null' - - def get_handlers(self, app): - return [('/nologin', NullLoginHandler)] diff --git a/jupyterhub/tests/test_auth.py b/jupyterhub/tests/test_auth.py index 58d48456..1f627426 100644 --- a/jupyterhub/tests/test_auth.py +++ b/jupyterhub/tests/test_auth.py @@ -3,6 +3,7 @@ # Distributed under the terms of the Modified BSD License. import logging from unittest import mock +from urllib.parse import urlparse import pytest from requests import HTTPError @@ -12,6 +13,8 @@ from .mocking import MockPAMAuthenticator from .mocking import MockStructGroup from .mocking import MockStructPasswd from .utils import add_user +from .utils import async_requests +from .utils import public_url from jupyterhub import auth from jupyterhub import crypto from jupyterhub import orm @@ -515,3 +518,12 @@ def test_deprecated_methods_subclass(): assert authenticator.check_whitelist("subclass-allowed") assert not authenticator.check_allowed("otheruser") assert not authenticator.check_whitelist("otheruser") + + +async def test_nullauthenticator(app): + with mock.patch.dict( + app.tornado_settings, {"authenticator": auth.NullAuthenticator(parent=app)} + ): + r = await async_requests.get(public_url(app)) + assert urlparse(r.url).path.endswith("/hub/login") + assert r.status_code == 403 diff --git a/setup.py b/setup.py index 5995080d..95cc73cd 100755 --- a/setup.py +++ b/setup.py @@ -100,7 +100,7 @@ setup_args = dict( 'default = jupyterhub.auth:PAMAuthenticator', 'pam = jupyterhub.auth:PAMAuthenticator', 'dummy = jupyterhub.auth:DummyAuthenticator', - "null = jupyterhub.nullauthenticator:NullAuthenticator", + 'null = jupyterhub.auth:NullAuthenticator', ], 'jupyterhub.proxies': [ 'default = jupyterhub.proxy:ConfigurableHTTPProxy',