33import logging
44from contextlib import asynccontextmanager
55
6+ from eoapi .auth_utils import OpenIdConnectAuth , OpenIdConnectSettings
67from fastapi import FastAPI
78from fastapi .responses import ORJSONResponse
89from stac_fastapi .api .app import StacApi
3435from starlette .templating import Jinja2Templates
3536from starlette_cramjam .middleware import CompressionMiddleware
3637
37- from . import config , extension , logs
38+ from .config import ApiSettings
39+ from .extension import TiTilerExtension
40+ from .logs import init_logging
3841
3942try :
4043 from importlib .resources import files as resources_files # type: ignore
4548
4649templates = Jinja2Templates (directory = str (resources_files (__package__ ) / "templates" )) # type: ignore
4750
48- api_settings = config .ApiSettings ()
51+ api_settings = ApiSettings ()
52+ auth_settings = OpenIdConnectSettings ()
4953settings = Settings (enable_response_models = True )
5054
5155# Logs
52- logs . init_logging (debug = api_settings .debug )
56+ init_logging (debug = api_settings .debug )
5357logger = logging .getLogger (__name__ )
5458
5559# Extensions
6670 "filter" : FilterExtension (client = FiltersClient ()),
6771 "bulk_transactions" : BulkTransactionExtension (client = BulkTransactionsClient ()),
6872 "titiler" : (
69- extension . TiTilerExtension (titiler_endpoint = api_settings .titiler_endpoint )
73+ TiTilerExtension (titiler_endpoint = api_settings .titiler_endpoint )
7074 if api_settings .titiler_endpoint
7175 else None
7276 ),
@@ -129,6 +133,10 @@ async def lifespan(app: FastAPI):
129133 openapi_url = "/api" ,
130134 docs_url = "/api.html" ,
131135 redoc_url = None ,
136+ swagger_ui_init_oauth = {
137+ "clientId" : auth_settings .client_id ,
138+ "usePkceWithAuthorizationCodeGrant" : auth_settings .use_pkce ,
139+ },
132140 ),
133141 title = api_settings .name ,
134142 description = api_settings .name ,
@@ -155,3 +163,15 @@ async def viewer_page(request: Request):
155163 },
156164 media_type = "text/html" ,
157165 )
166+
167+
168+ if auth_settings .openid_configuration_url :
169+ oidc_auth = OpenIdConnectAuth .from_settings (auth_settings )
170+
171+ restricted_prefixes = ["/collections" , "/search" ]
172+ for route in app .routes :
173+ if any (
174+ route .path .startswith (f"{ app .root_path } { prefix } " )
175+ for prefix in restricted_prefixes
176+ ):
177+ oidc_auth .apply_auth_dependencies (route , required_token_scopes = [])
0 commit comments