-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add jupyter-authenticator * add task-plugin and spi
- Loading branch information
1 parent
c4c9755
commit 20849e8
Showing
49 changed files
with
1,816 additions
and
1 deletion.
There are no files selected for viewing
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
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
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,2 @@ | ||
include CHANGELOG.md | ||
include *requirements.txt |
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,8 @@ | ||
# __version__ should be updated using tbump, based on configuration in | ||
# pyproject.toml, according to instructions in RELEASE.md. | ||
# | ||
__version__ = "1.0.0.dev" | ||
|
||
# version_info looks like (1, 2, 3, "dev") if __version__ is 1.2.3.dev | ||
version_info = tuple(int(p) if p.isdigit() | ||
else p for p in __version__.split(".")) |
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,63 @@ | ||
""" | ||
A JupyterHub authenticator class for use with WeDPR as an identity provider. | ||
""" | ||
from jupyter_server.auth.identity import IdentityProvider | ||
from jupyter_server.auth.identity import User | ||
import jwt | ||
from .wedpr_token_content import WeDPRTokenContent | ||
from tornado import web | ||
from traitlets import Unicode, default | ||
import os | ||
|
||
|
||
class WeDPRIdentityProvider(IdentityProvider): | ||
"""Authenticate local UNIX users with WeDPR-Auth""" | ||
AUTH_TOKEN_FIELD = "Authorization" | ||
AUTH_ALGORITHMS = ["HS256", "HS512", "HS384"] | ||
|
||
auth_secret = Unicode("<generated>", | ||
help="auth_secret").tag(config=True) | ||
cookie_secret_file = Unicode( | ||
'jupyterlab_authorization_secret', help="""File in which to store the authorization secret.""" | ||
).tag(config=True) | ||
|
||
@default("cookie_secret_file") | ||
def _cookie_secret_file_default(self): | ||
if os.getenv("JUPYTER_AUTH_SECRET"): | ||
return os.getenv("JUPYTER_AUTH_SECRET") | ||
return "jupyter_lab_secret" | ||
|
||
@default("auth_secret") | ||
def _auth_secret_default(self): | ||
secret_file = os.path.abspath( | ||
os.path.expanduser(self.cookie_secret_file)) | ||
if os.path.exists(secret_file): | ||
self.log.info(f"init auth secret, load from: {secret_file}") | ||
with open(secret_file) as f: | ||
return f.read().strip() | ||
return None | ||
|
||
def get_user(self, handler: web.RequestHandler): | ||
"""Authenticate with jwt token, and return the username if login is successful. | ||
Return None otherwise. | ||
""" | ||
try: | ||
token = handler.request.headers.get( | ||
WeDPRIdentityProvider.AUTH_TOKEN_FIELD, "") | ||
except KeyError as e: | ||
self.log.warning( | ||
f"WeDPR auth failed for no authorization inforamtion defined! error: {e}") | ||
return None | ||
try: | ||
token_payload = jwt.decode( | ||
token, self.auth_secret, WeDPRIdentityProvider.AUTH_ALGORITHMS) | ||
user_info = WeDPRTokenContent.deserialize(token_payload) | ||
except Exception as e: | ||
self.log.warning( | ||
f"WeDPR auth failed for jwt verify failed! error: {e}") | ||
return None | ||
if user_info is None: | ||
return None | ||
user_name = user_info.get_user_information().username | ||
self.log.info(f"WeDPR auth success, username: {user_name}") | ||
return User(username=user_name) |
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,32 @@ | ||
# -*- coding: utf-8 -*- | ||
import json | ||
from .wedpr_user_information import WeDPRUserInformation | ||
|
||
|
||
class WeDPRTokenContent: | ||
USER_TOKEN_CLAIM = "user" | ||
|
||
def __init__(self, token_content): | ||
self._token_content = token_content | ||
|
||
def get_token_content(self): | ||
return self._token_content | ||
|
||
def set_token_content(self, _token_content): | ||
self._token_content = _token_content | ||
|
||
def get_token_content_by_key(self, key): | ||
if key in self._token_content: | ||
return self._token_content[key] | ||
return None | ||
|
||
def get_user_information(self) -> WeDPRUserInformation: | ||
user_info = self.get_token_content_by_key( | ||
WeDPRTokenContent.USER_TOKEN_CLAIM) | ||
if user_info is None: | ||
return None | ||
return WeDPRUserInformation(**json.loads(user_info)) | ||
|
||
@staticmethod | ||
def deserialize(token_payload): | ||
return WeDPRTokenContent(token_payload) |
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,25 @@ | ||
# -*- coding: utf-8 -*- | ||
from dataclasses import dataclass | ||
import dataclasses | ||
from dataclass_wizard import JSONWizard | ||
import inspect | ||
|
||
|
||
@dataclass | ||
class WeDPRGroupInfo(JSONWizard): | ||
groupId: str | ||
groupName: str | ||
groupAdminName: str | ||
|
||
|
||
@dataclass | ||
class WeDPRUserInformation: | ||
username: str | ||
roleName: str | ||
permissions: list | ||
|
||
def __init__(self, **kwargs): | ||
names = set([f.name for f in dataclasses.fields(self)]) | ||
for k, v in kwargs.items(): | ||
if k in names: | ||
setattr(self, k, v) |
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,87 @@ | ||
# autoflake is used for autoformatting Python code | ||
# | ||
# ref: https://github.com/PyCQA/autoflake#readme | ||
# | ||
[tool.autoflake] | ||
ignore-init-module-imports = true | ||
remove-all-unused-imports = true | ||
remove-duplicate-keys = true | ||
#remove-unused-variables = true | ||
|
||
|
||
# isort is used for autoformatting Python code | ||
# | ||
# ref: https://pycqa.github.io/isort/ | ||
# | ||
[tool.isort] | ||
profile = "black" | ||
|
||
|
||
# black is used for autoformatting Python code | ||
# | ||
# ref: https://black.readthedocs.io/en/stable/ | ||
# | ||
[tool.black] | ||
skip-string-normalization = true | ||
# target-version should be all supported versions, see | ||
# https://github.com/psf/black/issues/751#issuecomment-473066811 | ||
target_version = [ | ||
"py38", | ||
"py39", | ||
"py310", | ||
"py311", | ||
] | ||
|
||
|
||
# pytest is used for running Python based tests | ||
# | ||
# ref: https://docs.pytest.org/en/stable/ | ||
# | ||
[tool.pytest.ini_options] | ||
addopts = "--verbose --color=yes --durations=10 --cov=authenticator" | ||
asyncio_mode = "auto" | ||
testpaths = [ | ||
"authenticator/tests" | ||
] | ||
|
||
|
||
# pytest-cov / coverage is used to measure code coverage of tests | ||
# | ||
# ref: https://coverage.readthedocs.io/en/stable/config.html | ||
# | ||
[tool.coverage.run] | ||
omit = [ | ||
"authenticator/tests/**", | ||
] | ||
|
||
|
||
# tbump is used to simplify and standardize the release process when updating | ||
# the version, making a git commit and tag, and pushing changes. | ||
# | ||
# ref: https://github.com/your-tools/tbump#readme | ||
# | ||
[tool.tbump] | ||
github_url = "https://github.com/wedpr/incubator-wedpr" | ||
|
||
[tool.tbump.version] | ||
current = "1.0.0.dev" | ||
regex = ''' | ||
(?P<major>\d+) | ||
\. | ||
(?P<minor>\d+) | ||
\. | ||
(?P<patch>\d+) | ||
(?P<pre>((a|b|rc)\d+)|) | ||
\.? | ||
(?P<dev>(?<=\.)dev\d*|) | ||
''' | ||
|
||
[tool.tbump.git] | ||
message_template = "Bump to {new_version}" | ||
tag_template = "{new_version}" | ||
|
||
[[tool.tbump.file]] | ||
src = "setup.py" | ||
|
||
[[tool.tbump.file]] | ||
src = "authenticator/_version.py" |
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,7 @@ | ||
traitlets>=4.3.2 | ||
jupyter_server>=2.2 | ||
pyjwt>=2 | ||
tornado | ||
PyJWT | ||
dataclass_wizard | ||
|
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,74 @@ | ||
#!/usr/bin/env python | ||
# Copyright (c) Jupyter Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||
# ----------------------------------------------------------------------------- | ||
# Minimal Python version sanity check (from IPython/Jupyterhub) | ||
# ----------------------------------------------------------------------------- | ||
|
||
import sys | ||
|
||
from setuptools import find_packages, setup | ||
from setuptools.command.bdist_egg import bdist_egg | ||
|
||
|
||
class bdist_egg_disabled(bdist_egg): | ||
"""Disabled version of bdist_egg | ||
Prevents setup.py install from performing setuptools' default easy_install, | ||
which it should never ever do. | ||
""" | ||
|
||
def run(self): | ||
sys.exit( | ||
"Aborting implicit building of eggs. Use `pip install .` to install from source." | ||
) | ||
|
||
|
||
setup_args = dict( | ||
name='authenticator', | ||
packages=find_packages(), | ||
version="1.0.0.dev", | ||
description="Authenticator: Authenticate JupyterHub users with wedpr providers", | ||
long_description_content_type="text/markdown", | ||
author="Jupyter Development Team", | ||
author_email="[email protected]", | ||
url="https://jupyter.org", | ||
license="BSD", | ||
platforms="Linux, Mac OS X", | ||
keywords=['Interactive', 'Interpreter', 'Shell', 'Web'], | ||
python_requires=">=3.8", | ||
include_package_data=True, | ||
entry_points={ | ||
'jupyter_server.IdentityProviders': [ | ||
'wedpr-identity-provider = authenticator.wedpr_identity_provider.WeDPRIdentityProvider' | ||
], | ||
}, | ||
classifiers=[ | ||
'Intended Audience :: Developers', | ||
'Intended Audience :: System Administrators', | ||
'Intended Audience :: Science/Research', | ||
'License :: OSI Approved :: Apache License', | ||
'Programming Language :: Python', | ||
'Programming Language :: Python :: 3', | ||
], | ||
) | ||
|
||
setup_args['cmdclass'] = { | ||
'bdist_egg': bdist_egg if 'bdist_egg' in sys.argv else bdist_egg_disabled, | ||
} | ||
|
||
setup_args['install_requires'] = install_requires = [] | ||
with open('requirements.txt') as f: | ||
for line in f.readlines(): | ||
req = line.strip() | ||
if not req or req.startswith(('-e', '#')): | ||
continue | ||
install_requires.append(req) | ||
|
||
|
||
def main(): | ||
setup(**setup_args) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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
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,15 @@ | ||
// Apply the java-library plugin to add support for Java Library | ||
plugins { | ||
id 'java' | ||
id 'com.github.sherter.google-java-format' | ||
} | ||
dependencies{ | ||
compile project(":wedpr-core-utils") | ||
} | ||
googleJavaFormat { | ||
//toolVersion = '1.7' | ||
options style: 'AOSP' | ||
source = sourceSets*.allJava | ||
include '**/*.java' | ||
//source = *.allJava | ||
} |
Oops, something went wrong.