Skip to content

Commit 2e2a0d3

Browse files
git-hyagiclaude
andauthored
Return Basic realm from CONTENT_ORIGIN in WWW-Authenticate header (#1640)
The OrgGroupAssignmentMixin.authenticate_header() was returning a static "Bearer" value. When DRF denied an unauthenticated request to a content-guarded PyPI view, the 401 response carried WWW-Authenticate: Bearer instead of a Basic challenge that prompts browsers for credentials. Derive the realm from settings.CONTENT_ORIGIN so the header matches the environment (e.g. packages.redhat.com vs packages.stage.redhat.com). Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8d16109 commit 2e2a0d3

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

‎pulp_service/pulp_service/app/authentication.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
import logging
33
from base64 import b64decode
44
from binascii import Error as Base64DecodeError
5+
from urllib.parse import urlparse
56

7+
from django.conf import settings
68
from django.contrib.auth import get_user_model
79

810
from pulpcore.app.authentication import JSONHeaderRemoteAuthentication
@@ -78,7 +80,8 @@ def authenticate(self, request):
7880
return result
7981

8082
def authenticate_header(self, request):
81-
return "Bearer"
83+
hostname = urlparse(settings.CONTENT_ORIGIN).hostname or "packages.redhat.com"
84+
return f'Basic realm="{hostname}"'
8285

8386

8487
class RHServiceAccountCertAuthentication(OrgGroupAssignmentMixin, JSONHeaderRemoteAuthentication):

‎pulp_service/pulp_service/tests/functional/test_content_guard_permission.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def test_unauthenticated_denied_on_guarded_pypi_simple_api(configure_guarded_pyp
194194
response = requests.get(pypi_url, timeout=30)
195195

196196
assert response.status_code == 401
197-
assert response.headers["WWW-Authenticate"] == "Bearer"
197+
assert response.headers["WWW-Authenticate"].startswith("Basic realm=")
198198

199199

200200
def test_write_operations_unaffected_by_content_guard(configure_guarded_pypi_distribution):

‎pulp_service/pulp_service/tests/unit/test_authentication_error_content_guard.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,16 @@ async def set(self, *args, **kwargs):
9191

9292

9393
class TestHandlerAuthentication:
94-
def test_permit_maps_authentication_error_to_401(self):
94+
def test_permit_maps_authentication_error_to_401(self, settings):
9595
detail_guard = _detail_guard("identity", AuthenticationError("Authentication required."))
9696
request = MagicMock(path="/lightwell/osv/record")
97+
settings.CONTENT_ORIGIN = "https://packages.stage.redhat.com"
9798

9899
with pytest.raises(HTTPUnauthorized) as exc_info:
99100
Handler._permit(request, _distribution(detail_guard))
100101

101102
assert exc_info.value.status == 401
102-
assert exc_info.value.headers["WWW-Authenticate"] == 'Basic realm="packages.redhat.com"'
103+
assert exc_info.value.headers["WWW-Authenticate"] == 'Basic realm="packages.stage.redhat.com"'
103104

104105
def test_permit_keeps_permission_error_as_403(self):
105106
detail_guard = _detail_guard("feature", PermissionError("Access denied."))

0 commit comments

Comments
 (0)