slack-import: Downgrade Slack legacy-token check failure to warning.

Slack has disabled creation of legacy tokens, which means we have to use other
tokens for importing the data. Thus, we shouldn't throw an error if the token
doesn't match the legacy token format.

Since we do not have any other validation for those tokens yet, we log a warning
but still try to continue with the import assuming that the token has the right
scopes.

See https://api.slack.com/changelog/2020-02-legacy-test-token-creation-to-retire.
This commit is contained in:
Rohitt Vashishtha
2020-05-11 18:45:20 +05:30
committed by Tim Abbott
parent 309266376e
commit 31a34836d3
2 changed files with 9 additions and 11 deletions

View File

@@ -993,6 +993,8 @@ def do_convert_data(slack_zip_file: str, output_dir: str, token: str, threads: i
realm_id = 0
domain_name = settings.EXTERNAL_HOST
log_token_warning(token)
slack_data_dir = slack_zip_file.replace('.zip', '')
if not os.path.exists(slack_data_dir):
os.makedirs(slack_data_dir)
@@ -1055,15 +1057,16 @@ def get_data_file(path: str) -> Any:
data = ujson.load(fp)
return data
def log_token_warning(token: str) -> None:
if not token.startswith("xoxp-"):
logging.info('Not a Slack legacy token.\n'
' This token might not have all the needed scopes. We need the following scopes:\n'
' - emoji:read\n - users:read\n - users:read.email\n - team:read')
def get_slack_api_data(slack_api_url: str, get_param: str, **kwargs: Any) -> Any:
if not kwargs.get("token"):
raise AssertionError("Slack token missing in kwargs")
token = kwargs["token"]
if not token.startswith("xoxp-"):
raise Exception('Invalid Slack legacy token.\n'
' You must pass a Slack "legacy token" starting with "xoxp-".\n'
' Create one at https://api.slack.com/custom-integrations/legacy-tokens')
data = requests.get("{}?{}".format(slack_api_url, urlencode(kwargs)))
if data.status_code == requests.codes.ok:

View File

@@ -94,11 +94,6 @@ class SlackImporter(ZulipTestCase):
get_slack_api_data(slack_user_list_url, "members", token=token)
self.assertEqual(invalid.exception.args, ('Error accessing Slack API: invalid_auth',),)
token = 'xoxe-invalid-token'
with self.assertRaises(Exception) as invalid:
get_slack_api_data(slack_user_list_url, "members", token=token)
self.assertTrue(invalid.exception.args[0].startswith("Invalid Slack legacy token.\n"))
with self.assertRaises(Exception) as invalid:
get_slack_api_data(slack_user_list_url, "members")
self.assertEqual(invalid.exception.args, ('Slack token missing in kwargs',),)