Skip to content

Commit d0cc8b3

Browse files
committed
Add start to filters
1 parent 3a0886a commit d0cc8b3

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

src/stac_auth_proxy/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,10 @@ class Settings(BaseSettings):
4848
public_endpoints: EndpointMethods = {"/api.html": ["GET"], "/api": ["GET"]}
4949
openapi_spec_endpoint: Optional[str] = None
5050

51+
collections_filter: Optional[ClassInput] = {
52+
"cls": "stac_auth_proxy.filters.Template",
53+
"args": ["""A_CONTAINEDBY(id, ( '{{ token.collections | join("', '") }}' ))"""],
54+
}
55+
items_filter: Optional[ClassInput] = None
56+
5157
model_config = SettingsConfigDict(env_prefix="STAC_AUTH_PROXY_")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .template import Template
2+
3+
__all__ = ["Template"]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import Any, Callable
2+
3+
from cql2 import Expr
4+
from jinja2 import Environment, BaseLoader
5+
from fastapi import Request, Security
6+
7+
from ..utils import extract_variables
8+
9+
from dataclasses import dataclass, field
10+
11+
12+
@dataclass
13+
class Template:
14+
template_str: str
15+
token_dependency: Callable[..., Any]
16+
17+
# Generated attributes
18+
env: Environment = field(init=False)
19+
20+
def __post_init__(self):
21+
self.env = Environment(loader=BaseLoader).from_string(self.template_str)
22+
self.render.__annotations__["auth_token"] = Security(self.token_dependency)
23+
24+
async def cql2(self, request: Request, auth_token=Security(...)) -> Expr:
25+
# TODO: How to handle the case where auth_token is null?
26+
context = {
27+
"req": {
28+
"path": request.url.path,
29+
"method": request.method,
30+
"query_params": dict(request.query_params),
31+
"path_params": extract_variables(request.url.path),
32+
"headers": dict(request.headers),
33+
"body": (
34+
await request.json()
35+
if request.headers.get("content-type") == "application/json"
36+
else (await request.body()).decode()
37+
),
38+
},
39+
"token": auth_token,
40+
}
41+
cql2_str = self.env.render(**context)
42+
cql2_expr = Expr(cql2_str)
43+
cql2_expr.validate()
44+
return cql2_expr

0 commit comments

Comments
 (0)