Skip to content
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
4 changes: 4 additions & 0 deletions slack_bolt/adapter/aws_lambda/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def to_bolt_request(event) -> BoltRequest:
if event["isBase64Encoded"]:
body = base64.b64decode(body).decode("utf-8")
cookies: Sequence[str] = event.get("cookies", [])
if cookies is None or len(cookies) == 0:
# In the case of format v1
multiValueHeaders = event.get("multiValueHeaders", {})
cookies = multiValueHeaders.get("Cookie", [])
headers = event.get("headers", {})
headers["cookie"] = cookies
return BoltRequest(
Expand Down
44 changes: 44 additions & 0 deletions tests/adapter_tests/aws/test_aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from moto import mock_lambda
from slack_sdk.signature import SignatureVerifier
from slack_sdk.web import WebClient
from slack_sdk.oauth import OAuthStateStore

from slack_bolt.adapter.aws_lambda import SlackRequestHandler
from slack_bolt.adapter.aws_lambda.handler import not_found
Expand Down Expand Up @@ -318,3 +319,46 @@ def test_oauth(self):
assert response["statusCode"] == 200
assert response["headers"]["content-type"] == "text/html; charset=utf-8"
assert "https://slack.com/oauth/v2/authorize?state=" in response.get("body")

@mock_lambda
def test_oauth_redirect(self):
class TestStateStore(OAuthStateStore):
def consume(self, state: str) -> bool:
return state == "uuid4-value"

app = App(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If we have a mock state_store, which returns a fixed value "uuid4-value", the test can be better. Can you check this?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Sorry, I could not understand where to check.
I would be grateful if you could give me specific instructions.
(I'm not familiar with @mock_lambda in moto, and you said about it??)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@tattee Thanks for asking this! I thought that we need to implement state_store with issue/consume methods (returning a fixed value "uuid4-value" means having the issue method). But, for this test, having only consume method is required. Thus, the following diff should work for you!

diff --git a/tests/adapter_tests/aws/test_aws_lambda.py b/tests/adapter_tests/aws/test_aws_lambda.py
index b92300e..1b0166c 100644
--- a/tests/adapter_tests/aws/test_aws_lambda.py
+++ b/tests/adapter_tests/aws/test_aws_lambda.py
@@ -3,6 +3,7 @@ from time import time
 from urllib.parse import quote
 
 from moto import mock_lambda
+from slack_sdk.oauth import OAuthStateStore
 from slack_sdk.signature import SignatureVerifier
 from slack_sdk.web import WebClient
 
@@ -321,6 +322,10 @@ class TestAWSLambda:
 
     @mock_lambda
     def test_oauth_redirect(self):
+        class TestStateStore(OAuthStateStore):
+            def consume(self, state: str) -> bool:
+                return state == "uuid4-value"
+
         app = App(
             client=self.web_client,
             signing_secret=self.signing_secret,
@@ -328,6 +333,7 @@ class TestAWSLambda:
                 client_id="111.111",
                 client_secret="xxx",
                 scopes=["chat:write", "commands"],
+                state_store=TestStateStore(),
             ),
         )
 
@@ -340,7 +346,7 @@ class TestAWSLambda:
             "isBase64Encoded": False,
         }
         response = SlackRequestHandler(app).handle(event, self.context)
-        assert response["statusCode"] == 401
+        assert response["statusCode"] == 200
         assert response["headers"]["content-type"] == "text/html; charset=utf-8"
         assert response.get("body") is not None
 
@@ -353,6 +359,6 @@ class TestAWSLambda:
             "isBase64Encoded": False,
         }
         response = SlackRequestHandler(app).handle(event, self.context)
-        assert response["statusCode"] == 401
+        assert response["statusCode"] == 200
         assert response["headers"]["content-type"] == "text/html; charset=utf-8"
         assert response.get("body") is not None

client=self.web_client,
signing_secret=self.signing_secret,
oauth_settings=OAuthSettings(
client_id="111.111",
client_secret="xxx",
scopes=["chat:write", "commands"],
state_store=TestStateStore(),
),
)

event = {
"body": "",
"queryStringParameters": {"code": "1234567890", "state": "uuid4-value"},
"headers": {},
"cookies": ["slack-app-oauth-state=uuid4-value"],
"requestContext": {"http": {"method": "GET"}},
"isBase64Encoded": False,
}
response = SlackRequestHandler(app).handle(event, self.context)
assert response["statusCode"] == 200
assert response["headers"]["content-type"] == "text/html; charset=utf-8"
assert response.get("body") is not None

event = {
"body": "",
"queryStringParameters": {"code": "1234567890", "state": "uuid4-value"},
"headers": {},
"multiValueHeaders": {"Cookie": ["slack-app-oauth-state=uuid4-value"]},
"requestContext": {"httpMethod": "GET"},
"isBase64Encoded": False,
}
response = SlackRequestHandler(app).handle(event, self.context)
assert response["statusCode"] == 200
assert response["headers"]["content-type"] == "text/html; charset=utf-8"
assert response.get("body") is not None