File tree Expand file tree Collapse file tree 2 files changed +56
-0
lines changed
examples/event_handler_rest/src Expand file tree Collapse file tree 2 files changed +56
-0
lines changed Original file line number Diff line number Diff line change @@ -1103,6 +1103,14 @@ Security schemes are declared at the top-level first. You can reference them glo
11031103
11041104 1. Using the oauth security scheme defined bellow, scoped to the "admin" role.
11051105
1106+ === "Global security schemes and optional security per route"
1107+
1108+ ```python title="security_schemes_global_and_optional.py" hl_lines="22 37-46"
1109+ --8<-- "examples/event_handler_rest/src/security_schemes_global_and_optional.py"
1110+ ```
1111+
1112+ 1. To make security optional in a specific route, an empty security requirement ({}) can be included in the array.
1113+
11061114OpenAPI 3 lets you describe APIs protected using the following security schemes:
11071115
11081116| Security Scheme | Type | Description |
Original file line number Diff line number Diff line change 1+ from aws_lambda_powertools import Logger , Tracer
2+ from aws_lambda_powertools .event_handler import (
3+ APIGatewayRestResolver ,
4+ )
5+ from aws_lambda_powertools .event_handler .openapi .models import (
6+ OAuth2 ,
7+ OAuthFlowAuthorizationCode ,
8+ OAuthFlows ,
9+ )
10+
11+ tracer = Tracer ()
12+ logger = Logger ()
13+
14+ app = APIGatewayRestResolver (enable_validation = True )
15+
16+
17+ @app .get ("/protected" , security = [{"oauth" : ["admin" ]}])
18+ def protected () -> dict :
19+ return {"hello" : "world" }
20+
21+
22+ @app .get ("/unprotected" , security = [{}]) # (1)!
23+ def unprotected () -> dict :
24+ return {"hello" : "world" }
25+
26+
27+ @logger .inject_lambda_context
28+ @tracer .capture_lambda_handler
29+ def lambda_handler (event , context ):
30+ return app .resolve (event , context )
31+
32+
33+ if __name__ == "__main__" :
34+ print (
35+ app .get_openapi_json_schema (
36+ title = "My API" ,
37+ security_schemes = {
38+ "oauth" : OAuth2 (
39+ flows = OAuthFlows (
40+ authorizationCode = OAuthFlowAuthorizationCode (
41+ authorizationUrl = "https://xxx.amazoncognito.com/oauth2/authorize" ,
42+ tokenUrl = "https://xxx.amazoncognito.com/oauth2/token" ,
43+ ),
44+ ),
45+ ),
46+ },
47+ ),
48+ )
You can’t perform that action at this time.
0 commit comments