Skip to content

Commit d62bae1

Browse files
committed
feat: add docs
1 parent 8994ec1 commit d62bae1

File tree

4 files changed

+178
-6
lines changed

4 files changed

+178
-6
lines changed

docs/core/event_handler/api_gateway.md

+46-6
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,18 @@ To implement these customizations, include extra parameters when defining your r
991991
--8<-- "examples/event_handler_rest/src/customizing_api_operations.py"
992992
```
993993

994+
#### Customizing OpenAPI metadata
995+
996+
--8<-- "docs/core/event_handler/_openapi_customization_metadata.md"
997+
998+
Include extra parameters when exporting your OpenAPI specification to apply these customizations:
999+
1000+
=== "customizing_api_metadata.py"
1001+
1002+
```python hl_lines="25-31"
1003+
--8<-- "examples/event_handler_rest/src/customizing_api_metadata.py"
1004+
```
1005+
9941006
#### Customizing Swagger UI
9951007

9961008
???+note "Customizing the Swagger metadata"
@@ -1014,16 +1026,44 @@ Below is an example configuration for serving Swagger UI from a custom path or C
10141026
--8<-- "examples/event_handler_rest/src/customizing_swagger_middlewares.py"
10151027
```
10161028

1017-
#### Customizing OpenAPI metadata
1029+
#### Security schemes
10181030

1019-
--8<-- "docs/core/event_handler/_openapi_customization_metadata.md"
1031+
???-info "Does Powertools implement any of the security schemes?"
1032+
No. Powertools adds support for generating OpenAPI documentation with security schemes, but it doesn't implement any of the security schemes itself.
10201033

1021-
Include extra parameters when exporting your OpenAPI specification to apply these customizations:
1034+
OpenAPI uses the term security scheme for [authentication and authorization schemes](https://swagger.io/docs/specification/authentication/){target="_blank"}.
1035+
When you're describing your API, declare security schemes at the top level, and reference them globally or per operation.
10221036

1023-
=== "customizing_api_metadata.py"
1037+
=== "Global OpenAPI security schemes"
10241038

1025-
```python hl_lines="25-31"
1026-
--8<-- "examples/event_handler_rest/src/customizing_api_metadata.py"
1039+
```python title="security_schemes_global.py" hl_lines="32-42"
1040+
--8<-- "examples/event_handler_rest/src/security_schemes_global.py"
1041+
```
1042+
1043+
1. Using the oauth security scheme defined earlier, scoped to the "admin" role.
1044+
1045+
=== "Per Operation security"
1046+
1047+
```python title="security_schemes_per_operation.py" hl_lines="17 32-41"
1048+
--8<-- "examples/event_handler_rest/src/security_schemes_per_operation.py"
1049+
```
1050+
1051+
1. Using the oauth security scheme defined bellow, scoped to the "admin" role.
1052+
1053+
OpenAPI 3 lets you describe APIs protected using the following security schemes:
1054+
1055+
| Security Scheme | Type | Description |
1056+
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
1057+
| [HTTP auth](https://www.iana.org/assignments/http-authschemes/http-authschemes.xhtml){target="_blank"} | `HTTPBase` | HTTP authentication schemes using the Authorization header (e.g: [Basic auth](https://swagger.io/docs/specification/authentication/basic-authentication/){target="_blank"}, [Bearer](https://swagger.io/docs/specification/authentication/bearer-authentication/){target="_blank"}) |
1058+
| [API keys](https://swagger.io/docs/specification/authentication/api-keys/https://swagger.io/docs/specification/authentication/api-keys/){target="_blank"} (e.g: query strings, cookies) | `APIKey` | API keys in headers, query strings or [cookies](https://swagger.io/docs/specification/authentication/cookie-authentication/){target="_blank"}. |
1059+
| [OAuth 2](https://swagger.io/docs/specification/authentication/oauth2/){target="_blank"} | `OAuth2` | Authorization protocol that gives an API client limited access to user data on a web server. |
1060+
| [OpenID Connect Discovery](https://swagger.io/docs/specification/authentication/openid-connect-discovery/){target="_blank"} | `OpenIdConnect` | Identity layer built [on top of the OAuth 2.0 protocol](https://openid.net/developers/how-connect-works/){target="_blank"} and supported by some OAuth 2.0. |
1061+
1062+
???-note "Using OAuth2 with the Swagger UI?"
1063+
You can use the `OAuth2Config` option to configure a default OAuth2 app on the generated Swagger UI.
1064+
1065+
```python hl_lines="10 15-18 22"
1066+
--8<-- "examples/event_handler_rest/src/swagger_with_oauth2.py"
10271067
```
10281068

10291069
### Custom serializer
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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("/")
18+
def helloworld() -> dict:
19+
return {"hello": "world"}
20+
21+
22+
@logger.inject_lambda_context
23+
@tracer.capture_lambda_handler
24+
def lambda_handler(event, context):
25+
return app.resolve(event, context)
26+
27+
28+
if __name__ == "__main__":
29+
print(
30+
app.get_openapi_json_schema(
31+
title="My API",
32+
security_schemes={
33+
"oauth": OAuth2(
34+
flows=OAuthFlows(
35+
authorizationCode=OAuthFlowAuthorizationCode(
36+
authorizationUrl="https://xxx.amazoncognito.com/oauth2/authorize",
37+
tokenUrl="https://xxx.amazoncognito.com/oauth2/token",
38+
),
39+
),
40+
),
41+
},
42+
security=[{"oauth": ["admin"]}], # (1)!
43+
),
44+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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("/", security=[{"oauth": ["admin"]}]) # (1)!
18+
def helloworld() -> dict:
19+
return {"hello": "world"}
20+
21+
22+
@logger.inject_lambda_context
23+
@tracer.capture_lambda_handler
24+
def lambda_handler(event, context):
25+
return app.resolve(event, context)
26+
27+
28+
if __name__ == "__main__":
29+
print(
30+
app.get_openapi_json_schema(
31+
title="My API",
32+
security_schemes={
33+
"oauth": OAuth2(
34+
flows=OAuthFlows(
35+
authorizationCode=OAuthFlowAuthorizationCode(
36+
authorizationUrl="https://xxx.amazoncognito.com/oauth2/authorize",
37+
tokenUrl="https://xxx.amazoncognito.com/oauth2/token",
38+
),
39+
),
40+
),
41+
},
42+
),
43+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
from aws_lambda_powertools.event_handler.openapi.swagger_ui import OAuth2Config
11+
12+
tracer = Tracer()
13+
logger = Logger()
14+
15+
oauth2 = OAuth2Config(
16+
client_id="xxxxxxxxxxxxxxxxxxxxxxxxxxxx",
17+
app_name="OAuth2 app",
18+
)
19+
20+
app = APIGatewayRestResolver(enable_validation=True)
21+
app.enable_swagger(
22+
oauth2_config=oauth2,
23+
security_schemes={
24+
"oauth": OAuth2(
25+
flows=OAuthFlows(
26+
authorizationCode=OAuthFlowAuthorizationCode(
27+
authorizationUrl="https://xxx.amazoncognito.com/oauth2/authorize",
28+
tokenUrl="https://xxx.amazoncognito.com/oauth2/token",
29+
),
30+
),
31+
),
32+
},
33+
security=[{"oauth": []}],
34+
)
35+
36+
37+
@app.get("/")
38+
def hello() -> str:
39+
return "world"
40+
41+
42+
@logger.inject_lambda_context
43+
@tracer.capture_lambda_handler
44+
def lambda_handler(event, context):
45+
return app.resolve(event, context)

0 commit comments

Comments
 (0)