slack: Provide more information when a Slack token fails to validate.

(cherry picked from commit 4c8915c8e4)
This commit is contained in:
Alex Vandiver
2023-05-19 21:26:06 +00:00
parent 783f3fac3b
commit 8878fee6d6
2 changed files with 68 additions and 2 deletions

View File

@@ -1466,8 +1466,13 @@ def check_token_access(token: str) -> None:
data = requests.get(
"https://slack.com/api/team.info", headers={"Authorization": f"Bearer {token}"}
)
if data.status_code != 200 or not data.json()["ok"]:
raise ValueError(f"Invalid Slack token: {token}")
if data.status_code != 200:
raise ValueError(
f"Failed to fetch data (HTTP status {data.status_code}) for Slack token: {token}"
)
if not data.json()["ok"]:
error = data.json()["error"]
raise ValueError(f"Invalid Slack token: {token}, {error}")
has_scopes = set(data.headers.get("x-oauth-scopes", "").split(","))
required_scopes = {"emoji:read", "users:read", "users:read.email", "team:read"}
missing_scopes = required_scopes - has_scopes