danswer/backend/onyx/server/manage/validate_tokens.py
Kaveen Jayamanna 880c42ad41
Validating slackbot tokens (#3695)
* 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
2025-01-17 11:50:22 -08:00

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