Repository navigation
Expand file tree
/
Copy pathauthorize.py
More file actions
292 lines (261 loc) · 11.3 KB
/
Copy pathauthorize.py
File metadata and controls
292 lines (261 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import inspect
from logging import Logger
from typing import Optional, Callable, Dict, Any
from slack_sdk.errors import SlackApiError
from slack_sdk.oauth import InstallationStore
from slack_sdk.oauth.installation_store import Bot
from slack_sdk.oauth.installation_store.models.installation import Installation
from slack_sdk.oauth.token_rotation.rotator import TokenRotator
from slack_bolt.authorization.authorize_args import AuthorizeArgs
from slack_bolt.authorization.authorize_result import AuthorizeResult
from slack_bolt.context.context import BoltContext
from slack_bolt.error import BoltError
class Authorize:
"""This provides authorize function that returns AuthorizeResult
for an incoming request from Slack."""
def __init__(self):
pass
def __call__(
self,
*,
context: BoltContext,
enterprise_id: Optional[str],
team_id: Optional[str], # can be None for org-wide installed apps
user_id: Optional[str],
) -> Optional[AuthorizeResult]:
raise NotImplementedError()
class CallableAuthorize(Authorize):
"""When you pass the authorize argument in AsyncApp constructor,
This authorize implementation will be used.
"""
def __init__(
self,
*,
logger: Logger,
func: Callable[..., AuthorizeResult],
):
self.logger = logger
self.func = func
self.arg_names = inspect.getfullargspec(func).args
def __call__(
self,
*,
context: BoltContext,
enterprise_id: Optional[str],
team_id: Optional[str], # can be None for org-wide installed apps
user_id: Optional[str],
) -> Optional[AuthorizeResult]:
try:
all_available_args = {
"args": AuthorizeArgs(
context=context,
enterprise_id=enterprise_id,
team_id=team_id,
user_id=user_id,
),
"logger": context.logger,
"client": context.client,
"context": context,
"enterprise_id": enterprise_id,
"team_id": team_id,
"user_id": user_id,
}
for k, v in context.items():
if k not in all_available_args:
all_available_args[k] = v
kwargs: Dict[str, Any] = { # type: ignore
k: v for k, v in all_available_args.items() if k in self.arg_names # type: ignore
}
found_arg_names = kwargs.keys()
for name in self.arg_names:
if name not in found_arg_names:
self.logger.warning(f"{name} is not a valid argument")
kwargs[name] = None
auth_result = self.func(**kwargs)
if auth_result is None:
return auth_result
if isinstance(auth_result, AuthorizeResult):
return auth_result
else:
raise ValueError(
f"Unexpected returned value from authorize function (type: {type(auth_result)})"
)
except SlackApiError as err:
self.logger.debug(
f"The stored bot token for enterprise_id: {enterprise_id} team_id: {team_id} "
f"is no longer valid. (response: {err.response})"
)
return None
class InstallationStoreAuthorize(Authorize):
"""If you use the OAuth flow settings, this authorize implementation will be used.
As long as your own InstallationStore (or the built-in ones) works as you expect,
you can expect that the authorize layer should work for you without any customization.
"""
authorize_result_cache: Dict[str, AuthorizeResult]
bot_only: bool
find_installation_available: bool
find_bot_available: bool
token_rotator: Optional[TokenRotator]
_config_error_message: str = (
"InstallationStore with client_id/client_secret are required for token rotation"
)
def __init__(
self,
*,
logger: Logger,
installation_store: InstallationStore,
client_id: Optional[str] = None,
client_secret: Optional[str] = None,
token_rotation_expiration_minutes: Optional[int] = None,
# For v1.0.x compatibility and people who still want its simplicity
# use only InstallationStore#find_bot(enterprise_id, team_id)
bot_only: bool = False,
cache_enabled: bool = False,
):
self.logger = logger
self.installation_store = installation_store
self.bot_only = bot_only
self.cache_enabled = cache_enabled
self.authorize_result_cache = {}
self.find_installation_available = hasattr(
installation_store, "find_installation"
)
self.find_bot_available = hasattr(installation_store, "find_bot")
if client_id is not None and client_secret is not None:
self.token_rotator = TokenRotator(
client_id=client_id,
client_secret=client_secret,
)
else:
self.token_rotator = None
self.token_rotation_expiration_minutes = (
token_rotation_expiration_minutes or 120
)
def __call__(
self,
*,
context: BoltContext,
enterprise_id: Optional[str],
team_id: Optional[str], # can be None for org-wide installed apps
user_id: Optional[str],
) -> Optional[AuthorizeResult]:
bot_token: Optional[str] = None
user_token: Optional[str] = None
if not self.bot_only and self.find_installation_available:
# Since v1.1, this is the default way.
# If you want to use find_bot / delete_bot only, you can set bot_only as True.
try:
# Note that this is the latest information for the org/workspace.
# The installer may not be the user associated with this incoming request.
installation: Optional[
Installation
] = self.installation_store.find_installation(
enterprise_id=enterprise_id,
team_id=team_id,
is_enterprise_install=context.is_enterprise_install,
)
if installation is not None:
if installation.user_id != user_id:
# First off, remove the user token as the installer is a different user
installation.user_token = None
installation.user_scopes = []
# try to fetch the request user's installation
# to reflect the user's access token if exists
user_installation = self.installation_store.find_installation(
enterprise_id=enterprise_id,
team_id=team_id,
user_id=user_id,
is_enterprise_install=context.is_enterprise_install,
)
if user_installation is not None:
# Overwrite the installation with the one for this user
installation = user_installation
bot_token, user_token = (
installation.bot_token,
installation.user_token,
)
if (
installation.user_refresh_token is not None
or installation.bot_refresh_token is not None
):
if self.token_rotator is None:
raise BoltError(self._config_error_message)
refreshed = self.token_rotator.perform_token_rotation(
installation=installation,
minutes_before_expiration=self.token_rotation_expiration_minutes,
)
if refreshed is not None:
self.installation_store.save(refreshed)
bot_token, user_token = (
refreshed.bot_token,
refreshed.user_token,
)
except NotImplementedError as _:
self.find_installation_available = False
if (
# If you intentionally use only find_bot / delete_bot,
self.bot_only
# If find_installation method is not available,
or not self.find_installation_available
# If find_installation did not return data and find_bot method is available,
or (
self.find_bot_available is True
and bot_token is None
and user_token is None
)
):
try:
bot: Optional[Bot] = self.installation_store.find_bot(
enterprise_id=enterprise_id,
team_id=team_id,
is_enterprise_install=context.is_enterprise_install,
)
if bot is not None:
bot_token = bot.bot_token
if bot.bot_refresh_token is not None:
# Token rotation
if self.token_rotator is None:
raise BoltError(self._config_error_message)
refreshed = self.token_rotator.perform_bot_token_rotation(
bot=bot,
minutes_before_expiration=self.token_rotation_expiration_minutes,
)
if refreshed is not None:
self.installation_store.save_bot(refreshed)
bot_token = refreshed.bot_token
except NotImplementedError as _:
self.find_bot_available = False
except Exception as e:
self.logger.info(f"Failed to call find_bot method: {e}")
token: Optional[str] = bot_token or user_token
if token is None:
# No valid token was found
self._debug_log_for_not_found(enterprise_id, team_id)
return None
# Check cache to see if the bot object already exists
if self.cache_enabled and token in self.authorize_result_cache:
return self.authorize_result_cache[token]
try:
auth_test_api_response = context.client.auth_test(token=token)
authorize_result = AuthorizeResult.from_auth_test_response(
auth_test_response=auth_test_api_response,
bot_token=bot_token,
user_token=user_token,
)
if self.cache_enabled:
self.authorize_result_cache[token] = authorize_result
return authorize_result
except SlackApiError as err:
self.logger.debug(
f"The stored bot token for enterprise_id: {enterprise_id} team_id: {team_id} "
f"is no longer valid. (response: {err.response})"
)
return None
# ------------------------------------------------
def _debug_log_for_not_found(
self, enterprise_id: Optional[str], team_id: Optional[str]
):
self.logger.debug(
"No installation data found "
f"for enterprise_id: {enterprise_id} team_id: {team_id}"
)