How to set the data HTTP request headers #403
-
Hi everyone, I've been trying to figure out how to set a specific HTTP header when TiTiler queries a STAC API. In my use case, I would like to set it to whatever the header is when TiTiler is called. I have been able to read the # Add private COG endpoints requiring token validation
from fastapi import Security, Query
from fastapi.security.api_key import APIKeyHeader
from titiler.application.main import app
from titiler.core.factory import TilerFactory
api_token_header = APIKeyHeader(name="Authorization", auto_error=False)
# Here I can access the token and modify the path, but not the headers :(
def DatasetPathParams(
url: str = Query(..., description="Dataset URL"),
access_token: str = Security(api_token_header),
) -> str:
"""Create dataset path from args"""
print("URL")
print(url)
print("TOKEN")
print(access_token)
return url
# Custom router with token dependency
tiler = TilerFactory(path_dependency=DatasetPathParams)
app.include_router(tiler.router, prefix="/test", tags=["Test"]) In this Is there a way to achieve what I would like to do? Thanks in advance for your help |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 8 replies
-
👋 @pepjo To modify the headers use for the STAC request you'll need to set Sadly this has to be pass at but we don't have a @self.router.get(
"/info",
response_model=Info,
response_model_exclude_none=True,
response_class=JSONResponse,
responses={200: {"description": "Return dataset's basic info."}},
)
def info(
src_path=Depends(self.path_dependency),
access_token: str = Security(api_token_header),
kwargs: Dict = Depends(self.additional_dependency),
):
"""Return dataset's basic info."""
with self.reader(
src_path,
fetch_options={
"headers": {
"Authorization": access_token,
}
},
**self.reader_options,
) as src_dst:
return src_dst.info(**kwargs) An other solution would maybe to get rid of the static |
Beta Was this translation helpful? Give feedback.
-
I've proposed 2 different solutions, I'll mark this as answered. @pepjo feel free to comment if it's not what you expected 🙏 |
Beta Was this translation helpful? Give feedback.
-
I'll leave here my approach to forward authorization headers from the frontend to a STAC API, just in case anybody needs to handle that use case: from typing import Any, Dict, Optional
import attr
from fastapi import Request
from titiler.core.dependencies import DefaultDependency
@attr.dataclass
class HeaderDependency(DefaultDependency):
fetch_options: Optional[Dict[str, Any]] = None
@classmethod
async def from_request(cls, request: Request):
"""Instantiate object from a FastAPI Request."""
headers = {
"headers": {
"authorization": request.headers.get("authorization", ""),
}
}
return cls(fetch_options=headers)
async def get_header_dependency(request: Request) -> HeaderDependency:
"""FastAPI Dependency that creates a `HeaderDependency` object from an incoming request."""
return await HeaderDependency.from_request(request)
######
# Add STAC extension with custom dependency
stac = MultiBaseTilerFactory(
reader=STACReader,
router_prefix="/stac",
reader_dependency=get_header_dependency, # Passes incoming headers to STAC API
) |
Beta Was this translation helpful? Give feedback.
👋 @pepjo
To modify the headers use for the STAC request you'll need to set
fetch_options={"headers": {YOUR HEADERS VALUE}}
rio-tilerSadly this has to be pass at
reader
inittitiler/src/titiler/core/titiler/core/factory.py
Line 212 in ac184ae
but we don't have a
dynamic
way of populating the reader options 🙁. I fear you'll have to re-write the routes where you want to use this.