init default oauth client in init_db

ensures jupyterhub client is present,
which is required for creation of tokens, etc.
This commit is contained in:
Min RK
2021-04-13 13:32:16 +02:00
parent d85c316928
commit c8821b7700
2 changed files with 25 additions and 26 deletions

View File

@@ -214,8 +214,6 @@ class NewToken(Application):
hub = JupyterHub(parent=self) hub = JupyterHub(parent=self)
hub.load_config_file(hub.config_file) hub.load_config_file(hub.config_file)
hub.init_db() hub.init_db()
hub.init_hub()
hub.init_oauth()
def init_users(): def init_users():
loop = asyncio.new_event_loop() loop = asyncio.new_event_loop()
@@ -1694,6 +1692,26 @@ class JupyterHub(Application):
except orm.DatabaseSchemaMismatch as e: except orm.DatabaseSchemaMismatch as e:
self.exit(e) self.exit(e)
# ensure the default oauth client exists
if (
not self.db.query(orm.OAuthClient)
.filter_by(identifier="jupyterhub")
.one_or_none()
):
# create the oauth client for jupyterhub itself
# this allows us to distinguish between orphaned tokens
# (failed cascade deletion) and tokens issued by the hub
# it has no client_secret, which means it cannot be used
# to make requests
client = orm.OAuthClient(
identifier="jupyterhub",
secret="",
redirect_uri="",
description="JupyterHub",
)
self.db.add(client)
self.db.commit()
def init_hub(self): def init_hub(self):
"""Load the Hub URL config""" """Load the Hub URL config"""
hub_args = dict( hub_args = dict(
@@ -2295,23 +2313,6 @@ class JupyterHub(Application):
login_url=url_path_join(base_url, 'login'), login_url=url_path_join(base_url, 'login'),
token_expires_in=self.oauth_token_expires_in, token_expires_in=self.oauth_token_expires_in,
) )
# ensure the default oauth client exists
if (
not self.db.query(orm.OAuthClient)
.filter_by(identifier="jupyterhub")
.first()
):
# create the oauth client for jupyterhub itself
# this allows us to distinguish between orphaned tokens
# (failed cascade deletion) and tokens issued by the hub
# it has no client_secret, which means it cannot be used
# to make requests
self.oauth_provider.add_client(
client_id="jupyterhub",
client_secret="",
redirect_uri="",
description="JupyterHub",
)
def cleanup_oauth_clients(self): def cleanup_oauth_clients(self):
"""Cleanup any OAuth clients that shouldn't be in the database. """Cleanup any OAuth clients that shouldn't be in the database.

View File

@@ -125,16 +125,14 @@ def db():
"""Get a db session""" """Get a db session"""
global _db global _db
if _db is None: if _db is None:
_db = orm.new_session_factory('sqlite:///:memory:')() # make sure some initial db contents are filled out
# specifically, the 'default' jupyterhub oauth client
app = MockHub(db_url='sqlite:///:memory:')
app.init_db()
_db = app.db
user = orm.User(name=getuser()) user = orm.User(name=getuser())
_db.add(user) _db.add(user)
_db.commit() _db.commit()
# make sure some initial db contents are filled out
# specifically, the 'default' jupyterhub oauth client
app = MockHub()
app.db = _db
app.init_hub()
app.init_oauth()
return _db return _db