From 16af2537b61bf289ef0b0f73816e377f65da3eca Mon Sep 17 00:00:00 2001 From: Taran <97586318+Tarraann@users.noreply.github.com> Date: Wed, 19 Jul 2023 13:46:03 +0530 Subject: [PATCH] Twitter and Google Oauth for Web (#808) Co-authored-by: Tarraann --- gui/pages/Content/Toolkits/ToolkitWorkspace.js | 10 ++++++++-- gui/pages/Dashboard/Content.js | 2 +- superagi/controllers/google_oauth.py | 8 +++++++- superagi/controllers/twitter_oauth.py | 2 -- superagi/helper/twitter_tokens.py | 8 +++++++- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/gui/pages/Content/Toolkits/ToolkitWorkspace.js b/gui/pages/Content/Toolkits/ToolkitWorkspace.js index 99fc711e7..894cc9d0f 100644 --- a/gui/pages/Content/Toolkits/ToolkitWorkspace.js +++ b/gui/pages/Content/Toolkits/ToolkitWorkspace.js @@ -10,7 +10,7 @@ import { import styles from './Tool.module.css'; import {setLocalStorageValue, setLocalStorageArray, returnToolkitIcon, convertToTitleCase} from "@/utils/utils"; -export default function ToolkitWorkspace({toolkitDetails, internalId}) { +export default function ToolkitWorkspace({env, toolkitDetails, internalId}) { const [activeTab, setActiveTab] = useState('configuration') const [showDescription, setShowDescription] = useState(false) const [apiConfigs, setApiConfigs] = useState([]); @@ -25,9 +25,15 @@ export default function ToolkitWorkspace({toolkitDetails, internalId}) { }; function getGoogleToken(client_data) { + var redirect_uri = ""; + if (env == "PROD"){ + redirect_uri = 'https://app.superagi.com/api/google/oauth-tokens'; + } + else { + redirect_uri = "http://localhost:3000/api/google/oauth-tokens"; + } const client_id = client_data.client_id const scope = 'https://www.googleapis.com/auth/calendar'; - const redirect_uri = 'http://localhost:3000/api/google/oauth-tokens'; window.location.href = `https://accounts.google.com/o/oauth2/v2/auth?client_id=${client_id}&redirect_uri=${redirect_uri}&access_type=offline&response_type=code&scope=${scope}`; } diff --git a/gui/pages/Dashboard/Content.js b/gui/pages/Dashboard/Content.js index fd02188f5..953e8cf40 100644 --- a/gui/pages/Dashboard/Content.js +++ b/gui/pages/Dashboard/Content.js @@ -340,7 +340,7 @@ export default function Content({env, selectedView, selectedProjectId, organisat } {tab.contentType === 'Toolkits' && - } + } {tab.contentType === 'Settings' && } {tab.contentType === 'Marketplace' && } {tab.contentType === 'Add_Toolkit' && } diff --git a/superagi/controllers/google_oauth.py b/superagi/controllers/google_oauth.py index 4eecd6ba3..3e996372b 100644 --- a/superagi/controllers/google_oauth.py +++ b/superagi/controllers/google_oauth.py @@ -15,6 +15,7 @@ from superagi.models.tool_config import ToolConfig from superagi.models.toolkit import Toolkit from superagi.models.oauth_tokens import OauthTokens +from superagi.config.config import get_config router = APIRouter() @@ -26,10 +27,15 @@ async def google_auth_calendar(code: str = Query(...), Authorize: AuthJWT = Depe client_secret = client_secret.value token_uri = 'https://oauth2.googleapis.com/token' scope = 'https://www.googleapis.com/auth/calendar' + env = get_config("ENV", "DEV") + if env == "DEV": + redirect_uri = "http://localhost:3000/api/google/oauth-tokens" + else: + redirect_uri = "https://superagi.com/api/google/oauth-tokens" params = { 'client_id': client_id, 'client_secret': client_secret, - 'redirect_uri': "http://localhost:3000/api/google/oauth-tokens", + 'redirect_uri': redirect_uri, 'scope': scope, 'grant_type': 'authorization_code', 'code': code, diff --git a/superagi/controllers/twitter_oauth.py b/superagi/controllers/twitter_oauth.py index 6bdfa3761..16adb8930 100644 --- a/superagi/controllers/twitter_oauth.py +++ b/superagi/controllers/twitter_oauth.py @@ -19,8 +19,6 @@ @router.get('/oauth-tokens') async def twitter_oauth(oauth_token: str = Query(...),oauth_verifier: str = Query(...), Authorize: AuthJWT = Depends()): - print("///////////////////////////") - print(oauth_token) token_uri = f'https://api.twitter.com/oauth/access_token?oauth_verifier={oauth_verifier}&oauth_token={oauth_token}' conn = http_client.HTTPSConnection("api.twitter.com") conn.request("POST", token_uri, "") diff --git a/superagi/helper/twitter_tokens.py b/superagi/helper/twitter_tokens.py index 10b36d59a..e8dcea71f 100644 --- a/superagi/helper/twitter_tokens.py +++ b/superagi/helper/twitter_tokens.py @@ -9,6 +9,7 @@ from sqlalchemy.orm import Session from superagi.models.toolkit import Toolkit from superagi.models.oauth_tokens import OauthTokens +from superagi.config.config import get_config class Creds: @@ -29,8 +30,13 @@ def get_request_token(self,api_data): http_method = 'POST' base_url = 'https://api.twitter.com/oauth/request_token' + env = get_config("ENV", "DEV") + if env == "DEV": + oauth_callback = "http://localhost:3000/api/twitter/oauth-tokens" + else: + oauth_callback = "https://superagi.com/api/twitter/oauth-tokens" params = { - 'oauth_callback': 'http://localhost:3000/api/twitter/oauth-tokens', + 'oauth_callback': oauth_callback, 'oauth_consumer_key': api_key, 'oauth_nonce': self.gen_nonce(), 'oauth_signature_method': 'HMAC-SHA1',