88import logging
99from typing import Optional
1010
11- from fastapi import FastAPI , Security
11+ from fastapi import FastAPI
1212
1313from .auth import OpenIdConnectAuth
1414from .config import Settings
15- from .handlers import ReverseProxyHandler , build_openapi_spec_handler
16- from .middleware import AddProcessTimeHeaderMiddleware
15+ from .handlers import ReverseProxyHandler
16+ from .middleware import (
17+ AddProcessTimeHeaderMiddleware ,
18+ EnforceAuthMiddleware ,
19+ OpenApiMiddleware ,
20+ )
1721
1822logger = logging .getLogger (__name__ )
1923
@@ -25,7 +29,17 @@ def create_app(settings: Optional[Settings] = None) -> FastAPI:
2529 app = FastAPI (
2630 openapi_url = None , # Disable OpenAPI schema endpoint, we want to serve upstream's schema
2731 )
32+
2833 app .add_middleware (AddProcessTimeHeaderMiddleware )
34+ if settings .openapi_spec_endpoint :
35+ app .add_middleware (
36+ OpenApiMiddleware ,
37+ openapi_spec_path = settings .openapi_spec_endpoint ,
38+ oidc_config_url = str (settings .oidc_discovery_url ),
39+ private_endpoints = settings .private_endpoints ,
40+ default_public = settings .default_public ,
41+ )
42+ app .add_middleware (EnforceAuthMiddleware )
2943
3044 if settings .debug :
3145 app .add_api_route (
@@ -44,37 +58,33 @@ def create_app(settings: Optional[Settings] = None) -> FastAPI:
4458 collections_filter = settings .collections_filter ,
4559 items_filter = settings .items_filter ,
4660 )
47- openapi_handler = build_openapi_spec_handler (
48- proxy = proxy_handler ,
49- oidc_config_url = str (settings .oidc_discovery_url ),
50- )
5161
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- )
62+ # # Configure security dependency for explicitely specified endpoints
63+ # for path_methods, dependencies in [
64+ # (settings.private_endpoints, [Security(auth_scheme.validated_user)]),
65+ # (settings.public_endpoints, []),
66+ # ]:
67+ # for path, methods in path_methods.items():
68+ # endpoint = (
69+ # openapi_handler
70+ # if path == settings.openapi_spec_endpoint
71+ # else proxy_handler.stream
72+ # )
73+ # app.add_api_route(
74+ # path,
75+ # endpoint=endpoint,
76+ # methods=methods,
77+ # dependencies=dependencies,
78+ # )
6979
7080 # Catchall for remainder of the endpoints
7181 app .add_api_route (
7282 "/{path:path}" ,
7383 proxy_handler .stream ,
7484 methods = ["GET" , "POST" , "PUT" , "PATCH" , "DELETE" ],
75- dependencies = (
76- [] if settings .default_public else [Security (auth_scheme .validated_user )]
77- ),
85+ # dependencies=(
86+ # [] if settings.default_public else [Security(auth_scheme.validated_user)]
87+ # ),
7888 )
7989
8090 return app
0 commit comments