diff --git a/api/app/crud.py b/api/app/crud.py index c3cc39fa..9bb8c1c9 100644 --- a/api/app/crud.py +++ b/api/app/crud.py @@ -241,4 +241,4 @@ async def update_channel_by_bot_id(bot_id: str, data): stmt = update(JBChannel).where(JBChannel.bot_id == bot_id).values(**data) await session.execute(stmt) await session.commit() - return bot_id + return bot_id \ No newline at end of file diff --git a/api/app/routers/v1/__init__.py b/api/app/routers/v1/__init__.py index 73c2d33e..a8f9c4cc 100644 --- a/api/app/routers/v1/__init__.py +++ b/api/app/routers/v1/__init__.py @@ -1,4 +1,5 @@ from fastapi import APIRouter, HTTPException, Request +import uuid from ...crud import get_chat_history, get_bot_list, get_bot_chat_sessions from ...handlers.v1 import handle_callback, handle_webhook from ...handlers.v1.bot_handlers import ( @@ -16,6 +17,7 @@ tags=["v1"], ) +JBMANAGER_KEY = str(uuid.uuid4()) @router.get("/bots") async def get_bots(): @@ -30,9 +32,25 @@ async def get_bots(): bot.status = status return bots +@router.get("/secret") +async def get_secret_key(): + return {"secret": JBMANAGER_KEY} + + +@router.put("/refresh-key") +async def refresh_secret_key(): + JBMANAGER_KEY = str(uuid.uuid4()) + return {"status": "success"} @router.post("/bot/install") -async def install_bot(install_content: JBBotCode): +async def install_bot(request:Request, install_content: JBBotCode): + headers = dict(request.headers) + authorization = headers.get("authorization") + if authorization is None: + raise HTTPException(status_code=401, detail="Authorization header not provided") + if authorization != f"Bearer {JBMANAGER_KEY}": + raise HTTPException(status_code=401, detail="Unauthorized") + flow_input = await handle_install_bot(install_content) try: produce_message(flow_input) diff --git a/frontend/src/components/settings-model/index.tsx b/frontend/src/components/settings-model/index.tsx index 69eb8a14..65fb48fd 100644 --- a/frontend/src/components/settings-model/index.tsx +++ b/frontend/src/components/settings-model/index.tsx @@ -1,6 +1,7 @@ import React, { useState, useEffect } from 'react'; import './settings-model.css'; import { sendRequest } from '@/api'; +import { fetchSecret } from '@/pages/home/home'; interface InputConfig { value: string | number | boolean; @@ -47,6 +48,7 @@ const SettingsModal: React.FC = ({ botId, isOpen, onClose, inputs, modelT const handleSubmit = async () => { let data:any = {}; let apiEndpoint = ''; + let accessToken = null; if (modelType === 'credentials') { apiEndpoint = `${APIHOST}/v1/bot/${botId}/configure`; let credentials:any = {}; @@ -73,6 +75,13 @@ const SettingsModal: React.FC = ({ botId, isOpen, onClose, inputs, modelT data['channels']['whatsapp'] = inputElements['whatsapp'].value; } else if (modelType === 'install') { apiEndpoint = `${APIHOST}/v1/bot/install`; + try { + accessToken = await fetchSecret(); + } catch (error) { + console.error("Failed to fetch the access token:", error); + alert("Failed to fetch the access token"); + return; + } for (let key in inputElements) { if (inputElements[key].required === true && !inputElements[key].value.toString().trim()) { alert(`${key} is required`); @@ -89,6 +98,7 @@ const SettingsModal: React.FC = ({ botId, isOpen, onClose, inputs, modelT await sendRequest({ url: apiEndpoint, method: 'POST', + accessToken: accessToken, body: JSON.stringify(data ), headers: { 'Content-Type': 'application/json', diff --git a/frontend/src/pages/home/home.tsx b/frontend/src/pages/home/home.tsx index 24abc594..cc6345b0 100644 --- a/frontend/src/pages/home/home.tsx +++ b/frontend/src/pages/home/home.tsx @@ -12,6 +12,19 @@ interface props { } +export async function fetchSecret() { + const url = `${APIHOST}/v1/secret`; + try { + const response = await fetch(url); + const data = await response.json(); + const secret = data.secret; + return secret; // Return the secret explicitly + } catch (error) { + console.error("Error fetching secret:", error); + throw error; // Throw error to handle it later + } +} + export const Home:React.FunctionComponent = (props:props) => { const [refreshBots, incrementrefreshBots] = React.useState(0); const [projects, setProjects] = React.useState([]); @@ -103,17 +116,27 @@ export const Home:React.FunctionComponent = (props:props) => {
-
{ navigator.clipboard.writeText("https://bandhujbstorage.z29.web.core.windows.net/"); alert("Installation URL copied") }}> +
{ navigator.clipboard.writeText(import.meta.env.VITE_SERVER_HOST+"/v1/bot/install"); alert("Installation URL copied") }}> - {import.meta.env.VITE_SERVER_HOST+"/install"} + {import.meta.env.VITE_SERVER_HOST+"/v1/bot/install"}
+
{ + try { + const secret = await fetchSecret(); + await navigator.clipboard.writeText(secret); + alert("JB Manager secret copied!"); + } catch (error) { + alert("Failed to copy secret"); + } + }}> *************************** +