Skip to content

Commit 9968804

Browse files
committed
Make single team auth Bolt JS compatible
Also, this change removes authorization_test_enabled option from all authorization middleare.
1 parent f96d9b7 commit 9968804

28 files changed

Lines changed: 180 additions & 272 deletions

slack_bolt/app/app.py

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ def __init__(
7777
# for the OAuth flow
7878
oauth_settings: Optional[OAuthSettings] = None,
7979
oauth_flow: Optional[OAuthFlow] = None,
80-
authorization_test_enabled: bool = True,
8180
# No need to set (the value is used only in response to ssl_check requests)
8281
verification_token: Optional[str] = None,
8382
):
@@ -93,8 +92,6 @@ def __init__(
9392
:param oauth_settings: The settings related to Slack app installation flow (OAuth flow)
9493
:param oauth_flow: Manually instantiated slack_bolt.oauth.OAuthFlow.
9594
This is always prioritized over oauth_settings.
96-
:param authorization_test_enabled: Set False if you want to skip auth.test calls
97-
for every single incoming request from Slack (default: True)
9895
:param verification_token: Deprecated verification mechanism.
9996
This can used only for ssl_check requests.
10097
"""
@@ -129,7 +126,6 @@ def __init__(
129126
self._installation_store: Optional[InstallationStore] = installation_store
130127

131128
self._oauth_flow: Optional[OAuthFlow] = None
132-
self._authorization_test_enabled = authorization_test_enabled
133129
if oauth_flow:
134130
self._oauth_flow = oauth_flow
135131
if self._installation_store is None:
@@ -176,27 +172,18 @@ def _init_middleware_list(self):
176172

177173
if self._oauth_flow is None:
178174
if self._token:
179-
if self._authorization_test_enabled:
180-
self._middleware_list.append(SingleTeamAuthorization())
181-
else:
182-
try:
183-
auth_test_result = self._client.auth_test(token=self._token)
184-
self._middleware_list.append(
185-
SingleTeamAuthorization(
186-
auth_test_result=auth_test_result,
187-
verification_enabled=self._authorization_test_enabled,
188-
)
189-
)
190-
except SlackApiError as err:
191-
raise BoltError(error_auth_test_failure(err.response))
175+
try:
176+
auth_test_result = self._client.auth_test(token=self._token)
177+
self._middleware_list.append(
178+
SingleTeamAuthorization(auth_test_result=auth_test_result)
179+
)
180+
except SlackApiError as err:
181+
raise BoltError(error_auth_test_failure(err.response))
192182
else:
193183
raise BoltError(error_token_required())
194184
else:
195185
self._middleware_list.append(
196-
MultiTeamsAuthorization(
197-
installation_store=self._installation_store,
198-
verification_enabled=self._authorization_test_enabled,
199-
)
186+
MultiTeamsAuthorization(installation_store=self._installation_store)
200187
)
201188
self._middleware_list.append(IgnoringSelfEvents())
202189
self._middleware_list.append(UrlVerification())

slack_bolt/app/async_app.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ def __init__(
8585
# for the OAuth flow
8686
oauth_settings: Optional[AsyncOAuthSettings] = None,
8787
oauth_flow: Optional[AsyncOAuthFlow] = None,
88-
authorization_test_enabled: bool = True,
8988
# No need to set (the value is used only in response to ssl_check requests)
9089
verification_token: Optional[str] = None,
9190
):
@@ -101,8 +100,6 @@ def __init__(
101100
:param oauth_settings: The settings related to Slack app installation flow (OAuth flow)
102101
:param oauth_flow: Manually instantiated slack_bolt.oauth.async_oauth_flow.AsyncOAuthFlow.
103102
This is always prioritized over oauth_settings.
104-
:param authorization_test_enabled: Set False if you want to skip auth.test calls
105-
for every single incoming request from Slack (default: True)
106103
:param verification_token: Deprecated verification mechanism.
107104
This can used only for ssl_check requests.
108105
"""
@@ -134,8 +131,6 @@ def __init__(
134131
# NOTE: the token here can be None
135132
self._async_client = create_async_web_client(token)
136133

137-
self._authorization_test_enabled = authorization_test_enabled
138-
139134
self._async_installation_store: Optional[
140135
AsyncInstallationStore
141136
] = installation_store
@@ -188,18 +183,13 @@ def _init_async_middleware_list(self):
188183
)
189184
if self._async_oauth_flow is None:
190185
if self._token:
191-
self._async_middleware_list.append(
192-
AsyncSingleTeamAuthorization(
193-
verification_enabled=self._authorization_test_enabled
194-
)
195-
)
186+
self._async_middleware_list.append(AsyncSingleTeamAuthorization())
196187
else:
197188
raise BoltError(error_token_required())
198189
else:
199190
self._async_middleware_list.append(
200191
AsyncMultiTeamsAuthorization(
201-
installation_store=self._async_installation_store,
202-
verification_enabled=self._authorization_test_enabled,
192+
installation_store=self._async_installation_store
203193
)
204194
)
205195

slack_bolt/middleware/authorization/async_multi_teams_authorization.py

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,12 @@ class AsyncMultiTeamsAuthorization(AsyncAuthorization):
1919
installation_store: AsyncInstallationStore
2020
verification_enabled: bool
2121

22-
def __init__(
23-
self,
24-
installation_store: AsyncInstallationStore,
25-
verification_enabled: bool = True,
26-
):
22+
def __init__(self, installation_store: AsyncInstallationStore):
2723
"""Multi-workspace authorization.
2824
2925
:param installation_store: The module offering find/save operations of installation data.
30-
:param verification_enabled:
31-
Calls auth.test for every single incoming request from Slack if True (Default: True)
3226
"""
3327
self.installation_store = installation_store
34-
self.verification_enabled = verification_enabled
3528
self.logger = get_bolt_logger(AsyncMultiTeamsAuthorization)
3629

3730
async def async_process(
@@ -50,36 +43,23 @@ async def async_process(
5043
if bot is None:
5144
return _build_error_response()
5245

53-
if self.verification_enabled:
54-
auth_result = await req.context.client.auth_test(token=bot.bot_token)
55-
if auth_result:
56-
req.context["authorization_result"] = AuthorizationResult(
57-
enterprise_id=auth_result.get("enterprise_id", None),
58-
team_id=auth_result.get("team_id", None),
59-
bot_user_id=auth_result.get("user_id", None),
60-
bot_id=auth_result.get("bot_id", None),
61-
bot_token=bot.bot_token,
62-
)
63-
# TODO: bot -> user token
64-
req.context["token"] = bot.bot_token
65-
req.context["client"] = create_async_web_client(bot.bot_token)
66-
return await next()
67-
else:
68-
# Just in case
69-
self.logger.error("auth.test API call result is unexpectedly None")
70-
return _build_error_response()
71-
else:
46+
auth_result = await req.context.client.auth_test(token=bot.bot_token)
47+
if auth_result:
7248
req.context["authorization_result"] = AuthorizationResult(
73-
enterprise_id=bot.enterprise_id,
74-
team_id=bot.team_id,
75-
bot_user_id=bot.bot_user_id,
76-
bot_id=bot.bot_id,
49+
enterprise_id=auth_result.get("enterprise_id", None),
50+
team_id=auth_result.get("team_id", None),
51+
bot_user_id=auth_result.get("user_id", None),
52+
bot_id=auth_result.get("bot_id", None),
7753
bot_token=bot.bot_token,
7854
)
7955
# TODO: bot -> user token
8056
req.context["token"] = bot.bot_token
8157
req.context["client"] = create_async_web_client(bot.bot_token)
8258
return await next()
59+
else:
60+
# Just in case
61+
self.logger.error("auth.test API call result is unexpectedly None")
62+
return _build_error_response()
8363

8464
except SlackApiError as e:
8565
self.logger.error(f"Failed to authorize with the given token ({e})")

slack_bolt/middleware/authorization/async_single_team_authorization.py

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@
1111

1212

1313
class AsyncSingleTeamAuthorization(AsyncAuthorization):
14-
def __init__(self, *, verification_enabled: bool = True):
15-
"""Single-workspace authorization.
16-
17-
:param verification_enabled:
18-
Calls auth.test for every single incoming request from Slack if True (Default: True)
19-
"""
20-
self.verification_enabled = verification_enabled
14+
def __init__(self):
15+
"""Single-workspace authorization."""
2116
self.auth_result: Optional[AsyncSlackResponse] = None
2217
self.logger = get_bolt_logger(AsyncSingleTeamAuthorization)
2318

@@ -32,20 +27,12 @@ async def async_process(
3227
return await next()
3328

3429
try:
35-
if not self.verification_enabled:
36-
if self.auth_result is None:
37-
self.auth_result = await req.context.client.auth_test()
38-
req.context["authorization_result"] = _to_authorization_result(
39-
auth_test_result=self.auth_result,
40-
bot_token=req.context.client.token,
41-
request_user_id=req.context.user_id,
42-
)
43-
return await next()
30+
if self.auth_result is None:
31+
self.auth_result = await req.context.client.auth_test()
4432

45-
auth_result = await req.context.client.auth_test()
46-
if auth_result:
33+
if self.auth_result:
4734
req.context["authorization_result"] = _to_authorization_result(
48-
auth_test_result=auth_result,
35+
auth_test_result=self.auth_result,
4936
bot_token=req.context.client.token,
5037
request_user_id=req.context.user_id,
5138
)

slack_bolt/middleware/authorization/multi_teams_authorization.py

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@ class MultiTeamsAuthorization(Authorization):
1515
installation_store: InstallationStore
1616
verification_enabled: bool
1717

18-
def __init__(
19-
self, installation_store: InstallationStore, verification_enabled: bool = True,
20-
):
18+
def __init__(self, installation_store: InstallationStore):
2119
"""Multi-workspace authorization.
2220
2321
:param installation_store: The module offering find/save operations of installation data.
24-
:param verification_enabled:
25-
Calls auth.test for every single incoming request from Slack if True (Default: True)
2622
"""
2723
self.installation_store = installation_store
28-
self.verification_enabled = verification_enabled
2924
self.logger = get_bolt_logger(MultiTeamsAuthorization)
3025

3126
def process(
@@ -40,36 +35,23 @@ def process(
4035
if bot is None:
4136
return _build_error_response()
4237

43-
if self.verification_enabled:
44-
auth_result = req.context.client.auth_test(token=bot.bot_token)
45-
if auth_result:
46-
req.context["authorization_result"] = AuthorizationResult(
47-
enterprise_id=auth_result.get("enterprise_id", None),
48-
team_id=auth_result.get("team_id", None),
49-
bot_user_id=auth_result.get("user_id", None),
50-
bot_id=auth_result.get("bot_id", None),
51-
bot_token=bot.bot_token,
52-
)
53-
# TODO: bot -> user token
54-
req.context["token"] = bot.bot_token
55-
req.context["client"] = create_web_client(bot.bot_token)
56-
return next()
57-
else:
58-
# Just in case
59-
self.logger.error("auth.test API call result is unexpectedly None")
60-
return _build_error_response()
61-
else:
38+
auth_result = req.context.client.auth_test(token=bot.bot_token)
39+
if auth_result:
6240
req.context["authorization_result"] = AuthorizationResult(
63-
enterprise_id=bot.enterprise_id,
64-
team_id=bot.team_id,
65-
bot_user_id=bot.bot_user_id,
66-
bot_id=bot.bot_id,
41+
enterprise_id=auth_result.get("enterprise_id", None),
42+
team_id=auth_result.get("team_id", None),
43+
bot_user_id=auth_result.get("user_id", None),
44+
bot_id=auth_result.get("bot_id", None),
6745
bot_token=bot.bot_token,
6846
)
6947
# TODO: bot -> user token
7048
req.context["token"] = bot.bot_token
7149
req.context["client"] = create_web_client(bot.bot_token)
7250
return next()
51+
else:
52+
# Just in case
53+
self.logger.error("auth.test API call result is unexpectedly None")
54+
return _build_error_response()
7355

7456
except SlackApiError as e:
7557
self.logger.error(f"Failed to authorize with the given token ({e})")

slack_bolt/middleware/authorization/single_team_authorization.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,12 @@
1414

1515

1616
class SingleTeamAuthorization(Authorization):
17-
def __init__(
18-
self,
19-
*,
20-
auth_test_result: Optional[SlackResponse] = None,
21-
verification_enabled: bool = True,
22-
):
17+
def __init__(self, *, auth_test_result: Optional[SlackResponse] = None):
2318
"""Single-workspace authorization.
2419
2520
:param auth_test_result: The initial `auth.test` API call result.
26-
:param verification_enabled:
27-
Calls auth.test for every single incoming request from Slack if True (Default: True)
2821
"""
2922
self.auth_test_result = auth_test_result
30-
self.verification_enabled = verification_enabled
3123
self.logger = get_bolt_logger(SingleTeamAuthorization)
3224

3325
def process(
@@ -36,20 +28,13 @@ def process(
3628
if _is_no_auth_required(req):
3729
return next()
3830

39-
if not self.verification_enabled:
40-
# Skip calling auth.test every time the app receives requests
41-
req.context["authorization_result"] = _to_authorization_result(
42-
auth_test_result=self.auth_test_result,
43-
bot_token=req.context.client.token,
44-
request_user_id=req.context.user_id,
45-
)
46-
return next()
47-
4831
try:
49-
auth_result = req.context.client.auth_test()
50-
if auth_result:
32+
if not self.auth_test_result:
33+
self.auth_test_result = req.context.client.auth_test()
34+
35+
if self.auth_test_result:
5136
req.context["authorization_result"] = _to_authorization_result(
52-
auth_test_result=auth_result,
37+
auth_test_result=self.auth_test_result,
5338
bot_token=req.context.client.token,
5439
request_user_id=req.context.user_id,
5540
)

tests/adapter_tests/test_bottle.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
from time import time
3+
from typing import Optional
34
from urllib.parse import quote
45

56
from slack_sdk.signature import SignatureVerifier
@@ -16,22 +17,16 @@
1617
signing_secret = "secret"
1718
valid_token = "xoxb-valid"
1819
mock_api_server_base_url = "http://localhost:8888"
19-
web_client = WebClient(token=valid_token, base_url=mock_api_server_base_url,)
20-
app = App(client=web_client, signing_secret=signing_secret,)
21-
handler = SlackRequestHandler(app)
2220

2321

24-
@app.event("app_mention")
2522
def event_handler():
2623
pass
2724

2825

29-
@app.shortcut("test-shortcut")
3026
def shortcut_handler(ack):
3127
ack()
3228

3329

34-
@app.command("/hello-world")
3530
def command_handler(ack):
3631
ack()
3732

@@ -42,16 +37,24 @@ def command_handler(ack):
4237

4338
@post("/slack/events")
4439
def slack_events():
45-
return handler.handle(request, response)
40+
return TestBottle.handler.handle(request, response)
4641

4742

4843
class TestBottle:
4944
signature_verifier = SignatureVerifier(signing_secret)
45+
handler: Optional[SlackRequestHandler] = None
5046

5147
def setup_method(self):
5248
self.old_os_env = remove_os_env_temporarily()
5349
setup_mock_web_api_server(self)
5450

51+
web_client = WebClient(token=valid_token, base_url=mock_api_server_base_url,)
52+
app = App(client=web_client, signing_secret=signing_secret,)
53+
TestBottle.handler = SlackRequestHandler(app)
54+
app.event("app_mention")(event_handler)
55+
app.shortcut("test-shortcut")(shortcut_handler)
56+
app.command("/hello-world")(command_handler)
57+
5558
def teardown_method(self):
5659
cleanup_mock_web_api_server(self)
5760
restore_os_env(self.old_os_env)

0 commit comments

Comments
 (0)