Skip to content

Commit

Permalink
Validating slackbot tokens (#3695)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ktjayamanna authored Jan 17, 2025
1 parent c9e0d77 commit 880c42a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
1 change: 1 addition & 0 deletions .vscode/env_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ REQUIRE_EMAIL_VERIFICATION=False

# Set these so if you wipe the DB, you don't end up having to go through the UI every time
GEN_AI_API_KEY=<REPLACE THIS>
OPENAI_API_KEY=<REPLACE THIS>
# If answer quality isn't important for dev, use gpt-4o-mini since it's cheaper
GEN_AI_MODEL_VERSION=gpt-4o
FAST_GEN_AI_MODEL_VERSION=gpt-4o
Expand Down
7 changes: 4 additions & 3 deletions CONTRIBUTING_VSCODE.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ Before starting, make sure the Docker Daemon is running.
1. Open the Debug view in VSCode (Cmd+Shift+D on macOS)
2. From the dropdown at the top, select "Clear and Restart External Volumes and Containers" and press the green play button
3. From the dropdown at the top, select "Run All Onyx Services" and press the green play button
4. Now, you can navigate to onyx in your browser (default is http://localhost:3000) and start using the app
5. You can set breakpoints by clicking to the left of line numbers to help debug while the app is running
6. Use the debug toolbar to step through code, inspect variables, etc.
4. CD into web, run "npm i" followed by npm run dev.
5. Now, you can navigate to onyx in your browser (default is http://localhost:3000) and start using the app
6. You can set breakpoints by clicking to the left of line numbers to help debug while the app is running
7. Use the debug toolbar to step through code, inspect variables, etc.

## Features

Expand Down
7 changes: 7 additions & 0 deletions backend/onyx/server/manage/slack_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from onyx.server.manage.models import SlackBotCreationRequest
from onyx.server.manage.models import SlackChannelConfig
from onyx.server.manage.models import SlackChannelConfigCreationRequest
from onyx.server.manage.validate_tokens import validate_app_token
from onyx.server.manage.validate_tokens import validate_bot_token
from onyx.utils.telemetry import create_milestone_and_report


Expand Down Expand Up @@ -222,6 +224,9 @@ def create_bot(
_: User | None = Depends(current_admin_user),
tenant_id: str | None = Depends(get_current_tenant_id),
) -> SlackBot:
validate_app_token(slack_bot_creation_request.app_token)
validate_bot_token(slack_bot_creation_request.bot_token)

slack_bot_model = insert_slack_bot(
db_session=db_session,
name=slack_bot_creation_request.name,
Expand All @@ -248,6 +253,8 @@ def patch_bot(
db_session: Session = Depends(get_session),
_: User | None = Depends(current_admin_user),
) -> SlackBot:
validate_bot_token(slack_bot_creation_request.bot_token)
validate_app_token(slack_bot_creation_request.app_token)
slack_bot_model = update_slack_bot(
db_session=db_session,
slack_bot_id=slack_bot_id,
Expand Down
43 changes: 43 additions & 0 deletions backend/onyx/server/manage/validate_tokens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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
8 changes: 7 additions & 1 deletion web/src/app/admin/bots/SlackTokensForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,13 @@ export const SlackTokensForm = ({
router.push(`/admin/bots/${encodeURIComponent(botId)}`);
} else {
const responseJson = await response.json();
const errorMsg = responseJson.detail || responseJson.message;
let errorMsg = responseJson.detail || responseJson.message;

if (errorMsg.includes("Invalid bot token:")) {
errorMsg = "Slack Bot Token is invalid";
} else if (errorMsg.includes("Invalid app token:")) {
errorMsg = "Slack App Token is invalid";
}
setPopup({
message: isUpdate
? `Error updating Slack Bot - ${errorMsg}`
Expand Down

0 comments on commit 880c42a

Please sign in to comment.