-
Notifications
You must be signed in to change notification settings - Fork 421
/
Copy pathsecurity_schemes_global_and_optional.py
48 lines (38 loc) · 1.24 KB
/
security_schemes_global_and_optional.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import (
APIGatewayRestResolver,
)
from aws_lambda_powertools.event_handler.openapi.models import (
OAuth2,
OAuthFlowAuthorizationCode,
OAuthFlows,
)
tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver(enable_validation=True)
@app.get("/protected", security=[{"oauth": ["admin"]}]) # (1)!
def protected() -> dict:
return {"hello": "world"}
@app.get("/unprotected", security=[{}]) # (1)!
def unprotected() -> dict:
return {"hello": "world"}
@logger.inject_lambda_context
@tracer.capture_lambda_handler
def lambda_handler(event, context):
return app.resolve(event, context)
if __name__ == "__main__":
print(
app.get_openapi_json_schema(
title="My API",
security_schemes={
"oauth": OAuth2(
flows=OAuthFlows(
authorizationCode=OAuthFlowAuthorizationCode(
authorizationUrl="https://xxx.amazoncognito.com/oauth2/authorize",
tokenUrl="https://xxx.amazoncognito.com/oauth2/token",
),
),
),
},
),
)