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

Rename jhub-apps access_token cookie to be explicit #474

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions jhub_apps/service/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
logger = structlog.get_logger(__name__)


def create_access_token(data: dict, expires_delta: typing.Optional[timedelta] = None):
def _create_access_token(data: dict, expires_delta: typing.Optional[timedelta] = None):
aktech marked this conversation as resolved.
Show resolved Hide resolved
logger.info("Creating access token")
to_encode = data.copy()
if expires_delta:
Expand All @@ -22,7 +22,7 @@ def create_access_token(data: dict, expires_delta: typing.Optional[timedelta] =
return encoded_jwt


def get_jhub_token_from_jwt_token(token):
def _get_jhub_token_from_jwt_token(token):
aktech marked this conversation as resolved.
Show resolved Hide resolved
logger.info("Trying to get JHub Apps token from JWT Token")
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand Down
8 changes: 4 additions & 4 deletions jhub_apps/service/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
from starlette.responses import RedirectResponse

from jhub_apps.hub_client.hub_client import HubClient
from jhub_apps.service.auth import create_access_token
from jhub_apps.service.auth import _create_access_token
from jhub_apps.service.client import get_client
from jhub_apps.service.models import (
AuthorizationError,
HubApiError,
ServerCreation,
User,
)
from jhub_apps.service.security import get_current_user
from jhub_apps.service.security import get_current_user, JHUB_APPS_AUTH_COOKIE_NAME
from jhub_apps.service.utils import (
get_conda_envs,
get_jupyterhub_config,
Expand Down Expand Up @@ -74,14 +74,14 @@ async def get_token(code: str):
}
resp = await client.post("/oauth2/token", data=data)
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = create_access_token(
access_token = _create_access_token(
data={"sub": resp.json()}, expires_delta=access_token_expires
)
### resp.json() is {'access_token': <token>, 'token_type': 'Bearer'}
response = RedirectResponse(
os.environ["PUBLIC_HOST"] + "/hub/home", status_code=302
)
response.set_cookie(key="access_token", value=access_token, httponly=True)
response.set_cookie(key=JHUB_APPS_AUTH_COOKIE_NAME, value=access_token, httponly=True)
return response


Expand Down
8 changes: 5 additions & 3 deletions jhub_apps/service/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from fastapi.security.api_key import APIKeyQuery

from jhub_apps.hub_client.hub_client import get_users_and_group_allowed_to_share_with, is_jupyterhub_5
from .auth import get_jhub_token_from_jwt_token
from .auth import _get_jhub_token_from_jwt_token
from .client import get_client
from .models import User

Expand All @@ -15,9 +15,11 @@
### Authorization: bearer token (header).
### Hub technically supports cookie auth too, but it is deprecated so
### not being included here.
JHUB_APPS_AUTH_COOKIE_NAME = "jhub_apps_access_token"

auth_by_param = APIKeyQuery(name="token", auto_error=False)

auth_by_cookie = APIKeyCookie(name="access_token")
auth_by_cookie = APIKeyCookie(name=JHUB_APPS_AUTH_COOKIE_NAME)
aktech marked this conversation as resolved.
Show resolved Hide resolved
auth_url = os.environ["PUBLIC_HOST"] + "/hub/api/oauth2/authorize"
auth_by_header = OAuth2AuthorizationCodeBearer(
authorizationUrl=auth_url, tokenUrl="oauth_callback", auto_error=False
Expand Down Expand Up @@ -50,7 +52,7 @@ async def get_current_user(
detail="Must login with token parameter or Authorization bearer header",
)

token = get_jhub_token_from_jwt_token(token)
token = _get_jhub_token_from_jwt_token(token)

async with get_client() as client:
endpoint = "/user"
Expand Down
Loading