From c406b4453e82d98934dfc21af1d81e3aa1a22835 Mon Sep 17 00:00:00 2001 From: frikky Date: Sat, 15 Jan 2022 00:47:35 +0100 Subject: [PATCH] Started adding Github Oauth2 system for creators --- frontend/src/views/SetAuthentication.jsx | 106 +++++++++++++++++++++-- frontend/src/views/SettingsPage.jsx | 82 +++++++++++++++--- 2 files changed, 170 insertions(+), 18 deletions(-) diff --git a/frontend/src/views/SetAuthentication.jsx b/frontend/src/views/SetAuthentication.jsx index fc109926..e2dd8be3 100644 --- a/frontend/src/views/SetAuthentication.jsx +++ b/frontend/src/views/SetAuthentication.jsx @@ -18,6 +18,7 @@ const SetAuthentication = (props) => { //session_state const urlSearchParams = new URLSearchParams(window.location.search); const params = Object.fromEntries(urlSearchParams.entries()); + console.log("PARAMS: ", params) const authenticationStore = []; var appAuthData = { @@ -29,19 +30,16 @@ const SetAuthentication = (props) => { }, fields: [], type: "oauth2", - }; + } if (window !== undefined && window !== null) { - console.log(window.location); + //console.log(window.location); appAuthData.fields.push({ key: "redirect_uri", value: window.location.origin + window.location.pathname, }); } - if (params.code !== undefined && params.code !== null) { - appAuthData.fields.push({ key: "code", value: params.code }); - } if (params.session_state !== undefined && params.session_state !== null) { appAuthData.fields.push({ @@ -50,12 +48,34 @@ const SetAuthentication = (props) => { }); } + var externalData = { + handleExternal: false, + user: "", + type: "", + code: "", + } + + if (params.code !== undefined && params.code !== null) { + appAuthData.fields.push({ key: "code", value: params.code }); + externalData.code = params.code + } + if (params.state !== undefined && params.state !== null) { const paramsplit = params.state.split("&"); console.log(paramsplit); for (var key in paramsplit) { const query = paramsplit[key].split("="); - console.log(query); + console.log("K:V: ", key, query); + if (query.length > 1) { + if (query[0] === "type" && query[1] === "github"){ + externalData.handleExternal = true + externalData.type = "github" + } + + if (query[0] === "username" || query[0] === "user"){ + externalData.user = query[1] + } + } if (query.length !== 2) { console.log("INVALID QUERY: ", query); @@ -116,7 +136,79 @@ const SetAuthentication = (props) => { } } - console.log(appAuthData); + if (externalData.handleExternal) { + console.log("RUN EXTERNAL!!: ", externalData) + + fetch(globalUrl + "/api/v1/triggers/github/register", { + method: "PUT", + headers: { + "Content-Type": "application/json", + Accept: "application/json", + }, + credentials: "include", + body: JSON.stringify(externalData), + }) + .then((response) => { + const cursearch = typeof window === "undefined" || window.location === undefined ? "" : window.location.search; + const tmpView = new URLSearchParams(cursearch).get("state"); + if ( + tmpView !== undefined && + tmpView !== null && + tmpView.length > 0 + ) { + console.log("State to find app name from: ", tmpView) + } + + if (response.status !== 200) { + console.log("Status not 200 for oauth2 authentication"); + setFailed(true); + } else { + setFinished(true); + //setTimeout(() => { + // window.close(); + //}, 2500); + } + + return response.json(); + }) + .then((responseJson) => { + //setUserSettings(responseJson) + console.log("Resp: ", responseJson); + + if (responseJson.reason !== undefined) { + setResponse(responseJson.reason); + setFinished(true); + + } else { + const cursearch = typeof window === "undefined" || window.location === undefined ? "" : window.location.search; + var tmpView = new URLSearchParams(cursearch).get("error_description"); + if ( + tmpView !== undefined && + tmpView !== null && + tmpView.length > 0 + ) { + setResponse(tmpView) + } else { + tmpView = new URLSearchParams(cursearch).get("error"); + if ( + tmpView !== undefined && + tmpView !== null && + tmpView.length > 0 + ) { + setResponse(tmpView) + } + } + } + + }) + .catch((error) => { + console.log(error); + }); + + return + } + + console.log(appAuthData); fetch(globalUrl + "/api/v1/apps/authentication", { method: "PUT", diff --git a/frontend/src/views/SettingsPage.jsx b/frontend/src/views/SettingsPage.jsx index 2d284e60..468b8bc0 100644 --- a/frontend/src/views/SettingsPage.jsx +++ b/frontend/src/views/SettingsPage.jsx @@ -728,17 +728,19 @@ const Settings = (props) => {

Platform Earnings

- + {isCloud ? + + : null}
@@ -914,6 +916,64 @@ const Settings = (props) => { }); }; + const handleGithubConnection = () => { + console.log("GITHUB CONNECT WOO") + //result = RestClient.post('https://github.com/login/oauth/access_token', + + console.log("HOST: ", window.location.host); + console.log("HOST: ", window.location); + const redirectUri = isCloud + ? window.location.host === "localhost:3002" + ? "http%3A%2F%2Flocalhost:3002%2Fset_authentication" + : "https%3A%2F%2Fshuffler.io%2Fset_authentication" + : window.location.protocol === "http:" ? + `http%3A%2F%2F${window.location.host}%2Fset_authentication` + : + `https%3A%2F%2F${window.location.host}%2Fset_authentication` + + + const client_id = "3d272b1b782b100b1e61" + const username = userdata.id; + const scopes = "user:email"; + + const url = `https://github.com/login/oauth/authorize?access_type=offline&prompt=consent&client_id=${client_id}&redirect_uri=${redirectUri}&response_type=code&scope=${scopes}&state=username%3D${username}%26type%3Dgithub` + + console.log("URL: ", url); + + var newwin = window.open(url, "", "width=800,height=600"); + + // Check whether we got a callback somewhere + //var id = setInterval(function () { + // fetch( + // globalUrl + "/api/v1/triggers/gmail/" + selectedTrigger.id, + // { + // method: "GET", + // headers: { "content-type": "application/json" }, + // credentials: "include", + // } + // ) + // .then((response) => { + // if (response.status !== 200) { + // throw new Error("No trigger info :o!"); + // } + + // return response.json(); + // }) + // .then((responseJson) => { + // console.log("RESPONSE: "); + // setTriggerAuthentication(responseJson); + // clearInterval(id); + // newwin.close(); + // setGmailFolders(); + // }) + // .catch((error) => { + // console.log(error.toString()); + // }); + //}, 2500); + + //saveWorkflow(workflow); + } + const handleEthereumTokenCreation = async () => { const provider = await detectEthereumProvider(); if (!provider) {