1212from src .extension import TiTilerExtension
1313
1414from fastapi import APIRouter , FastAPI
15- from fastapi .params import Depends
1615from fastapi .responses import ORJSONResponse
1716from stac_fastapi .pgstac .db import close_db_connection , connect_to_db
1817from starlette .middleware import Middleware
2524from .api import VedaStacApi
2625from .core import VedaCrudClient
2726from .monitoring import LoggerRouteHandler , logger , metrics , tracer
28- from .routes import add_route_dependencies
2927from .validation import ValidationMiddleware
3028
29+ from eoapi .auth_utils import OpenIdConnectAuth , OpenIdConnectSettings
30+
3131try :
3232 from importlib .resources import files as resources_files # type: ignore
3333except ImportError :
3838templates = Jinja2Templates (directory = str (resources_files (__package__ ) / "templates" )) # type: ignore
3939
4040tiles_settings = TilesApiSettings ()
41+ auth_settings = OpenIdConnectSettings (_env_prefix = "VEDA_STAC_" )
4142
4243
4344@asynccontextmanager
@@ -56,11 +57,12 @@ async def lifespan(app: FastAPI):
5657 root_path = api_settings .root_path ,
5758 swagger_ui_init_oauth = (
5859 {
59- "appName" : "Cognito " ,
60- "clientId" : api_settings .client_id ,
60+ "appName" : "STAC API " ,
61+ "clientId" : auth_settings .client_id ,
6162 "usePkceWithAuthorizationCodeGrant" : True ,
63+ "scopes" : "openid stac:item:create stac:item:update stac:item:delete stac:collection:create stac:collection:update stac:collection:delete" ,
6264 }
63- if api_settings .client_id
65+ if auth_settings .client_id
6466 else {}
6567 ),
6668 lifespan = lifespan ,
@@ -88,41 +90,35 @@ async def lifespan(app: FastAPI):
8890 allow_headers = ["*" ],
8991 )
9092
91- if api_settings .enable_transactions :
92- from veda_auth import VedaAuth
93-
94- auth = VedaAuth (api_settings )
95- # Require auth for all endpoints that create, modify or delete data.
96- add_route_dependencies (
97- app .router .routes ,
98- [
99- {"path" : "/collections" , "method" : "POST" , "type" : "http" },
100- {"path" : "/collections/{collectionId}" , "method" : "PUT" , "type" : "http" },
101- {"path" : "/collections/{collectionId}" , "method" : "DELETE" , "type" : "http" },
102- {
103- "path" : "/collections/{collectionId}/items" ,
104- "method" : "POST" ,
105- "type" : "http" ,
106- },
107- {
108- "path" : "/collections/{collectionId}/items/{itemId}" ,
109- "method" : "PUT" ,
110- "type" : "http" ,
111- },
112- {
113- "path" : "/collections/{collectionId}/items/{itemId}" ,
114- "method" : "DELETE" ,
115- "type" : "http" ,
116- },
117- {
118- "path" : "/collections/{collectionId}/bulk_items" ,
119- "method" : "POST" ,
120- "type" : "http" ,
121- },
122- ],
123- [Depends (auth .validated_token )],
93+ if api_settings .enable_transactions and auth_settings .client_id :
94+ oidc_auth = OpenIdConnectAuth (
95+ openid_configuration_url = auth_settings .openid_configuration_url ,
96+ allowed_jwt_audiences = "account" ,
12497 )
12598
99+ restricted_prefixes_methods = {
100+ "/collections" : [("POST" , "stac:collection:create" )],
101+ "/collections/{collection_id}" : [
102+ ("PUT" , "stac:collection:update" ),
103+ ("DELETE" , "stac:collection:delete" ),
104+ ],
105+ "/collections/{collection_id}/items" : [("POST" , "stac:item:create" )],
106+ "/collections/{collection_id}/items/{item_id}" : [
107+ ("PUT" , "stac:item:update" ),
108+ ("DELETE" , "stac:item:delete" ),
109+ ],
110+ "/collections/{collection_id}/bulk_items" : [("POST" , "stac:item:create" )],
111+ }
112+
113+ for route in app .router .routes :
114+ method_scopes = restricted_prefixes_methods .get (route .path )
115+ if not method_scopes :
116+ continue
117+ for method , scope in method_scopes :
118+ if method not in route .methods :
119+ continue
120+ oidc_auth .apply_auth_dependencies (route , required_token_scopes = [scope ])
121+
126122if tiles_settings .titiler_endpoint :
127123 # Register to the TiTiler extension to the api
128124 extension = TiTilerExtension ()
0 commit comments