Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Mar 23, 2023
1 parent 15d529e commit 72aee7a
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 30 deletions.
4 changes: 2 additions & 2 deletions jupyverse_api/jupyverse_api/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@


class App:
"""A wrapper around FastAPI that checks for endpoint path conflicts.
"""
"""A wrapper around FastAPI that checks for endpoint path conflicts."""

_app: FastAPI
_router_paths: Dict[str, List[str]]

Expand Down
1 change: 0 additions & 1 deletion jupyverse_api/jupyverse_api/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from jupyverse_api import Config

from .models import User


class Auth(ABC):
Expand Down
28 changes: 8 additions & 20 deletions plugins/auth/fps_auth/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

logger = logging.getLogger("auth")


@dataclass
class Res:
cookie_authentication: Any
Expand All @@ -43,8 +44,8 @@ class Res:
github_cookie_authentication: Any
websocket_auth: Any

def get_backend(auth_config: _AuthConfig, frontend_config: FrontendConfig, db) -> Res:

def get_backend(auth_config: _AuthConfig, frontend_config: FrontendConfig, db) -> Res:
class NoAuthTransport(Transport):
scheme = None # type: ignore

Expand All @@ -62,7 +63,6 @@ def get_openapi_login_responses_success():
def get_openapi_logout_responses_success():
pass


class NoAuthStrategy(Strategy, Generic[models.UP, models.ID]):
async def read_token(
self, token: str | None, user_manager: BaseUserManager[models.UP, models.ID]
Expand All @@ -76,21 +76,18 @@ async def write_token(self, user: models.UP):
async def destroy_token(self, token: str, user: models.UP):
pass


class GitHubTransport(CookieTransport):
async def get_login_response(self, token: str, response: Response):
await super().get_login_response(token, response)
response.status_code = status.HTTP_302_FOUND
response.headers["Location"] = "/lab"


def get_noauth_strategy() -> NoAuthStrategy:
return NoAuthStrategy()

def get_jwt_strategy() -> JWTStrategy:
return JWTStrategy(secret=db.secret, lifetime_seconds=None)


noauth_authentication = AuthenticationBackend(
name="noauth",
transport=NoAuthTransport(),
Expand Down Expand Up @@ -133,11 +130,9 @@ async def on_after_register(self, user: User, request: Request | None = None):
),
)


async def get_user_manager(user_db: SQLAlchemyUserDatabase = Depends(db.get_user_db)):
yield UserManager(user_db)


def get_enabled_backends():
if auth_config.mode == "noauth" and not frontend_config.collaborative:
res = [noauth_authentication, github_cookie_authentication]
Expand Down Expand Up @@ -174,10 +169,9 @@ def current_user(permissions: Dict[str, List[str]] | None = None):
async def _(
response: Response,
token: str | None = None,
user: User | None = Depends(
fapi_users.current_user(
optional=True, get_enabled_backends=get_enabled_backends
)
user: User
| None = Depends(
fapi_users.current_user(optional=True, get_enabled_backends=get_enabled_backends)
),
user_manager: BaseUserManager[User, models.ID] = Depends(get_user_manager),
):
Expand All @@ -194,25 +188,19 @@ async def _(
if frontend_config.collaborative:
if not user and auth_config.mode == "noauth":
user = await create_guest(user_manager)
await cookie_authentication.login(
get_jwt_strategy(), user, response
)
await cookie_authentication.login(get_jwt_strategy(), user, response)

elif not user and auth_config.mode == "token":
global_user = await user_manager.get_by_email(auth_config.global_email)
if global_user and global_user.username == token:
user = await create_guest(user_manager)
await cookie_authentication.login(
get_jwt_strategy(), user, response
)
await cookie_authentication.login(get_jwt_strategy(), user, response)
else:
if auth_config.mode == "token":
global_user = await user_manager.get_by_email(auth_config.global_email)
if global_user and global_user.username == token:
user = global_user
await cookie_authentication.login(
get_jwt_strategy(), user, response
)
await cookie_authentication.login(get_jwt_strategy(), user, response)

if user:
return user
Expand Down
6 changes: 1 addition & 5 deletions plugins/auth/fps_auth/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Res:
get_user_db: Any
secret: Any


def get_db(auth_config: _AuthConfig) -> Res:
jupyter_dir = Path.home() / ".local" / "share" / "jupyter"
jupyter_dir.mkdir(parents=True, exist_ok=True)
Expand All @@ -73,25 +74,20 @@ def get_db(auth_config: _AuthConfig) -> Res:

database_url = f"sqlite+aiosqlite:///{userdb_path}"


engine = create_async_engine(database_url)
async_session_maker = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)


async def create_db_and_tables():
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)


async def get_async_session() -> AsyncGenerator[AsyncSession, None]:
async with async_session_maker() as session:
yield session


async def get_user_db(session: AsyncSession = Depends(get_async_session)):
yield SQLAlchemyUserDatabase(session, User, OAuthAccount)


return Res(
User=User,
async_session_maker=async_session_maker,
Expand Down
3 changes: 1 addition & 2 deletions plugins/auth/fps_auth/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ async def get_user_by_email(user_email):
async with _get_user_manager() as user_manager:
return await user_manager.get_by_email(user_email)


class _Auth(Auth, Router):
def __init__(self) -> None:
super().__init__(app)
Expand Down Expand Up @@ -140,7 +139,7 @@ async def _update_user(self, user, **kwargs):
def current_user(self, permissions: Dict[str, List[str]] | None = None) -> Callable:
return backend.current_user(permissions)

async def update_user(self, update_user = Depends(backend.update_user)) -> Callable:
async def update_user(self, update_user=Depends(backend.update_user)) -> Callable:
return update_user

def websocket_auth(
Expand Down

0 comments on commit 72aee7a

Please sign in to comment.