Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run pre-commit #176

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,10 @@ jobs:
pip install ./plugins/lab
pip install ./plugins/jupyterlab
pip install flake8 black mypy pytest pytest-asyncio requests ipykernel
pip install mypy pytest pytest-asyncio requests ipykernel
- name: Check style
- name: Check types
run: |
black --check jupyverse plugins tests
flake8 jupyverse plugins tests
mypy jupyverse
mypy plugins/contents/fps_contents
mypy plugins/kernels/fps_kernels
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,4 @@ $RECYCLE.BIN/
# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/git,linux,macos,python,windows,pycharm,jupyternotebook,vscode
# End of https://www.toptal.com/developers/gitignore/api/git,linux,macos,python,windows,pycharm,jupyternotebook,vscode
40 changes: 40 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
hooks:
- id: end-of-file-fixer
- id: check-case-conflict
- id: check-executables-have-shebangs
- id: requirements-txt-fixer
- id: check-added-large-files
- id: check-case-conflict
- id: check-toml
- id: check-yaml
- id: debug-statements
- id: forbid-new-submodules
- id: check-builtin-literals
- id: trailing-whitespace

- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
args: ["--line-length", "100"]

- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
files: \.py$
args: [--profile=black]

- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
exclude: binder
additional_dependencies:
[
"flake8-logging-format==0.6.0",
"flake8-implicit-str-concat==0.2.0",
]
65 changes: 34 additions & 31 deletions binder/jupyter_notebook_config.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
jupyverse_jlab_command = ' '.join([
'jupyverse',
'--no-open-browser',
'--authenticator.mode=noauth',
'--authenticator.collaborative',
'--RetroLab.enabled=false',
'--Lab.base_url={base_url}jupyverse-jlab/',
'--port={port}',
] + ['>jupyverse_jlab.log 2>&1'])
jupyverse_jlab_command = " ".join(
[
"jupyverse",
"--no-open-browser",
"--authenticator.mode=noauth",
"--authenticator.collaborative",
"--RetroLab.enabled=false",
"--Lab.base_url={base_url}jupyverse-jlab/",
"--port={port}",
]
+ [">jupyverse_jlab.log 2>&1"]
)


jupyverse_rlab_command = ' '.join([
'jupyverse',
'--no-open-browser',
'--authenticator.mode=noauth',
'--authenticator.collaborative',
'--JupyterLab.enabled=false',
'--Lab.base_url={base_url}jupyverse-rlab/',
'--port={port}',
] + ['>jupyverse_rlab.log 2>&1'])
jupyverse_rlab_command = " ".join(
[
"jupyverse",
"--no-open-browser",
"--authenticator.mode=noauth",
"--authenticator.collaborative",
"--JupyterLab.enabled=false",
"--Lab.base_url={base_url}jupyverse-rlab/",
"--port={port}",
]
+ [">jupyverse_rlab.log 2>&1"]
)


