Skip to content

Commit 1b2dd81

Browse files
committed
Better support internal oidc config address
1 parent 1b4b83b commit 1b2dd81

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/stac_auth_proxy/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def create_app(settings: Optional[Settings] = None) -> FastAPI:
6969
private_endpoints=settings.private_endpoints,
7070
default_public=settings.default_public,
7171
oidc_config_url=settings.oidc_discovery_url,
72+
oidc_config_internal_url=settings.oidc_discovery_internal_url,
7273
)
7374

7475
return app

src/stac_auth_proxy/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Settings(BaseSettings):
3434
# External URLs
3535
upstream_url: HttpUrl
3636
oidc_discovery_url: HttpUrl
37+
oidc_discovery_internal_url: Optional[HttpUrl] = None
3738

3839
# Endpoints
3940
healthz_prefix: str = Field(pattern=_PREFIX_PATTERN, default="/healthz")

src/stac_auth_proxy/middleware/EnforceAuthMiddleware.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"""Middleware to enforce authentication."""
22

3-
import json
43
import logging
5-
import urllib.request
64
from dataclasses import dataclass, field
75
from typing import Annotated, Optional, Sequence
86

7+
import httpx
98
import jwt
109
from fastapi import HTTPException, Request, Security, status
1110
from pydantic import HttpUrl
@@ -28,7 +27,7 @@ class EnforceAuthMiddleware:
2827
default_public: bool
2928

3029
oidc_config_url: HttpUrl
31-
openid_configuration_internal_url: Optional[HttpUrl] = None
30+
oidc_config_internal_url: Optional[HttpUrl] = None
3231
allowed_jwt_audiences: Optional[Sequence[str]] = None
3332

3433
state_key: str = "user"
@@ -39,18 +38,24 @@ class EnforceAuthMiddleware:
3938
def __post_init__(self):
4039
"""Initialize the OIDC authentication class."""
4140
logger.debug("Requesting OIDC config")
42-
origin_url = str(self.openid_configuration_internal_url or self.oidc_config_url)
43-
with urllib.request.urlopen(origin_url) as response:
44-
if response.status != 200:
45-
logger.error(
46-
"Received a non-200 response when fetching OIDC config: %s",
47-
response.text,
48-
)
49-
raise OidcFetchError(
50-
f"Request for OIDC config failed with status {response.status}"
51-
)
52-
oidc_config = json.load(response)
41+
origin_url = str(self.oidc_config_internal_url or self.oidc_config_url)
42+
43+
try:
44+
response = httpx.get(origin_url)
45+
response.raise_for_status()
46+
oidc_config = response.json()
5347
self.jwks_client = jwt.PyJWKClient(oidc_config["jwks_uri"])
48+
except httpx.HTTPStatusError as e:
49+
logger.error(
50+
"Received a non-200 response when fetching OIDC config: %s",
51+
e.response.text,
52+
)
53+
raise OidcFetchError(
54+
f"Request for OIDC config failed with status {e.response.status_code}"
55+
)
56+
except httpx.RequestError as e:
57+
logger.error("Error fetching OIDC config from %s: %s", origin_url, str(e))
58+
raise OidcFetchError(f"Request for OIDC config failed: {str(e)}")
5459

5560
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
5661
"""Enforce authentication."""

0 commit comments

Comments
 (0)