Module aws_lambda_powertools.event_handler.openapi.swagger_ui

Sub-modules

aws_lambda_powertools.event_handler.openapi.swagger_ui.html
aws_lambda_powertools.event_handler.openapi.swagger_ui.oauth2

Functions

def generate_oauth2_redirect_html() ‑> str

Generates the HTML content for the OAuth2 redirect page.

Source: https://github.com/swagger-api/swagger-ui/blob/master/dist/oauth2-redirect.html

def generate_swagger_html(spec: str, swagger_js: str, swagger_css: str, swagger_base_url: str, oauth2_config: OAuth2Config | None, persist_authorization: bool = False)

Generate Swagger UI HTML page

Parameters

spec : str
The OpenAPI spec
swagger_js : str
 
Swagger UI JavaScript source code or URL
swagger_css : str
Swagger UI CSS source code or URL
swagger_base_url : str
The base URL for Swagger UI
oauth2_config : OAuth2Config, optional
The OAuth2 configuration.
persist_authorization : bool, optional
Whether to persist authorization data on browser close/refresh.

Classes

class OAuth2Config (**data: Any)

OAuth2 configuration for Swagger UI

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

Expand source code
class OAuth2Config(BaseModel):
    """
    OAuth2 configuration for Swagger UI
    """

    # The client ID for the OAuth2 application
    clientId: Optional[str] = Field(alias="client_id", default=None)

    # The client secret for the OAuth2 application. This is sensitive information and requires the explicit presence
    # of the POWERTOOLS_DEV environment variable.
    clientSecret: Optional[str] = Field(alias="client_secret", default=None)

    # The realm in which the OAuth2 application is registered. Optional.
    realm: Optional[str] = Field(default=None)

    # The name of the OAuth2 application
    appName: str = Field(alias="app_name")

    # The scopes that the OAuth2 application requires. Defaults to an empty list.
    scopes: Sequence[str] = Field(default=[])

    # Additional query string parameters to be included in the OAuth2 request. Defaults to an empty dictionary.
    additionalQueryStringParams: Dict[str, str] = Field(alias="additional_query_string_params", default={})

    # Whether to use basic authentication with the access code grant type. Defaults to False.
    useBasicAuthenticationWithAccessCodeGrant: bool = Field(
        alias="use_basic_authentication_with_access_code_grant",
        default=False,
    )

    # Whether to use PKCE with the authorization code grant type. Defaults to False.
    usePkceWithAuthorizationCodeGrant: bool = Field(alias="use_pkce_with_authorization_code_grant", default=False)

    model_config = MODEL_CONFIG_ALLOW

    @field_validator("clientSecret")
    def client_secret_only_on_dev(cls, v: Optional[str]) -> Optional[str]:
        if not v:
            return None

        if not powertools_dev_is_set():
            raise ValueError(
                "cannot use client_secret without POWERTOOLS_DEV mode. See "
                "https://docs.powertools.aws.dev/lambda/python/latest/#optimizing-for-non-production-environments",
            )
        else:
            warnings.warn(
                "OAuth2Config is using client_secret and POWERTOOLS_DEV is set. This reveals sensitive information. "
                "DO NOT USE THIS OUTSIDE LOCAL DEVELOPMENT",
                stacklevel=2,
            )
            return v

Ancestors

  • pydantic.main.BaseModel

Class variables

var additionalQueryStringParams : Dict[str, str]
var appName : str
var clientId : str | None
var clientSecret : str | None
var model_computed_fields
var model_config
var model_fields
var realm : str | None
var scopes : Sequence[str]
var useBasicAuthenticationWithAccessCodeGrant : bool
var usePkceWithAuthorizationCodeGrant : bool

Static methods

def client_secret_only_on_dev(v: str | None) ‑> str | None