Skip to content

Commit

Permalink
move cookie name to auth
Browse files Browse the repository at this point in the history
  • Loading branch information
aktech committed Sep 4, 2024
1 parent a0a77dd commit e0333f3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
8 changes: 4 additions & 4 deletions jhub_apps/service/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import jwt
from fastapi import HTTPException, status

from jhub_apps.service.security import JHUB_APPS_AUTH_COOKIE_NAME

logger = structlog.get_logger(__name__)

JHUB_APPS_AUTH_COOKIE_NAME = "jhub_apps_access_token"


def create_access_token(data: dict, expires_delta: typing.Optional[timedelta] = None):
def _create_access_token(data: dict, expires_delta: typing.Optional[timedelta] = None):
logger.info("Creating access token")
to_encode = data.copy()
if expires_delta:
Expand All @@ -24,7 +24,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):
logger.info("Trying to get JHub Apps token from JWT Token")
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand Down
6 changes: 3 additions & 3 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, JHUB_APPS_AUTH_COOKIE_NAME
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, JHUB_APPS_AUTH_COOKIE_NAME
from jhub_apps.service.security import get_current_user
from jhub_apps.service.utils import (
get_conda_envs,
get_jupyterhub_config,
Expand Down Expand Up @@ -74,7 +74,7 @@ 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'}
Expand Down
6 changes: 2 additions & 4 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, JHUB_APPS_AUTH_COOKIE_NAME
from .client import get_client
from .models import User

Expand All @@ -16,8 +16,6 @@
### 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=JHUB_APPS_AUTH_COOKIE_NAME)
Expand Down Expand Up @@ -53,7 +51,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

0 comments on commit e0333f3

Please sign in to comment.