Skip to content

Commit

Permalink
Simplify the logic to add the security stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
francbartoli committed Apr 7, 2024
1 parent bfae815 commit 1a73bc1
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions app/middleware/pygeoapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,12 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):
return await self.app(scope, receive, send)
pygeoapi_path = scope["path"]
pygeoapi_query_params = scope["query_string"].decode()
if (
pygeoapi_path not in routes_with_openapi
or pygeoapi_query_params == "f=html"
):
if pygeoapi_path not in routes_with_openapi:
return await self.app(scope, receive, send)
else:
if pygeoapi_query_params in queryparams_with_openapi:
openapi_responder = OpenAPIResponder(self.app, self.security_schemes)
await openapi_responder(scope, receive, send)
return
openapi_responder = OpenAPIResponder(self.app, self.security_schemes)
await openapi_responder(scope, receive, send)
return
await self.app(scope, receive, send)


Expand Down Expand Up @@ -79,21 +75,21 @@ async def send_with_security(self, message: Message) -> None: # noqa: C901
self.initial_message = message
headers = Headers(raw=self.initial_message["headers"])
headers_dict = dict(headers.items())
logger.debug(f"pygeoapi headers: {headers}")
content_type = str(headers_dict.get("content-type"))
if "application/vnd.oai.openapi+json" not in content_type:
logger.error(f"Incosistent content-type: {content_type}")
raise ValueError(f"Wrong content-type: {content_type} for openapi path")
logger.info(f"Content-Type: {content_type}")
self.headers.update(headers_dict)
if message_type == "http.response.body":
initial_body = message.get("body", b"").decode()
openapi_body = augment_security(
doc=initial_body, security_schemes=self.security_schemes
)
binary_body = openapi_body.model_dump_json(
by_alias=True, exclude_none=True, indent=2
).encode()
headers = MutableHeaders(raw=self.initial_message["headers"])
headers["Content-Length"] = str(len(binary_body))
message["body"] = binary_body
if not "<!-- HTML" in initial_body:
openapi_body = augment_security(
doc=initial_body, security_schemes=self.security_schemes
)
binary_body = openapi_body.model_dump_json(
by_alias=True, exclude_none=True, indent=2
).encode()
headers = MutableHeaders(raw=self.initial_message["headers"])
headers["Content-Length"] = str(len(binary_body))
message["body"] = binary_body
await self.send(self.initial_message)
await self.send(message)

0 comments on commit 1a73bc1

Please sign in to comment.