-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FIX] Ensure token forwarded to n-APIs does not include an extra sche…
…me string (#134) * strip auth scheme from token before forwarding * add positive test for verify_token * rename func and variable for clarity * rename test fixtures
- Loading branch information
Showing
5 changed files
with
58 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import pytest | ||
from fastapi import HTTPException | ||
from google.oauth2 import id_token | ||
|
||
from app.api.security import verify_token | ||
from app.api.security import verify_and_extract_token | ||
|
||
|
||
def test_missing_client_id_raises_error_when_auth_enabled( | ||
|
@@ -40,7 +41,7 @@ def test_missing_client_id_ignored_when_auth_disabled(monkeypatch, test_app): | |
def test_invalid_token_raises_error(invalid_token): | ||
"""Test that an invalid token raises an error from the verification process.""" | ||
with pytest.raises(HTTPException) as exc_info: | ||
verify_token(invalid_token) | ||
verify_and_extract_token(invalid_token) | ||
|
||
assert exc_info.value.status_code == 401 | ||
assert "Invalid token" in exc_info.value.detail | ||
|
@@ -52,7 +53,7 @@ def test_invalid_token_raises_error(invalid_token): | |
) | ||
def test_query_with_malformed_auth_header_fails( | ||
test_app, | ||
set_mock_verify_token, | ||
set_mock_verify_and_extract_token, | ||
enable_auth, | ||
invalid_auth_header, | ||
monkeypatch, | ||
|
@@ -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_and_extract_token(mock_valid_token) == "foo" |