Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(event_handler): mutualTLS Security Scheme for OpenAPI #5484

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion aws_lambda_powertools/event_handler/openapi/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ class SecuritySchemeType(Enum):
http = "http"
oauth2 = "oauth2"
openIdConnect = "openIdConnect"
mutualTLS = "mutualTLS"


class SecurityBase(OpenAPIExtensions):
Expand Down Expand Up @@ -440,7 +441,11 @@ class OpenIdConnect(SecurityBase):
openIdConnectUrl: str


SecurityScheme = Union[APIKey, HTTPBase, OAuth2, OpenIdConnect, HTTPBearer]
class MutualTLS(SecurityBase):
type_: SecuritySchemeType = Field(default=SecuritySchemeType.mutualTLS, alias="type")


SecurityScheme = Union[APIKey, HTTPBase, OAuth2, OpenIdConnect, HTTPBearer, MutualTLS]


# https://swagger.io/specification/#components-object
Expand Down
1 change: 1 addition & 0 deletions docs/core/event_handler/api_gateway.md
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,7 @@ OpenAPI 3 lets you describe APIs protected using the following security schemes:
| [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"}. |
| [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. |
| [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. |
| [Mutual TLS](https://swagger.io/specification/#security-scheme-object){target="_blank"}. | `MutualTLS` | Client/server certificate mutual authentication scheme. |

???-note "Using OAuth2 with the Swagger UI?"
You can use the `OAuth2Config` option to configure a default OAuth2 app on the generated Swagger UI.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
APIKey,
APIKeyIn,
HTTPBearer,
MutualTLS,
OAuth2,
OAuthFlowImplicit,
OAuthFlows,
Expand Down Expand Up @@ -110,3 +111,24 @@ def handler():
open_id_connect_scheme = security_schemes["openIdConnect"]
assert open_id_connect_scheme.type_.value == "openIdConnect"
assert open_id_connect_scheme.openIdConnectUrl == "https://example.com/oauth2/authorize"


def test_openapi_security_scheme_mtls():
app = APIGatewayRestResolver()

@app.get("/")
def handler():
raise NotImplementedError()

schema = app.get_openapi_schema(
security_schemes={
"mutualTLS": MutualTLS(description="mTLS Authentication"),
},
)

security_schemes = schema.components.securitySchemes
assert security_schemes is not None

assert "mutualTLS" in security_schemes
mtls_scheme = security_schemes["mutualTLS"]
assert mtls_scheme.description == "mTLS Authentication"