83
83
Contact ,
84
84
License ,
85
85
OpenAPI ,
86
+ SecurityScheme ,
86
87
Server ,
87
88
Tag ,
88
89
)
90
+ from aws_lambda_powertools .event_handler .openapi .oauth2 import OAuth2Config
89
91
from aws_lambda_powertools .event_handler .openapi .params import Dependant
90
92
from aws_lambda_powertools .event_handler .openapi .types import (
91
93
TypeModelOrEnum ,
@@ -282,6 +284,7 @@ def __init__(
282
284
tags : Optional [List [str ]],
283
285
operation_id : Optional [str ],
284
286
include_in_schema : bool ,
287
+ security : Optional [List [Dict [str , List [str ]]]],
285
288
middlewares : Optional [List [Callable [..., Response ]]],
286
289
):
287
290
"""
@@ -317,6 +320,8 @@ def __init__(
317
320
The OpenAPI operationId for this route
318
321
include_in_schema: bool
319
322
Whether or not to include this route in the OpenAPI schema
323
+ security: List[Dict[str, List[str]]], optional
324
+ The OpenAPI security for this route
320
325
middlewares: Optional[List[Callable[..., Response]]]
321
326
The list of route middlewares to be called in order.
322
327
"""
@@ -339,6 +344,7 @@ def __init__(
339
344
self .response_description = response_description
340
345
self .tags = tags or []
341
346
self .include_in_schema = include_in_schema
347
+ self .security = security
342
348
self .middlewares = middlewares or []
343
349
self .operation_id = operation_id or self ._generate_operation_id ()
344
350
@@ -486,6 +492,10 @@ def _get_openapi_path(
486
492
)
487
493
parameters .extend (operation_params )
488
494
495
+ # Add security if present
496
+ if self .security :
497
+ operation ["security" ] = self .security
498
+
489
499
# Add the parameters to the OpenAPI operation
490
500
if parameters :
491
501
all_parameters = {(param ["in" ], param ["name" ]): param for param in parameters }
@@ -885,6 +895,7 @@ def route(
885
895
tags : Optional [List [str ]] = None ,
886
896
operation_id : Optional [str ] = None ,
887
897
include_in_schema : bool = True ,
898
+ security : Optional [List [Dict [str , List [str ]]]] = None ,
888
899
middlewares : Optional [List [Callable [..., Any ]]] = None ,
889
900
):
890
901
raise NotImplementedError ()
@@ -943,6 +954,7 @@ def get(
943
954
tags : Optional [List [str ]] = None ,
944
955
operation_id : Optional [str ] = None ,
945
956
include_in_schema : bool = True ,
957
+ security : Optional [List [Dict [str , List [str ]]]] = None ,
946
958
middlewares : Optional [List [Callable [..., Any ]]] = None ,
947
959
):
948
960
"""Get route decorator with GET `method`
@@ -980,6 +992,7 @@ def lambda_handler(event, context):
980
992
tags ,
981
993
operation_id ,
982
994
include_in_schema ,
995
+ security ,
983
996
middlewares ,
984
997
)
985
998
@@ -996,6 +1009,7 @@ def post(
996
1009
tags : Optional [List [str ]] = None ,
997
1010
operation_id : Optional [str ] = None ,
998
1011
include_in_schema : bool = True ,
1012
+ security : Optional [List [Dict [str , List [str ]]]] = None ,
999
1013
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1000
1014
):
1001
1015
"""Post route decorator with POST `method`
@@ -1034,6 +1048,7 @@ def lambda_handler(event, context):
1034
1048
tags ,
1035
1049
operation_id ,
1036
1050
include_in_schema ,
1051
+ security ,
1037
1052
middlewares ,
1038
1053
)
1039
1054
@@ -1050,6 +1065,7 @@ def put(
1050
1065
tags : Optional [List [str ]] = None ,
1051
1066
operation_id : Optional [str ] = None ,
1052
1067
include_in_schema : bool = True ,
1068
+ security : Optional [List [Dict [str , List [str ]]]] = None ,
1053
1069
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1054
1070
):
1055
1071
"""Put route decorator with PUT `method`
@@ -1088,6 +1104,7 @@ def lambda_handler(event, context):
1088
1104
tags ,
1089
1105
operation_id ,
1090
1106
include_in_schema ,
1107
+ security ,
1091
1108
middlewares ,
1092
1109
)
1093
1110
@@ -1104,6 +1121,7 @@ def delete(
1104
1121
tags : Optional [List [str ]] = None ,
1105
1122
operation_id : Optional [str ] = None ,
1106
1123
include_in_schema : bool = True ,
1124
+ security : Optional [List [Dict [str , List [str ]]]] = None ,
1107
1125
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1108
1126
):
1109
1127
"""Delete route decorator with DELETE `method`
@@ -1141,6 +1159,7 @@ def lambda_handler(event, context):
1141
1159
tags ,
1142
1160
operation_id ,
1143
1161
include_in_schema ,
1162
+ security ,
1144
1163
middlewares ,
1145
1164
)
1146
1165
@@ -1157,6 +1176,7 @@ def patch(
1157
1176
tags : Optional [List [str ]] = None ,
1158
1177
operation_id : Optional [str ] = None ,
1159
1178
include_in_schema : bool = True ,
1179
+ security : Optional [List [Dict [str , List [str ]]]] = None ,
1160
1180
middlewares : Optional [List [Callable ]] = None ,
1161
1181
):
1162
1182
"""Patch route decorator with PATCH `method`
@@ -1197,6 +1217,7 @@ def lambda_handler(event, context):
1197
1217
tags ,
1198
1218
operation_id ,
1199
1219
include_in_schema ,
1220
+ security ,
1200
1221
middlewares ,
1201
1222
)
1202
1223
@@ -1419,6 +1440,8 @@ def get_openapi_schema(
1419
1440
terms_of_service : Optional [str ] = None ,
1420
1441
contact : Optional ["Contact" ] = None ,
1421
1442
license_info : Optional ["License" ] = None ,
1443
+ security_schemes : Optional [Dict [str , "SecurityScheme" ]] = None ,
1444
+ security : Optional [List [Dict [str , List [str ]]]] = None ,
1422
1445
) -> "OpenAPI" :
1423
1446
"""
1424
1447
Returns the OpenAPI schema as a pydantic model.
@@ -1445,6 +1468,10 @@ def get_openapi_schema(
1445
1468
The contact information for the exposed API.
1446
1469
license_info: License, optional
1447
1470
The license information for the exposed API.
1471
+ security_schemes: Dict[str, "SecurityScheme"]], optional
1472
+ A declaration of the security schemes available to be used in the specification.
1473
+ security: List[Dict[str, List[str]]], optional
1474
+ A declaration of which security mechanisms are applied globally across the API.
1448
1475
1449
1476
Returns
1450
1477
-------
@@ -1498,6 +1525,16 @@ def get_openapi_schema(
1498
1525
# with an url value of /.
1499
1526
output ["servers" ] = [Server (url = "/" )]
1500
1527
1528
+ if security :
1529
+ if not security_schemes :
1530
+ raise ValueError ("security_schemes must be provided if security is provided" )
1531
+
1532
+ # Check if all keys in security are present in the security_schemes
1533
+ if not all (key in security_schemes for sec in security for key in sec ):
1534
+ raise ValueError ("Some security schemes not found in security_schemes" )
1535
+
1536
+ output ["security" ] = security
1537
+
1501
1538
components : Dict [str , Dict [str , Any ]] = {}
1502
1539
paths : Dict [str , Dict [str , Any ]] = {}
1503
1540
operation_ids : Set [str ] = set ()
@@ -1534,6 +1571,8 @@ def get_openapi_schema(
1534
1571
1535
1572
if definitions :
1536
1573
components ["schemas" ] = {k : definitions [k ] for k in sorted (definitions )}
1574
+ if security_schemes :
1575
+ components ["securitySchemes" ] = security_schemes
1537
1576
if components :
1538
1577
output ["components" ] = components
1539
1578
if tags :
@@ -1556,6 +1595,8 @@ def get_openapi_json_schema(
1556
1595
terms_of_service : Optional [str ] = None ,
1557
1596
contact : Optional ["Contact" ] = None ,
1558
1597
license_info : Optional ["License" ] = None ,
1598
+ security_schemes : Optional [Dict [str , "SecurityScheme" ]] = None ,
1599
+ security : Optional [List [Dict [str , List [str ]]]] = None ,
1559
1600
) -> str :
1560
1601
"""
1561
1602
Returns the OpenAPI schema as a JSON serializable dict
@@ -1582,6 +1623,10 @@ def get_openapi_json_schema(
1582
1623
The contact information for the exposed API.
1583
1624
license_info: License, optional
1584
1625
The license information for the exposed API.
1626
+ security_schemes: Dict[str, "SecurityScheme"]], optional
1627
+ A declaration of the security schemes available to be used in the specification.
1628
+ security: List[Dict[str, List[str]]], optional
1629
+ A declaration of which security mechanisms are applied globally across the API.
1585
1630
1586
1631
Returns
1587
1632
-------
@@ -1602,6 +1647,8 @@ def get_openapi_json_schema(
1602
1647
terms_of_service = terms_of_service ,
1603
1648
contact = contact ,
1604
1649
license_info = license_info ,
1650
+ security_schemes = security_schemes ,
1651
+ security = security ,
1605
1652
),
1606
1653
by_alias = True ,
1607
1654
exclude_none = True ,
@@ -1623,6 +1670,7 @@ def enable_swagger(
1623
1670
contact : Optional ["Contact" ] = None ,
1624
1671
license_info : Optional ["License" ] = None ,
1625
1672
swagger_base_url : Optional [str ] = None ,
1673
+ oauth2 : Optional ["OAuth2Config" ] = None ,
1626
1674
middlewares : Optional [List [Callable [..., Response ]]] = None ,
1627
1675
compress : bool = False ,
1628
1676
):
@@ -1655,6 +1703,8 @@ def enable_swagger(
1655
1703
The license information for the exposed API.
1656
1704
swagger_base_url: str, optional
1657
1705
The base url for the swagger UI. If not provided, we will serve a recent version of the Swagger UI.
1706
+ oauth2: OAuth2Config, optional
1707
+ The OAuth2 configuration for the Swagger UI.
1658
1708
middlewares: List[Callable[..., Response]], optional
1659
1709
List of middlewares to be used for the swagger route.
1660
1710
compress: bool, default = False
@@ -1719,6 +1769,7 @@ def swagger_handler():
1719
1769
swagger_js ,
1720
1770
swagger_css ,
1721
1771
swagger_base_url ,
1772
+ oauth2 ,
1722
1773
)
1723
1774
1724
1775
return Response (
@@ -1741,6 +1792,7 @@ def route(
1741
1792
tags : Optional [List [str ]] = None ,
1742
1793
operation_id : Optional [str ] = None ,
1743
1794
include_in_schema : bool = True ,
1795
+ security : Optional [List [Dict [str , List [str ]]]] = None ,
1744
1796
middlewares : Optional [List [Callable [..., Any ]]] = None ,
1745
1797
):
1746
1798
"""Route decorator includes parameter `method`"""
@@ -1767,6 +1819,7 @@ def register_resolver(func: Callable):
1767
1819
tags ,
1768
1820
operation_id ,
1769
1821
include_in_schema ,
1822
+ security ,
1770
1823
middlewares ,
1771
1824
)
1772
1825
@@ -2318,6 +2371,7 @@ def route(
2318
2371
tags : Optional [List [str ]] = None ,
2319
2372
operation_id : Optional [str ] = None ,
2320
2373
include_in_schema : bool = True ,
2374
+ security : Optional [List [Dict [str , List [str ]]]] = None ,
2321
2375
middlewares : Optional [List [Callable [..., Any ]]] = None ,
2322
2376
):
2323
2377
# NOTE: see #1552 for more context.
@@ -2334,6 +2388,7 @@ def route(
2334
2388
tags ,
2335
2389
operation_id ,
2336
2390
include_in_schema ,
2391
+ security ,
2337
2392
middlewares ,
2338
2393
)
2339
2394
0 commit comments