Skip to content

Commit e2505bf

Browse files
authored
Add bot|user_scopes to context.authorize_result set by SingleTeamAuthorization (#1104)
1 parent ce27780 commit e2505bf

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

slack_bolt/middleware/authorization/internals.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def _to_authorize_result( # type: ignore
6868
request_user_id: Optional[str],
6969
) -> AuthorizeResult:
7070
user_id = auth_test_result.get("user_id")
71+
oauth_scopes: Optional[str] = auth_test_result.headers.get("x-oauth-scopes")
7172
return AuthorizeResult(
7273
enterprise_id=auth_test_result.get("enterprise_id"),
7374
team_id=auth_test_result.get("team_id"),
@@ -76,4 +77,6 @@ def _to_authorize_result( # type: ignore
7677
bot_token=token if _is_bot_token(token) else None,
7778
user_id=request_user_id or (user_id if not _is_bot_token(token) else None),
7879
user_token=token if not _is_bot_token(token) else None,
80+
bot_scopes=oauth_scopes if _is_bot_token(token) else None,
81+
user_scopes=None if _is_bot_token(token) else oauth_scopes,
7982
)

tests/mock_web_api_server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,15 @@ def _handle(self):
149149
if self.is_valid_user_token():
150150
if path == "/auth.test":
151151
self.send_response(200)
152+
self.send_header("x-oauth-scopes", "chat:write,search:read")
152153
self.set_common_headers(len(USER_AUTH_TEST_RESPONSE))
153154
self.wfile.write(USER_AUTH_TEST_RESPONSE.encode("utf-8"))
154155
return
155156

156157
if self.is_valid_token():
157158
if path == "/auth.test":
158159
self.send_response(200)
160+
self.send_header("x-oauth-scopes", "chat:write,commands")
159161
self.set_common_headers(len(BOT_AUTH_TEST_RESPONSE))
160162
self.wfile.write(BOT_AUTH_TEST_RESPONSE.encode("utf-8"))
161163
return

tests/slack_bolt/middleware/authorization/test_single_team_authorization.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from slack_sdk import WebClient
2+
from slack_sdk.web import SlackResponse
23

34
from slack_bolt.middleware import SingleTeamAuthorization
45
from slack_bolt.middleware.authorization.internals import _build_user_facing_authorize_error_message
@@ -34,6 +35,29 @@ def test_success_pattern(self):
3435
assert resp.status == 200
3536
assert resp.body == ""
3637

38+
def test_success_pattern_with_bot_scopes(self):
39+
client = WebClient(base_url=self.mock_api_server_base_url, token="xoxb-valid")
40+
auth_test_result: SlackResponse = SlackResponse(
41+
client=client,
42+
http_verb="POST",
43+
api_url="https://slack.com/api/auth.test",
44+
req_args={},
45+
data={},
46+
headers={"x-oauth-scopes": "chat:write,commands"},
47+
status_code=200,
48+
)
49+
authorization = SingleTeamAuthorization(auth_test_result=auth_test_result)
50+
req = BoltRequest(body="payload={}", headers={})
51+
req.context["client"] = client
52+
resp = BoltResponse(status=404)
53+
54+
resp = authorization.process(req=req, resp=resp, next=next)
55+
56+
assert resp.status == 200
57+
assert resp.body == ""
58+
assert req.context.authorize_result.bot_scopes == ["chat:write", "commands"]
59+
assert req.context.authorize_result.user_scopes is None
60+
3761
def test_failure_pattern(self):
3862
authorization = SingleTeamAuthorization(auth_test_result={})
3963
req = BoltRequest(body="payload={}", headers={})

tests/slack_bolt_async/middleware/authorization/test_single_team_authorization.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22

33
import pytest
4+
from slack.web.async_slack_response import AsyncSlackResponse
45
from slack_sdk.web.async_client import AsyncWebClient
56

67
from slack_bolt.middleware.authorization.async_single_team_authorization import (
@@ -47,6 +48,21 @@ async def test_success_pattern(self):
4748
assert resp.status == 200
4849
assert resp.body == ""
4950

51+
@pytest.mark.asyncio
52+
async def test_success_pattern_with_bot_scopes(self):
53+
client = AsyncWebClient(base_url=self.mock_api_server_base_url, token="xoxb-valid")
54+
authorization = AsyncSingleTeamAuthorization()
55+
req = AsyncBoltRequest(body="payload={}", headers={})
56+
req.context["client"] = client
57+
resp = BoltResponse(status=404)
58+
59+
resp = await authorization.async_process(req=req, resp=resp, next=next)
60+
61+
assert resp.status == 200
62+
assert resp.body == ""
63+
assert req.context.authorize_result.bot_scopes == ["chat:write", "commands"]
64+
assert req.context.authorize_result.user_scopes is None
65+
5066
@pytest.mark.asyncio
5167
async def test_failure_pattern(self):
5268
authorization = AsyncSingleTeamAuthorization()

0 commit comments

Comments
 (0)