Skip to content

Commit

Permalink
Fix fps_auth_jupyterhub (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart authored Aug 29, 2023
1 parent f3c131f commit cca0480
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 2 additions & 2 deletions plugins/auth_jupyterhub/fps_auth_jupyterhub/launch.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os
from urllib.parse import urlparse
from urllib.parse import unquote, urlparse

from jupyverse_api.cli import main


def launch():
service_url = os.environ.get("JUPYTERHUB_SERVICE_URL")
service_url = unquote(os.environ.get("JUPYTERHUB_SERVICE_URL"))
url = urlparse(service_url)
try:
return main.callback(
Expand Down
17 changes: 16 additions & 1 deletion plugins/auth_jupyterhub/fps_auth_jupyterhub/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def get_oauth_callback(
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)

token = self.hub_auth.token_for_code(code)
hub_user = self.hub_auth.user_for_token(token)
hub_user = await self.hub_auth.user_for_token(token, use_cache=False, sync=False)
async with self.db_lock:
db_session.add(
UserDB(
Expand Down Expand Up @@ -101,6 +101,16 @@ async def _(
jupyverse_jupyterhub_token: Annotated[Union[str, None], Cookie()] = None,
):
if jupyverse_jupyterhub_token is not None:
hub_user = await self.hub_auth.user_for_token(
jupyverse_jupyterhub_token, use_cache=False, sync=False
)
scopes = self.hub_auth.check_scopes(self.hub_auth.access_scopes, hub_user)
if not scopes:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail=f"User {hub_user['name']} cannot access this server",
)

async with self.db_lock:
user_db = await db_session.scalar(
select(UserDB).filter_by(token=jupyverse_jupyterhub_token)
Expand All @@ -125,6 +135,11 @@ async def _(
task.add_done_callback(self.background_tasks.discard)
return user

if permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
)

state = self.hub_auth.generate_state(next_url=str(request.url))
raise HTTPException(
status_code=status.HTTP_307_TEMPORARY_REDIRECT,
Expand Down

0 comments on commit cca0480

Please sign in to comment.