Skip to content

Commit

Permalink
fix: update get_username function (#365)
Browse files Browse the repository at this point in the history
### Issue

NASA-IMPACT/veda-data-airflow#134
#347

### What?

- Update get_username to fall back on `sub` if `username` doesn't exist
in token
- Added some more logging statements to make debugging easier in the
future

### Why?

- This fix is to enable a successful workflows API run since the
workflow API passes a token to backend API and it's currently erroring
- Also, `sub` is a more definitive identifier because it represents a
unique identifier compared to `username`

### Testing?

- Relevant testing details
  • Loading branch information
botanical authored Apr 29, 2024
2 parents eb6a922 + f640fdf commit 28afca4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
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)
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

0 comments on commit 28afca4

Please sign in to comment.