From 0665ceb5cbb9ed422f4b1fd76f01cc64420cbb6e Mon Sep 17 00:00:00 2001 From: Tobasco99 Date: Mon, 15 Jul 2024 13:31:22 +0200 Subject: [PATCH 1/5] make login required --- src/static-app.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/static-app.js b/src/static-app.js index 67733c6f..c87d920e 100644 --- a/src/static-app.js +++ b/src/static-app.js @@ -179,7 +179,7 @@ class StaticApp extends LitElement { { path: "/modeling", component: "main-page", - action: async () => await import("./main.js"), + action: async () => this.handleModelingRoute(), }, ]); // if on modeling route, upload meta model @@ -192,6 +192,17 @@ class StaticApp extends LitElement { } } + async handleModelingRoute() { + const token = localStorage.getItem("access_token"); + if (!token) { + this.alertMessage = "Please sign in to access all functionalities."; + Router.go("/"); + return; + } + + await import("./main.js"); + } + _onChangeButtonClicked() { const input = document.querySelector("#yjsRoomInput").value; const currentRoomName = Common.getYjsRoom(); @@ -214,6 +225,13 @@ class StaticApp extends LitElement { */ goToModeling(subRoute) { const currentPath = window.location.pathname; + const token = localStorage.getItem("access_token"); + if (!token) { + alert("You need to be signed in to access this feature."); + this.alertMessage = + "Please sign in to access all functionalities."; + return; + } if (currentPath === "/") { Router.go(`modeling#${subRoute}`); } From 153f291f7a4bfbf991d38023f54251f99b2b7153 Mon Sep 17 00:00:00 2001 From: Tobasco99 Date: Tue, 16 Jul 2024 11:56:29 +0200 Subject: [PATCH 2/5] add dummy for pw space --- src/bot.manager.widget.js | 93 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 92 insertions(+), 1 deletion(-) diff --git a/src/bot.manager.widget.js b/src/bot.manager.widget.js index c7e45613..ce5afd41 100644 --- a/src/bot.manager.widget.js +++ b/src/bot.manager.widget.js @@ -25,6 +25,8 @@ const keyboardEnterPrevent = { class BotManagerWidget extends LitElement { storeNameInputEditor; sbfManagerEndpointEditor; + storePasswordInputEditor; + botModels = []; guidance = null; @@ -373,6 +375,59 @@ class BotManagerWidget extends LitElement { } } + storePassword(password) { + let spacePassword = ""; + if (typeof password === "string") { + spacePassword = password; + } else { + spacePassword = y.getText("storePassword").toString(); + } + var endpoint = y.getText("sbfManager").toString(); + var saveStatus = $("#saveStatus"); + const spinner = $("#saveStatusSpinner"); + const btn = $("#store-password"); + spinner.show(); + saveStatus.text("Saving..."); + btn.prop("disabled", true); + + if (spacePassword) { + //todo get space name + fetch(endpoint + "/secure/" + "SPACE_NAME", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ password: spacePassword }), + }) + .then((response) => { + if (response.ok) { + if (!password) alert("Your space password has been successfully saved"); + this.updateMenu(); + } else { + throw new Error( + "Your space password could not be set up. Make sure that the SBF endpoint is correct." + ); + } + }) + .catch((error) => { + console.error(error); + }) + .finally(() => { + spinner.hide(); + btn.prop("disabled", false); + }); + } else { + if (!spacePassword) { + alert("The space password is invalid."); + } else { + alert("The space password is empty."); + } + spinner.hide(); + btn.prop("disabled", false); + cleanStatus("saveStatus"); + } + } + firstUpdated() { super.firstUpdated(); const instance = getInstance({ @@ -418,6 +473,20 @@ class BotManagerWidget extends LitElement { } new QuillBinding(y.getText("storeName"), this.storeNameInputEditor); + this.storePasswordInputEditor = new Quill( + document.querySelector("#storePasswordInput"), + { + modules: { toolbar: false, keyboard: keyboardEnterPrevent }, + cursors: false, + placeholder: "Set a password for this space...", + theme: "snow", + } + ); + if (!this.storePasswordInputEditor) { + throw new Error("Could not find quill editor"); + } + new QuillBinding(y.getText("storePassword"), this.storePasswordInputEditor); + this.updateMenu(); // function showAlert(message, type) { @@ -466,7 +535,29 @@ class BotManagerWidget extends LitElement { } - +
+

