mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-18 07:23:00 +00:00
allow creating new API tokens on the command line
with jupyterhub token [username]
This commit is contained in:
@@ -79,6 +79,38 @@ flags = {
|
|||||||
|
|
||||||
SECRET_BYTES = 2048 # the number of bytes to use when generating new secrets
|
SECRET_BYTES = 2048 # the number of bytes to use when generating new secrets
|
||||||
|
|
||||||
|
class NewToken(Application):
|
||||||
|
"""Generate and print a new API token"""
|
||||||
|
name = 'jupyterhub-token'
|
||||||
|
description = """Generate and return new API token for a user."""
|
||||||
|
|
||||||
|
examples = """
|
||||||
|
$> jupyterhub token myuser
|
||||||
|
ab01cd23ef45
|
||||||
|
"""
|
||||||
|
|
||||||
|
name = Unicode()
|
||||||
|
aliases = {}
|
||||||
|
flags = {}
|
||||||
|
|
||||||
|
def parse_command_line(self, argv=None):
|
||||||
|
super().parse_command_line(argv=argv)
|
||||||
|
if len(self.extra_args) != 1:
|
||||||
|
print("Must specify exactly one username", file=sys.stderr)
|
||||||
|
self.exit(1)
|
||||||
|
self.name = self.extra_args[0]
|
||||||
|
|
||||||
|
def start(self):
|
||||||
|
hub = JupyterHub(parent=self)
|
||||||
|
hub.init_db()
|
||||||
|
user = orm.User.find(hub.db, self.name)
|
||||||
|
if user is None:
|
||||||
|
print("No such user: %s" % self.name)
|
||||||
|
self.exit(1)
|
||||||
|
token = user.new_api_token()
|
||||||
|
print(token)
|
||||||
|
|
||||||
|
|
||||||
class JupyterHub(Application):
|
class JupyterHub(Application):
|
||||||
"""An Application for starting a Multi-User Jupyter Notebook server."""
|
"""An Application for starting a Multi-User Jupyter Notebook server."""
|
||||||
name = 'jupyterhub'
|
name = 'jupyterhub'
|
||||||
@@ -104,6 +136,10 @@ class JupyterHub(Application):
|
|||||||
aliases = Dict(aliases)
|
aliases = Dict(aliases)
|
||||||
flags = Dict(flags)
|
flags = Dict(flags)
|
||||||
|
|
||||||
|
subcommands = {
|
||||||
|
'token': (NewToken, "Generate an API token for a user")
|
||||||
|
}
|
||||||
|
|
||||||
classes = List([
|
classes = List([
|
||||||
Spawner,
|
Spawner,
|
||||||
LocalProcessSpawner,
|
LocalProcessSpawner,
|
||||||
@@ -688,7 +724,7 @@ class JupyterHub(Application):
|
|||||||
@catch_config_error
|
@catch_config_error
|
||||||
def initialize(self, *args, **kwargs):
|
def initialize(self, *args, **kwargs):
|
||||||
super().initialize(*args, **kwargs)
|
super().initialize(*args, **kwargs)
|
||||||
if self.generate_config:
|
if self.generate_config or self.subapp:
|
||||||
return
|
return
|
||||||
self.load_config_file(self.config_file)
|
self.load_config_file(self.config_file)
|
||||||
self.init_logging()
|
self.init_logging()
|
||||||
@@ -793,6 +829,9 @@ class JupyterHub(Application):
|
|||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
"""Start the whole thing"""
|
"""Start the whole thing"""
|
||||||
|
if self.subapp:
|
||||||
|
return self.subapp.start()
|
||||||
|
|
||||||
if self.generate_config:
|
if self.generate_config:
|
||||||
self.write_config_file()
|
self.write_config_file()
|
||||||
return
|
return
|
||||||
|
@@ -1,7 +1,9 @@
|
|||||||
"""Test the JupyterHub entry point"""
|
"""Test the JupyterHub entry point"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
from getpass import getuser
|
||||||
from subprocess import check_output
|
from subprocess import check_output
|
||||||
from tempfile import NamedTemporaryFile
|
from tempfile import NamedTemporaryFile
|
||||||
|
|
||||||
@@ -10,6 +12,12 @@ def test_help_all():
|
|||||||
assert '--ip' in out
|
assert '--ip' in out
|
||||||
assert '--JupyterHub.ip' in out
|
assert '--JupyterHub.ip' in out
|
||||||
|
|
||||||
|
def test_token_app():
|
||||||
|
cmd = [sys.executable, '-m', 'jupyterhub', 'token']
|
||||||
|
out = check_output(cmd + ['--help-all']).decode('utf8', 'replace')
|
||||||
|
out = check_output(cmd + [getuser()]).decode('utf8', 'replace').strip()
|
||||||
|
assert re.match(r'^[a-z0-9]+$', out)
|
||||||
|
|
||||||
def test_generate_config():
|
def test_generate_config():
|
||||||
with NamedTemporaryFile(prefix='jupyterhub_config', suffix='.py') as tf:
|
with NamedTemporaryFile(prefix='jupyterhub_config', suffix='.py') as tf:
|
||||||
cfg_file = tf.name
|
cfg_file = tf.name
|
||||||
|
Reference in New Issue
Block a user