stac_auth_proxy.utils.requests
¶
Utility functions for working with HTTP requests.
MatchResult
dataclass
¶
Result of a match between a path and method and a set of endpoints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uses_auth
|
bool
|
|
required |
required_scopes
|
Sequence[str]
|
Built-in mutable sequence. If no argument is given, the constructor creates a new empty list. The argument must be an iterable if specified. |
<dynamic>
|
Source code in src/stac_auth_proxy/utils/requests.py
156 157 158 159 160 161 | |
build_server_timing_header(current_value: Optional[str] = None, *, name: str, desc: str, dur: float)
¶
Append a timing header to headers.
Source code in src/stac_auth_proxy/utils/requests.py
164 165 166 167 168 169 170 171 | |
checked_path(scope: dict) -> str
¶
Return the path that auth, filter and transaction checks matched on.
Recorded once rather than re-derived from root_path, which Starlette also extends for Mounts: a proxy mounted under "/proxy" must forward "/proxy/x", the path that was checked, not "/x".
Source code in src/stac_auth_proxy/utils/requests.py
70 71 72 73 74 75 76 77 78 | |
dict_to_bytes(d: dict) -> bytes
¶
Convert a dictionary to a body.
Source code in src/stac_auth_proxy/utils/requests.py
81 82 83 | |
extract_variables(url: str) -> dict
¶
Extract variables from a URL path. Being that we use a catch-all endpoint for the proxy, we can't rely on the path parameters that FastAPI provides.
Source code in src/stac_auth_proxy/utils/requests.py
17 18 19 20 21 22 23 24 25 26 | |
find_match(path: str, method: str, private_endpoints: EndpointMethods, public_endpoints: EndpointMethods, default_public: bool, items_filter_path: Optional[str] = None, collections_filter_path: Optional[str] = None) -> MatchResult
¶
Check if the given path and method match any of the regex patterns and methods in the endpoints.
Some upstreams (e.g. stac-server/Express) route case-insensitively, so private endpoints and filter paths match case-insensitively. Public endpoints stay case-sensitive so that a case variation can only ever require more auth.
Source code in src/stac_auth_proxy/utils/requests.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
get_base_url(request: Request) -> str
¶
Get the request's base URL, accounting for forwarded headers from load balancers/proxies.
This function handles both the standard Forwarded header (RFC 7239) and legacy X-Forwarded-* headers to reconstruct the original client URL when the service is deployed behind load balancers or reverse proxies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The Starlette request object |
required |
Returns:
| Type | Description |
|---|---|
str
|
The reconstructed client base URL |
Example
With Forwarded header¶
request.headers = {"Forwarded": "for=192.0.2.43; proto=https; host=api.example.com"} get_base_url(request) "api.example.com/"
With X-Forwarded-* headers¶
request.headers = {"X-Forwarded-Host": "api.example.com", "X-Forwarded-Proto": "https"} get_base_url(request) "api.example.com/"
Source code in src/stac_auth_proxy/utils/requests.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
is_under_prefix(path: str, prefix: str) -> bool
¶
Whether path is prefix or below it, at a segment boundary ("/stacx" isn't under "/stac").
Source code in src/stac_auth_proxy/utils/requests.py
52 53 54 55 | |
match_path(pattern: str, path: str, widen: bool = True) -> Optional[re.Match[str]]
¶
Match a path-based access rule against the path an upstream may route as.
Some upstreams route case-insensitively (stac-server/Express) or ignore a
trailing slash (Express, pygeoapi). So a rule matches if it matches the path as
given or, with widen, ignoring case or without one trailing slash. Each
variation only adds matches: e.g. "^/admin/" still matches "/admin/", and
"(?!public-)" still matches "PUBLIC-x" as written. Rules that grant access
(public endpoints) must not be widened.
Source code in src/stac_auth_proxy/utils/requests.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | |
parse_forwarded_header(forwarded_header: str) -> Dict[str, str]
¶
Parse the Forwarded header according to RFC 7239.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
forwarded_header
|
str
|
The Forwarded header value |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, str]
|
Dictionary containing parsed forwarded information (proto, host, for, by, etc.) |
Example
parse_forwarded_header("for=192.0.2.43; by=203.0.113.60; proto=https; host=api.example.com")
Source code in src/stac_auth_proxy/utils/requests.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
strip_prefix(path: str, prefix: str) -> str
¶
Remove a root path prefix from path, at a segment boundary, if path is under it.
Source code in src/stac_auth_proxy/utils/requests.py
58 59 60 61 62 63 | |