-
Notifications
You must be signed in to change notification settings - Fork 406
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(event_handler): demonstrate handling optional security routes (#…
…5895) * Adding example on how to work with optional security routes * Adding example on how to work with optional security routes
- Loading branch information
1 parent
b74a659
commit bb24814
Showing
2 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
examples/event_handler_rest/src/security_schemes_global_and_optional.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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"]}]) | ||
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", | ||
), | ||
), | ||
), | ||
}, | ||
), | ||
) |