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] Ensure token forwarded to n-APIs does not include an extra scheme string #134

Merged
merged 6 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion app/api/routers/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def get_query(
status_code=status.HTTP_403_FORBIDDEN,
detail="Not authenticated",
)
verify_token(token)
token = verify_token(token)

response_dict = await crud.get(
query.min_age,
Expand Down
8 changes: 6 additions & 2 deletions app/api/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ def check_client_id():
)


def verify_token(token: str):
"""Verify the Google ID token. Raise an HTTPException if the token is invalid."""
def verify_token(token: str) -> str:
"""
Verify and return the Google ID token with the authorization scheme stripped.
Raise an HTTPException if the token is invalid.
"""
# Adapted from https://developers.google.com/identity/gsi/web/guides/verify-google-id-token#python
try:
# Extract the token from the "Bearer" scheme
Expand All @@ -35,6 +38,7 @@ def verify_token(token: str):
)
# TODO: Remove print statement or turn into logging
print("Token verified: ", id_info)
return param
except (GoogleAuthError, ValueError) as exc:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
Expand Down
33 changes: 33 additions & 0 deletions tests/test_security.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from fastapi import HTTPException
from google.oauth2 import id_token

from app.api.security import verify_token

Expand Down Expand Up @@ -69,3 +70,35 @@ def test_query_with_malformed_auth_header_fails(
)

assert response.status_code == 403


def test_verified_token_returned_without_auth_scheme(monkeypatch, enable_auth):
"""
Test that when a token is valid, verify_token correctly returns the token with the authorization scheme stripped.
"""
mock_valid_token = "Bearer foo"
mock_id_info = {
"iss": "https://accounts.google.com",
"azp": "123abc.apps.googleusercontent.com",
"aud": "123abc.apps.googleusercontent.com",
"sub": "1234567890",
"email": "[email protected]",
"email_verified": True,
"nbf": 1730476622,
"name": "Jane Doe",
"picture": "https://lh3.googleusercontent.com/a/example1234567890",
"given_name": "Jane",
"family_name": "Doe",
"iat": 1730476922,
"exp": 1730480522,
"jti": "123e4567-e89b",
}

def mock_oauth2_verify_token(param, request, client_id, **kwargs):
return mock_id_info

monkeypatch.setattr(
id_token, "verify_oauth2_token", mock_oauth2_verify_token
)

assert verify_token(mock_valid_token) == "foo"