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

fix: update get_username function #365

Merged
merged 4 commits into from
Apr 29, 2024
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
8 changes: 6 additions & 2 deletions ingest_api/runtime/src/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ def validated_token(
required_scopes: security.SecurityScopes,
) -> Dict:
# Parse & validate token
logger.info(f"\nToken String {token_str}")
try:
token = jwt.decode(
token_str,
jwks_client.get_signing_key_from_jwt(token_str).key,
algorithms=["RS256"],
)
logger.info(f"\nDecoded token {token}")
except jwt.exceptions.InvalidTokenError as e:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand All @@ -53,8 +55,10 @@ def validated_token(
return token


def get_username(token: Annotated[Dict[Any, Any], Depends(validated_token)]):
return token["username"]
def get_username(token: Annotated[Dict[Any, Any], Depends(validated_token)]) -> str:
logger.info(f"\nToken {token}")
result = token["username"] if "username" in token else token.get("sub", None)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm mixed about having a default when we might want to fail but because this depends on validation I think it is OK? We can decide after it works or not

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dbb62fb Perhaps we can raise the error instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f640fdf change reverted since we don't want to be blocked and could forget about this change later on

return result


def _get_secret_hash(username: str, client_id: str, client_secret: str) -> str:
Expand Down
2 changes: 2 additions & 0 deletions ingest_api/runtime/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ async def enqueue_ingestion(
"""
Queues a STAC item for ingestion.
"""

logger.info(f"\nUsername {username}")
return schemas.Ingestion(
id=item.id,
created_by=username,
Expand Down