Skip to content

feat(event_source): add class APIGatewayAuthorizerResponseWebSocket #6058

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

Merged
merged 5 commits into from
Feb 10, 2025

Conversation

leandrodamascena
Copy link
Contributor

Issue number: #5800

Summary

Changes

The current APIGatewayAuthorizerResponse class is not suitable for generating valid policy statements for WebSocket APIs. This new class will ensure that developers can easily create correct authorization responses for WebSocket APIs.

The new APIGatewayAuthorizerResponseWebSocket class address the limitations of the current APIGatewayAuthorizerResponse class when dealing with WebSocket APIs. The existing allow_route(), deny_route(), deny_all_routes and allow_all_routes() methods do not create valid policy statements for WebSocket connections, necessitating a separate class to handle WebSocket-specific authorization responses.

User experience

from aws_lambda_powertools.utilities.data_classes import event_source
from aws_lambda_powertools.utilities.data_classes.api_gateway_authorizer_event import (
    APIGatewayAuthorizerRequestEvent,
    APIGatewayAuthorizerResponseWebSocket,
)


@event_source(data_class=APIGatewayAuthorizerRequestEvent)
def lambda_handler(event: APIGatewayAuthorizerRequestEvent, context):
    # Simple auth check (replace with your actual auth logic)
    is_authorized = event.headers.get("HeaderAuth1") == "headerValue1"

    if not is_authorized:
        return {"principalId": "", "policyDocument": {"Version": "2012-10-17", "Statement": []}}

    arn = event.parsed_arn

    policy = APIGatewayAuthorizerResponseWebSocket(
        principal_id="user",
        context={"user": "example"},
        region=arn.region,
        aws_account_id=arn.aws_account_id,
        api_id=arn.api_id,
        stage=arn.stage,
    )

    policy.allow_all_routes()

    return policy.asdict()

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@leandrodamascena leandrodamascena requested a review from a team February 8, 2025 21:48
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation tests labels Feb 8, 2025
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 8, 2025
@github-actions github-actions bot added feature New feature or functionality and removed documentation Improvements or additions to documentation labels Feb 8, 2025
@leandrodamascena leandrodamascena changed the title feat(event_source): add specialized class APIGatewayAuthorizerResponseWebSocket feat(event_source): add class APIGatewayAuthorizerResponseWebSocket Feb 8, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 8, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 8, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 8, 2025
@leandrodamascena leandrodamascena self-assigned this Feb 8, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 8, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 8, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 8, 2025
Copy link

codecov bot commented Feb 8, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.25%. Comparing base (4b6fb57) to head (bf17b50).
Report is 2 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #6058      +/-   ##
===========================================
+ Coverage    96.24%   96.25%   +0.01%     
===========================================
  Files          234      234              
  Lines        11024    11057      +33     
  Branches       800      803       +3     
===========================================
+ Hits         10610    10643      +33     
  Misses         327      327              
  Partials        87       87              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@anafalcao anafalcao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing PR! Thank you Leo, approved

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 10, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 10, 2025
@leandrodamascena leandrodamascena merged commit a4ec359 into develop Feb 10, 2025
14 checks passed
@leandrodamascena leandrodamascena deleted the authorizer-ws branch February 10, 2025 12:14
sinofseven pushed a commit to sinofseven/powertools-lambda-python-my-extend that referenced this pull request Feb 13, 2025
…ws-powertools#6058)

* Adding new class to WebSocket Authorizer

* Adding new class to WebSocket Authorizer

* Making mypy happy

* Making mypy happy

---------

Co-authored-by: Ana Falcão <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or functionality size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: APIGatewayAuthorizerResponse cannot be used for Websocket $connect
2 participants