c.ServerProxy.servers = {
'jupyverse-jlab': {
'command': [
'/bin/bash', '-c', jupyverse_jlab_command
],
'timeout': 60,
'absolute_url': False
"jupyverse-jlab": {
"command": ["/bin/bash", "-c", jupyverse_jlab_command],
"timeout": 60,
"absolute_url": False,
},
'jupyverse-rlab': {
'command': [
'/bin/bash', '-c', jupyverse_rlab_command
],
'timeout': 60,
'absolute_url': False
"jupyverse-rlab": {
"command": ["/bin/bash", "-c", jupyverse_rlab_command],
"timeout": 60,
"absolute_url": False,
},
}

c.NotebookApp.default_url = '/jupyverse-jlab'
c.NotebookApp.default_url = "/jupyverse-jlab"

import logging

c.NotebookApp.log_level = logging.DEBUG
41 changes: 14 additions & 27 deletions plugins/auth/fps_auth/backends.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
from typing import Generic, Optional
from uuid import uuid4
from typing import Optional, Generic

from fps.exceptions import RedirectException # type: ignore

import httpx
from httpx_oauth.clients.github import GitHubOAuth2 # type: ignore
from fastapi import Depends, Response, HTTPException, status

from fastapi import Depends, HTTPException, Response, status
from fastapi_users import BaseUserManager, FastAPIUsers, models # type: ignore
from fastapi_users.authentication import (
AuthenticationBackend,
CookieTransport,
JWTStrategy,
)
from fastapi_users.authentication.transport.base import Transport
from fastapi_users.authentication.strategy.base import Strategy
from fastapi_users import FastAPIUsers, BaseUserManager, models # type: ignore
from starlette.requests import Request

from fastapi_users.authentication.transport.base import Transport
from fps.exceptions import RedirectException # type: ignore
from fps.logging import get_configured_logger # type: ignore
from httpx_oauth.clients.github import GitHubOAuth2 # type: ignore
from starlette.requests import Request

from .config import get_auth_config
from .db import secret, get_user_db
from .models import User, UserDB, UserCreate, UserUpdate
from .db import get_user_db, secret
from .models import User, UserCreate, UserDB, UserUpdate

logger = get_configured_logger("auth")

Expand All @@ -34,9 +31,7 @@ class NoAuthStrategy(Strategy, Generic[models.UC, models.UD]):
async def read_token(
self, token: Optional[str], user_manager: BaseUserManager[models.UC, models.UD]
) -> Optional[models.UD]:
active_user = await user_manager.user_db.get_by_email(
get_auth_config().global_email
)
active_user = await user_manager.user_db.get_by_email(get_auth_config().global_email)
return active_user


Expand Down Expand Up @@ -88,9 +83,7 @@ async def on_after_register(self, user: UserDB, request: Optional[Request] = Non
if oauth_account.oauth_name == "github":
async with httpx.AsyncClient() as client:
r = (
await client.get(
f"https://api.github.com/user/{oauth_account.account_id}"
)
await client.get(f"https://api.github.com/user/{oauth_account.account_id}")
).json()

user.anonymous = False
Expand Down Expand Up @@ -145,9 +138,7 @@ async def current_user(
response: Response,
token: Optional[str] = None,
user: User = Depends(
fapi_users.current_user(
optional=True, get_enabled_backends=get_enabled_backends
)
fapi_users.current_user(optional=True, get_enabled_backends=get_enabled_backends)
),
user_db=Depends(get_user_db),
user_manager: UserManager = Depends(get_user_manager),
Expand All @@ -164,17 +155,13 @@ async def current_user(
global_user = await user_db.get_by_email(auth_config.global_email)
if global_user and global_user.hashed_password == token:
active_user = await create_guest(user_db, auth_config)
await cookie_authentication.login(
get_jwt_strategy(), active_user, response
)
await cookie_authentication.login(get_jwt_strategy(), active_user, response)
else:
if auth_config.mode == "token":
global_user = await user_db.get_by_email(auth_config.global_email)
if global_user and global_user.hashed_password == token:
active_user = global_user
await cookie_authentication.login(
get_jwt_strategy(), active_user, response
)
await cookie_authentication.login(get_jwt_strategy(), active_user, response)

if active_user:
return active_user
Expand Down
8 changes: 3 additions & 5 deletions plugins/auth/fps_auth/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from uuid import uuid4
from typing import Optional
from pydantic import SecretStr
from uuid import uuid4

from fps.config import PluginModel, get_config # type: ignore
from fps.hooks import register_config, register_plugin_name # type: ignore
from pydantic import SecretStr


class AuthConfig(PluginModel):
Expand All @@ -15,9 +15,7 @@ class AuthConfig(PluginModel):
token: str = str(uuid4())
collaborative: bool = False
global_email: str = "guest@jupyter.com"
cookie_secure: bool = (
False # FIXME: should default to True, and set to False for tests
)
cookie_secure: bool = False # FIXME: should default to True, and set to False for tests
clear_users: bool = False
login_url: Optional[str] = None

Expand Down
21 changes: 10 additions & 11 deletions plugins/auth/fps_auth/db.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import secrets
from pathlib import Path

from fastapi_users.db import SQLAlchemyBaseUserTable, SQLAlchemyUserDatabase # type: ignore
from fastapi_users.db import SQLAlchemyBaseOAuthAccountTable # type: ignore
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base # type: ignore
from sqlalchemy import Boolean, String, Text, Column # type: ignore
import sqlalchemy # type: ignore
import databases # type: ignore
import sqlalchemy # type: ignore
from fastapi_users.db import SQLAlchemyBaseOAuthAccountTable # type: ignore
from fastapi_users.db import ( # type: ignore
SQLAlchemyBaseUserTable,
SQLAlchemyUserDatabase,
)
from fps.config import get_config # type: ignore
from sqlalchemy import Boolean, Column, String, Text # type: ignore
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base # type: ignore

from .config import AuthConfig
from .models import (
UserDB,
)
from .models import UserDB

auth_config = get_config(AuthConfig)

Expand Down Expand Up @@ -57,9 +58,7 @@ class OAuthAccount(SQLAlchemyBaseOAuthAccountTable, Base):
pass


engine = sqlalchemy.create_engine(
DATABASE_URL, connect_args={"check_same_thread": False}
)
engine = sqlalchemy.create_engine(DATABASE_URL, connect_args={"check_same_thread": False})

Base.metadata.create_all(engine)

Expand Down
2 changes: 1 addition & 1 deletion plugins/auth/fps_auth/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional

from pydantic import BaseModel
from fastapi_users import models # type: ignore
from pydantic import BaseModel


class JupyterUser(BaseModel):
Expand Down
23 changes: 9 additions & 14 deletions plugins/auth/fps_auth/routes.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
from uuid import uuid4

from fps.hooks import register_router # type: ignore
from fastapi import APIRouter, Depends
from fps.config import get_config # type: ignore
from fps.hooks import register_router # type: ignore
from fps.logging import get_configured_logger # type: ignore
from fps_uvicorn.config import UvicornConfig # type: ignore

from fastapi import APIRouter, Depends
from sqlalchemy.orm import sessionmaker # type: ignore

from .config import get_auth_config
from .db import user_db, secret, database, engine, UserTable
from .backends import (
fapi_users,
current_user,
cookie_authentication,
github_cookie_authentication,
current_user,
fapi_users,
github_authentication,
github_cookie_authentication,
)
from .config import get_auth_config
from .db import UserTable, database, engine, secret, user_db
from .models import User, UserDB

logger = get_configured_logger("auth")
Expand Down Expand Up @@ -74,17 +73,13 @@ async def get_users(user: User = Depends(current_user)):


# Cookie based auth login and logout
r_cookie_auth = register_router(
fapi_users.get_auth_router(cookie_authentication), prefix="/auth"
)
r_cookie_auth = register_router(fapi_users.get_auth_router(cookie_authentication), prefix="/auth")
r_register = register_router(fapi_users.get_register_router(), prefix="/auth")
r_user = register_router(fapi_users.get_users_router(), prefix="/auth/user")

# GitHub OAuth register router
r_github = register_router(
fapi_users.get_oauth_router(
github_authentication, github_cookie_authentication, secret
),
fapi_users.get_oauth_router(github_authentication, github_cookie_authentication, secret),
prefix="/auth/github",
)

Expand Down
Loading