Skip to content

Commit 76e5cb8

Browse files
committed
Refactor for legibility
1 parent b89cfce commit 76e5cb8

File tree

1 file changed

+22
-29
lines changed

1 file changed

+22
-29
lines changed

src/stac_auth_proxy/app.py

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ def create_app(settings: Optional[Settings] = None) -> FastAPI:
2323
settings = settings or Settings()
2424

2525
app = FastAPI(
26-
openapi_url=None,
26+
openapi_url=None, # Disable OpenAPI schema endpoint, we want to serve upstream's schema
2727
)
2828
app.add_middleware(AddProcessTimeHeaderMiddleware)
2929

30-
auth_scheme = OpenIdConnectAuth(
31-
openid_configuration_url=settings.oidc_discovery_url
32-
)
33-
3430
if settings.debug:
3531
app.add_api_route(
3632
"/_debug",
3733
lambda: {"settings": settings},
3834
methods=["GET"],
3935
)
4036

37+
# Tooling
38+
auth_scheme = OpenIdConnectAuth(
39+
openid_configuration_url=settings.oidc_discovery_url
40+
)
4141
proxy_handler = ReverseProxyHandler(
4242
upstream=str(settings.upstream_url),
4343
auth_dependency=auth_scheme.maybe_validated_user,
@@ -48,31 +48,24 @@ def create_app(settings: Optional[Settings] = None) -> FastAPI:
4848
proxy=proxy_handler,
4949
oidc_config_url=str(settings.oidc_discovery_url),
5050
)
51-
# Endpoints that are explicitely marked private
52-
for path, methods in settings.private_endpoints.items():
53-
app.add_api_route(
54-
path,
55-
(
56-
proxy_handler.stream
57-
if path != settings.openapi_spec_endpoint
58-
else openapi_handler
59-
),
60-
methods=methods,
61-
dependencies=[Security(auth_scheme.validated_user)],
62-
)
6351

64-
# Endpoints that are explicitely marked as public
65-
for path, methods in settings.public_endpoints.items():
66-
app.add_api_route(
67-
path,
68-
(
69-
proxy_handler.stream
70-
if path != settings.openapi_spec_endpoint
71-
else openapi_handler
72-
),
73-
methods=methods,
74-
dependencies=[],
75-
)
52+
# Configure security dependency for explicitely specified endpoints
53+
for path_methods, dependencies in [
54+
(settings.private_endpoints, [Security(auth_scheme.validated_user)]),
55+
(settings.public_endpoints, []),
56+
]:
57+
for path, methods in path_methods.items():
58+
endpoint = (
59+
openapi_handler
60+
if path == settings.openapi_spec_endpoint
61+
else proxy_handler.stream
62+
)
63+
app.add_api_route(
64+
path,
65+
endpoint=endpoint,
66+
methods=methods,
67+
dependencies=dependencies,
68+
)
7669

7770
# Catchall for remainder of the endpoints
7871
app.add_api_route(

0 commit comments

Comments
 (0)