Skip to content

Commit 9e980eb

Browse files
committed
feat: add oauth2 to swagger-ui
1 parent 6b6d7cf commit 9e980eb

File tree

3 files changed

+63
-3
lines changed

3 files changed

+63
-3
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
Tag,
8989
)
9090
from aws_lambda_powertools.event_handler.openapi.params import Dependant
91+
from aws_lambda_powertools.event_handler.openapi.swagger_ui.oauth2 import OAuth2Config
9192
from aws_lambda_powertools.event_handler.openapi.types import (
9293
TypeModelOrEnum,
9394
)
@@ -1671,6 +1672,9 @@ def enable_swagger(
16711672
swagger_base_url: Optional[str] = None,
16721673
middlewares: Optional[List[Callable[..., Response]]] = None,
16731674
compress: bool = False,
1675+
security_schemes: Optional[Dict[str, "SecurityScheme"]] = None,
1676+
security: Optional[List[Dict[str, List[str]]]] = None,
1677+
oauth2Config: Optional["OAuth2Config"] = None,
16741678
):
16751679
"""
16761680
Returns the OpenAPI schema as a JSON serializable dict
@@ -1705,6 +1709,12 @@ def enable_swagger(
17051709
List of middlewares to be used for the swagger route.
17061710
compress: bool, default = False
17071711
Whether or not to enable gzip compression swagger route.
1712+
security_schemes: Dict[str, "SecurityScheme"], optional
1713+
A declaration of the security schemes available to be used in the specification.
1714+
security: List[Dict[str, List[str]]], optional
1715+
A declaration of which security mechanisms are applied globally across the API.
1716+
oauth2Config: OAuth2Config, optional
1717+
The OAuth2 configuration for the Swagger UI.
17081718
"""
17091719
from aws_lambda_powertools.event_handler.openapi.compat import model_json
17101720
from aws_lambda_powertools.event_handler.openapi.models import Server
@@ -1736,6 +1746,8 @@ def swagger_handler():
17361746
terms_of_service=terms_of_service,
17371747
contact=contact,
17381748
license_info=license_info,
1749+
security_schemes=security_schemes,
1750+
security=security,
17391751
)
17401752

17411753
# The .replace('</', '<\\/') part is necessary to prevent a potential issue where the JSON string contains
@@ -1765,6 +1777,7 @@ def swagger_handler():
17651777
swagger_js,
17661778
swagger_css,
17671779
swagger_base_url,
1780+
oauth2Config,
17681781
)
17691782

17701783
return Response(

aws_lambda_powertools/event_handler/openapi/swagger_ui/html.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
def generate_swagger_html(spec: str, path: str, swagger_js: str, swagger_css: str, swagger_base_url: str) -> str:
1+
from typing import Optional
2+
3+
from aws_lambda_powertools.event_handler.openapi.swagger_ui.oauth2 import OAuth2Config
4+
5+
6+
def generate_swagger_html(
7+
spec: str,
8+
path: str,
9+
swagger_js: str,
10+
swagger_css: str,
11+
swagger_base_url: str,
12+
oauth2: Optional[OAuth2Config],
13+
) -> str:
214
"""
315
Generate Swagger UI HTML page
416
@@ -8,10 +20,14 @@ def generate_swagger_html(spec: str, path: str, swagger_js: str, swagger_css: st
820
The OpenAPI spec
921
path: str
1022
The path to the Swagger documentation
11-
js_url: str
23+
swagger_js: str
1224
The URL to the Swagger UI JavaScript file
13-
css_url: str
25+
swagger_css: str
1426
The URL to the Swagger UI CSS file
27+
swagger_base_url: str
28+
The base URL for Swagger UI
29+
oauth2: OAuth2Config, optional
30+
The OAuth2 configuration.
1531
"""
1632

1733
# If Swagger base URL is present, generate HTML content with linked CSS and JavaScript files
@@ -23,6 +39,9 @@ def generate_swagger_html(spec: str, path: str, swagger_js: str, swagger_css: st
2339
swagger_css_content = f"<style>{swagger_css}</style>"
2440
swagger_js_content = f"<script>{swagger_js}</script>"
2541

42+
# Prepare oauth2 config
43+
oauth2_content = f"ui.initOAuth({oauth2.json(exclude_none=True, exclude_unset=True)});" if oauth2 else ""
44+
2645
return f"""
2746
<!DOCTYPE html>
2847
<html>
@@ -65,6 +84,7 @@ def generate_swagger_html(spec: str, path: str, swagger_js: str, swagger_css: st
6584
6685
var ui = SwaggerUIBundle(swaggerUIOptions)
6786
ui.specActions.updateUrl('{path}?format=json');
87+
{oauth2_content}
6888
</script>
6989
</html>
7090
""".strip()
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from typing import Dict, Sequence
2+
3+
from pydantic import BaseModel, Field
4+
5+
from aws_lambda_powertools.event_handler.openapi.pydantic_loader import PYDANTIC_V2
6+
7+
8+
# Based on https://swagger.io/docs/open-source-tools/swagger-ui/usage/oauth2/
9+
class OAuth2Config(BaseModel):
10+
clientId: str = Field(alias="client_id")
11+
realm: str
12+
appName: str = Field(alias="app_name")
13+
scopes: Sequence[str] = Field(default=[])
14+
additionalQueryStringParams: Dict[str, str] = Field(alias="additional_query_string_params", default={})
15+
useBasicAuthenticationWithAccessCodeGrant: bool = Field(
16+
alias="use_basic_authentication_with_access_code_grant",
17+
default=False,
18+
)
19+
usePkceWithAuthorizationCodeGrant: bool = Field(alias="use_pkce_with_authorization_code_grant", default=False)
20+
21+
if PYDANTIC_V2:
22+
model_config = {"extra": "allow"}
23+
else:
24+
25+
class Config:
26+
extra = "allow"
27+
allow_population_by_field_name = True

0 commit comments

Comments
 (0)