Space Settings

+
+
+ +
+
+ +
+
+
+
+

Bot Operations

From b4b5a1040997020444d7789c219f9093b3b68984 Mon Sep 17 00:00:00 2001 From: Tobasco99 Date: Wed, 17 Jul 2024 09:35:15 +0200 Subject: [PATCH 3/5] add missing parameters --- src/bot.manager.widget.js | 58 +++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/src/bot.manager.widget.js b/src/bot.manager.widget.js index ce5afd41..b8e162fe 100644 --- a/src/bot.manager.widget.js +++ b/src/bot.manager.widget.js @@ -375,8 +375,44 @@ class BotManagerWidget extends LitElement { } } + removePassword() { + const userInfo = JSON.parse(localStorage.getItem("userInfo")); + const loginName = userInfo.loginName; + var endpoint = y.getText("sbfManager").toString(); + var removePassword = $("#removePassword"); + const spinner = $("#removePasswordSpinner"); + const btn = $("#remove-password"); + spinner.show(); + removePassword.text("Removing..."); + btn.prop("disabled", true); + + const currentRoomName = Common.getSyncmetaSpaceName(); + fetch(endpoint + "/secure/"+currentRoomName+"/"+ loginName, { + method: "DELETE", + }) + .then((response) => { + if (response.ok) { + alert("Your space password has been successfully removed"); + this.updateMenu(); + } else { + throw new Error( + "Your space password could not be removed. Make sure that the SBF endpoint is correct and you set the password." + ); + } + }) + .catch((error) => { + console.error(error); + }) + .finally(() => { + spinner.hide(); + btn.prop("disabled", false); + }); + } + storePassword(password) { let spacePassword = ""; + const userInfo = JSON.parse(localStorage.getItem("userInfo")); + const loginName = userInfo.loginName; if (typeof password === "string") { spacePassword = password; } else { @@ -391,13 +427,14 @@ class BotManagerWidget extends LitElement { btn.prop("disabled", true); if (spacePassword) { - //todo get space name - fetch(endpoint + "/secure/" + "SPACE_NAME", { + const currentRoomName = Common.getSyncmetaSpaceName(); + var pw = btoa(spacePassword) + fetch(endpoint + "/secure/"+currentRoomName+"/"+ loginName, { method: "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ password: spacePassword }), + body: JSON.stringify({ password: pw }), }) .then((response) => { if (response.ok) { @@ -536,14 +573,14 @@ class BotManagerWidget extends LitElement {
-

Space Settings

+

Secure Space

+
From a06b7f2f348b48a4069dfeda6a8259395a319fd7 Mon Sep 17 00:00:00 2001 From: Tobasco99 Date: Tue, 24 Sep 2024 18:27:01 +0200 Subject: [PATCH 4/5] add auth header to modeling --- src/bot.manager.widget.js | 155 ++++---------------------------------- 1 file changed, 14 insertions(+), 141 deletions(-) diff --git a/src/bot.manager.widget.js b/src/bot.manager.widget.js index b8e162fe..c80bb13d 100644 --- a/src/bot.manager.widget.js +++ b/src/bot.manager.widget.js @@ -25,7 +25,6 @@ const keyboardEnterPrevent = { class BotManagerWidget extends LitElement { storeNameInputEditor; sbfManagerEndpointEditor; - storePasswordInputEditor; botModels = []; @@ -96,7 +95,12 @@ class BotManagerWidget extends LitElement { $(loadStatus).text("Loading..."); spinner.show(); btn.prop("disabled", true); - fetch(endpoint + "/models/" + name) + const accessToken = localStorage.getItem("access_token"); + fetch(endpoint + "/models/" + name, { + headers: { + "Authorization": `Bearer ${accessToken}` + } + }) .then((response) => { if ( response.ok && @@ -195,7 +199,8 @@ class BotManagerWidget extends LitElement { sendStatus.text("Sending..."); spinner.show(); btn.prop("disabled", true); - + + const accessToken = localStorage.getItem("access_token"); var xhr = new XMLHttpRequest(); xhr.onload = function () { if (xhr.status == 200) { @@ -219,6 +224,7 @@ class BotManagerWidget extends LitElement { xhr.open("POST", endpoint + "/bots"); xhr.setRequestHeader("Content-Type", "application/json"); + xhr.setRequestHeader("Authorization", `Bearer ${accessToken}`); xhr.send(JSON.stringify(model)); let botName; const botNode = Object.values(model["nodes"]).find( @@ -270,6 +276,7 @@ class BotManagerWidget extends LitElement { spinner.show(); btn.prop("disabled", true); + const accessToken = localStorage.getItem("access_token"); var xhr = new XMLHttpRequest(); var agentId = ""; xhr.onload = function () { @@ -279,6 +286,7 @@ class BotManagerWidget extends LitElement { agentId = JSON.parse(xhr.response)[instanceName][botName]["id"]; xhr2.open("DELETE", endpoint + "/bots/" + agentId); xhr2.setRequestHeader("Content-Type", "application/json"); + xhr2.setRequestHeader("Authorization", `Bearer ${accessToken}`); // delete the chosen bot xhr2.send(JSON.stringify({ messengers: messengers })); } catch (error) { @@ -318,6 +326,7 @@ class BotManagerWidget extends LitElement { // first fetch the deployed bots xhr.open("GET", endpoint + "/bots"); xhr.setRequestHeader("Content-Type", "application/json"); + xhr.setRequestHeader("Authorization", `Bearer ${accessToken}`); xhr.send(); } @@ -337,11 +346,13 @@ class BotManagerWidget extends LitElement { storeStatus.text("Storing..."); btn.prop("disabled", true); + const accessToken = localStorage.getItem("access_token"); if (botName && model) { fetch(endpoint + "/models/" + botName, { method: "POST", headers: { "Content-Type": "application/json", + "Authorization": `Bearer ${accessToken}` }, body: JSON.stringify(model), }) @@ -375,96 +386,6 @@ class BotManagerWidget extends LitElement { } } - removePassword() { - const userInfo = JSON.parse(localStorage.getItem("userInfo")); - const loginName = userInfo.loginName; - var endpoint = y.getText("sbfManager").toString(); - var removePassword = $("#removePassword"); - const spinner = $("#removePasswordSpinner"); - const btn = $("#remove-password"); - spinner.show(); - removePassword.text("Removing..."); - btn.prop("disabled", true); - - const currentRoomName = Common.getSyncmetaSpaceName(); - fetch(endpoint + "/secure/"+currentRoomName+"/"+ loginName, { - method: "DELETE", - }) - .then((response) => { - if (response.ok) { - alert("Your space password has been successfully removed"); - this.updateMenu(); - } else { - throw new Error( - "Your space password could not be removed. Make sure that the SBF endpoint is correct and you set the password." - ); - } - }) - .catch((error) => { - console.error(error); - }) - .finally(() => { - spinner.hide(); - btn.prop("disabled", false); - }); - } - - storePassword(password) { - let spacePassword = ""; - const userInfo = JSON.parse(localStorage.getItem("userInfo")); - const loginName = userInfo.loginName; - if (typeof password === "string") { - spacePassword = password; - } else { - spacePassword = y.getText("storePassword").toString(); - } - var endpoint = y.getText("sbfManager").toString(); - var saveStatus = $("#saveStatus"); - const spinner = $("#saveStatusSpinner"); - const btn = $("#store-password"); - spinner.show(); - saveStatus.text("Saving..."); - btn.prop("disabled", true); - - if (spacePassword) { - const currentRoomName = Common.getSyncmetaSpaceName(); - var pw = btoa(spacePassword) - fetch(endpoint + "/secure/"+currentRoomName+"/"+ loginName, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ password: pw }), - }) - .then((response) => { - if (response.ok) { - if (!password) alert("Your space password has been successfully saved"); - this.updateMenu(); - } else { - throw new Error( - "Your space password could not be set up. Make sure that the SBF endpoint is correct." - ); - } - }) - .catch((error) => { - console.error(error); - }) - .finally(() => { - spinner.hide(); - btn.prop("disabled", false); - }); - } else { - if (!spacePassword) { - alert("The space password is invalid."); - } else { - alert("The space password is empty."); - } - spinner.hide(); - btn.prop("disabled", false); - cleanStatus("saveStatus"); - } - } - firstUpdated() { super.firstUpdated(); const instance = getInstance({ @@ -510,20 +431,6 @@ class BotManagerWidget extends LitElement { } new QuillBinding(y.getText("storeName"), this.storeNameInputEditor); - this.storePasswordInputEditor = new Quill( - document.querySelector("#storePasswordInput"), - { - modules: { toolbar: false, keyboard: keyboardEnterPrevent }, - cursors: false, - placeholder: "Set a password for this space...", - theme: "snow", - } - ); - if (!this.storePasswordInputEditor) { - throw new Error("Could not find quill editor"); - } - new QuillBinding(y.getText("storePassword"), this.storePasswordInputEditor); - this.updateMenu(); // function showAlert(message, type) { @@ -572,40 +479,6 @@ class BotManagerWidget extends LitElement { } -
-

Secure Space

-
-
- -
-
- - -
-
-
-
-

Bot Operations

From af88d0552d41a946186e4f664aba4939cd5ce660 Mon Sep 17 00:00:00 2001 From: Tobasco99 Date: Tue, 24 Sep 2024 18:51:02 +0200 Subject: [PATCH 5/5] add auth to training --- src/bot.manager.widget.js | 7 ++++++- src/model-training.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/bot.manager.widget.js b/src/bot.manager.widget.js index c80bb13d..d339d7fc 100644 --- a/src/bot.manager.widget.js +++ b/src/bot.manager.widget.js @@ -52,8 +52,13 @@ class BotManagerWidget extends LitElement { if (!endpoint) { return; } + const accessToken = localStorage.getItem("access_token"); - fetch(endpoint + "/models/") + fetch(endpoint + "/models/", { + headers: { + "Authorization": `Bearer ${accessToken}` + } + }) .then((response) => { if ( response.ok && diff --git a/src/model-training.js b/src/model-training.js index 14cd6938..5ade7ace 100644 --- a/src/model-training.js +++ b/src/model-training.js @@ -309,6 +309,9 @@ class ModelTraining extends LitElement { markdownTrainingData: _this.editor.getText(), }), contentType: "application/json", + headers: { + "Authorization": "Bearer " + localStorage.getItem("access_token"), + }, success: function (data, textStatus, jqXHR) { $(_this.htmlQuery("#trainingStatus")).text(data); }, @@ -413,6 +416,9 @@ class ModelTraining extends LitElement { type: "GET", url: trainingStatusUrl, contentType: "text/plain", + headers: { + "Authorization": "Bearer " + localStorage.getItem("access_token"), + }, success: function (data, textStatus, jqXHR) { $(_this.htmlQuery("#trainingStatus")).text(data); }, @@ -435,6 +441,9 @@ class ModelTraining extends LitElement { $.ajax({ type: "POST", url: trainingStatusUrl + name, + headers: { + "Authorization": "Bearer " + localStorage.getItem("access_token"), + }, data: trainingData, contentType: "text/plain", success: function (data, textStatus, jqXHR) { @@ -460,6 +469,9 @@ class ModelTraining extends LitElement { type: "GET", url: trainingStatusUrl + name, contentType: "text/plain", + headers: { + "Authorization": "Bearer " + localStorage.getItem("access_token"), + }, success: function (data, textStatus, jqXHR) { $(_this.htmlQuery("#trainingStatus")).text("Data loaded."); _this.editor.setText(data); @@ -480,6 +492,9 @@ class ModelTraining extends LitElement { type: "GET", url: trainingStatusUrl + "/training/", contentType: "application/json", + headers: { + "Authorization": "Bearer " + localStorage.getItem("access_token"), + }, success: function (data, textStatus, jqXHR) { if (textStatus !== "success") { return;