|
#!/usr/bin/env python |
|
from asyncio import get_event_loop |
|
|
|
from jupyterhub.auth import Authenticator |
|
from jupyterhub.traitlets import EntryPointType |
|
from traitlets import Dict, Instance, List, Unicode, default |
|
from traitlets.config import Application, Configurable |
|
|
|
|
|
class AuthTester(Application): |
|
name = Unicode("AuthTester") |
|
log_level = Unicode("DEBUG").tag(config=True) |
|
|
|
classes = List() |
|
|
|
@default("classes") |
|
def _load_classes(self): |
|
classes = {Authenticator} |
|
for entry_point in ( |
|
self.traits(config=True)["authenticator_class"].load_entry_points().values() |
|
): |
|
cls = entry_point.load() |
|
if issubclass(cls, Configurable): |
|
classes.add(cls) |
|
return list(classes) |
|
|
|
config_file = Unicode("jupyterhub_config.py", help="Load this config file").tag( |
|
config=True |
|
) |
|
|
|
authenticator_class = EntryPointType( |
|
default_value=None, |
|
klass=Authenticator, |
|
entry_point_group="jupyterhub.authenticators", |
|
help="Class for authenticating users", |
|
).tag(config=True) |
|
|
|
@default("authenticator_class") |
|
def _authenticator_class_default(self): |
|
return self.config.JupyterHub.authenticator_class |
|
|
|
authenticator = Instance(Authenticator) |
|
|
|
@default("authenticator") |
|
def _authenticator_default(self): |
|
return self.authenticator_class(parent=self) |
|
|
|
username = Unicode(help="Test login with this username").tag(config=True) |
|
password = Unicode(help="Test login with this password").tag(config=True) |
|
|
|
aliases = Dict( |
|
dict( |
|
c="AuthTester.config_file", |
|
p="AuthTester.password", |
|
u="AuthTester.username", |
|
) |
|
) |
|
|
|
def initialize(self, argv=None): |
|
self.parse_command_line(argv) |
|
if self.config_file: |
|
self.load_config_file(self.config_file) |
|
self.load_config_environ() |
|
|
|
def start(self): |
|
print("app.config:") |
|
print(self.config) |
|
print("try running with --help-all to see all available flags") |
|
|
|
def auth(self): |
|
self.log.info( |
|
"Calling %s.authenticate()", self.authenticator.__class__.__name__ |
|
) |
|
loop = get_event_loop() |
|
r = loop.run_until_complete( |
|
self.authenticator.authenticate( |
|
handler=None, data=dict(username=self.username, password=self.password) |
|
), |
|
) |
|
self.log.info(r) |
|
|
|
|
|
def main(): |
|
app = AuthTester() |
|
app.initialize() |
|
# app.start() |
|
app.auth() |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |