mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-05-07 18:30:25 +02:00
* added missing dependency, missing api key placeholder, updated docs * Apply black formatting and validate bot token functionality * acknowledging black formatting * added the validation to update tokens as well * Made the token validation errors looks nicer * getting rif of duplicate dependency
44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
import requests
|
|
from fastapi import HTTPException
|
|
|
|
SLACK_API_URL = "https://slack.com/api/auth.test"
|
|
SLACK_CONNECTIONS_OPEN_URL = "https://slack.com/api/apps.connections.open"
|
|
|
|
|
|
def validate_bot_token(bot_token: str) -> bool:
|
|
headers = {"Authorization": f"Bearer {bot_token}"}
|
|
response = requests.post(SLACK_API_URL, headers=headers)
|
|
|
|
if response.status_code != 200:
|
|
raise HTTPException(
|
|
status_code=500, detail="Error communicating with Slack API."
|
|
)
|
|
|
|
data = response.json()
|
|
if not data.get("ok", False):
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail=f"Invalid bot token: {data.get('error', 'Unknown error')}",
|
|
)
|
|
|
|
return True
|
|
|
|
|
|
def validate_app_token(app_token: str) -> bool:
|
|
headers = {"Authorization": f"Bearer {app_token}"}
|
|
response = requests.post(SLACK_CONNECTIONS_OPEN_URL, headers=headers)
|
|
|
|
if response.status_code != 200:
|
|
raise HTTPException(
|
|
status_code=500, detail="Error communicating with Slack API."
|
|
)
|
|
|
|
data = response.json()
|
|
if not data.get("ok", False):
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail=f"Invalid app token: {data.get('error', 'Unknown error')}",
|
|
)
|
|
|
|
return True
|