Skip to content

Commit 659c6e9

Browse files
committed
fix(jwk): missing auth header correctly returns 401 error
1 parent 55d38af commit 659c6e9

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/authentication/jwk_token.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from cachetools import TTLCache
1717
from fastapi import HTTPException, Request
1818

19-
from authentication.interface import NO_AUTH_TUPLE, AuthInterface, AuthTuple
19+
from authentication.interface import AuthInterface, AuthTuple
2020
from authentication.utils import extract_user_token
2121
from constants import (
2222
DEFAULT_VIRTUAL_PATH,
@@ -180,7 +180,8 @@ async def __call__(self, request: Request) -> AuthTuple:
180180
Authorization header is present.
181181
"""
182182
if not request.headers.get("Authorization"):
183-
return NO_AUTH_TUPLE
183+
response = UnauthorizedResponse(cause="No Authorization header found")
184+
raise HTTPException(**response.model_dump())
184185

185186
user_token = extract_user_token(request.headers)
186187

tests/e2e/features/rbac.feature

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ Feature: Role-Based Access Control (RBAC)
1212
# Authentication - Token Validation
1313
# ============================================
1414

15-
#https://issues.redhat.com/browse/LCORE-1210
16-
@skip
1715
Scenario: Request without token returns 401
1816
Given The system is in default state
1917
And I remove the auth header
2018
When I access REST API endpoint "models" using HTTP GET method
2119
Then The status code of the response is 401
22-
And The body of the response contains Missing or invalid credentials
20+
And The body of the response is the following
21+
"""
22+
{
23+
"detail": {
24+
"response": "Missing or invalid credentials provided by client",
25+
"cause": "No Authorization header found"
26+
}
27+
}
28+
"""
2329

2430
Scenario: Request with malformed Authorization header returns 401
2531
Given The system is in default state

tests/unit/authentication/test_jwk_token.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
from authlib.jose import JsonWebKey, JsonWebToken
1414

1515
from authentication.jwk_token import JwkTokenAuthDependency, _jwk_cache
16-
from constants import DEFAULT_USER_NAME, DEFAULT_USER_UID, NO_USER_TOKEN
1716
from models.config import JwkConfiguration, JwtConfiguration
1817

1918
TEST_USER_ID = "test-user-123"
@@ -435,19 +434,16 @@ async def test_no_auth_header(
435434
mocked_signing_keys_server: Any,
436435
no_token_request: Request,
437436
) -> None:
438-
"""Test with no Authorization header."""
437+
"""Test with no Authorization header returns 401 Unauthorized."""
439438
_ = mocked_signing_keys_server
440439

441440
dependency = JwkTokenAuthDependency(default_jwk_configuration)
442441

443-
user_id, username, skip_userid_check, token_claims = await dependency(
444-
no_token_request
445-
)
442+
with pytest.raises(HTTPException) as exc_info:
443+
await dependency(no_token_request)
446444

447-
assert user_id == DEFAULT_USER_UID
448-
assert username == DEFAULT_USER_NAME
449-
assert skip_userid_check is True
450-
assert token_claims == NO_USER_TOKEN
445+
assert exc_info.value.status_code == 401
446+
assert "No Authorization header found" in str(exc_info.value.detail)
451447

452448

453449
async def test_no_bearer(

0 commit comments

Comments
 (0)