From 5a3249a80441122c298ab678ae720654def51e9f Mon Sep 17 00:00:00 2001 From: lovestaco Date: Mon, 24 Feb 2025 21:01:31 +0530 Subject: [PATCH 01/10] wasmbuild.sh --- wasmbuild.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasmbuild.sh b/wasmbuild.sh index e48a1fc5..19e8e537 100755 --- a/wasmbuild.sh +++ b/wasmbuild.sh @@ -1,5 +1,5 @@ #!/bin/bash GOOS=js GOARCH=wasm go build -a -gcflags=all="-l -B -wb=false" -ldflags="-w -s" -o static/main.wasm -cp /home/sreedeep/Downloads/Lama2/static/main.wasm /home/sreedeep/js-widget/dist/main.wasm +cp main.wasm /home/i3nux-mint/repos/liveapi/src/views/dist From b84f2b443822202e4eb1147ed27c8f665f8d8dfe Mon Sep 17 00:00:00 2001 From: lovestaco Date: Mon, 24 Feb 2025 21:35:12 +0530 Subject: [PATCH 02/10] build script --- wasmbuild.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wasmbuild.sh b/wasmbuild.sh index 19e8e537..9273287d 100755 --- a/wasmbuild.sh +++ b/wasmbuild.sh @@ -1,5 +1,5 @@ #!/bin/bash GOOS=js GOARCH=wasm go build -a -gcflags=all="-l -B -wb=false" -ldflags="-w -s" -o static/main.wasm -cp main.wasm /home/i3nux-mint/repos/liveapi/src/views/dist +cp static/main.wasm /home/i3nux-mint/repos/liveapi/src/views/dist From 98d20f5bba6bc60a74eced5f79b636471e20c115 Mon Sep 17 00:00:00 2001 From: lovestaco Date: Tue, 25 Feb 2025 20:50:32 +0530 Subject: [PATCH 03/10] print selected auth --- cmdgen/cmdgen.wasm.go | 80 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 9 deletions(-) diff --git a/cmdgen/cmdgen.wasm.go b/cmdgen/cmdgen.wasm.go index 6a1f1904..ad038e3f 100644 --- a/cmdgen/cmdgen.wasm.go +++ b/cmdgen/cmdgen.wasm.go @@ -3,26 +3,88 @@ package cmdgen import ( + "encoding/json" + "fmt" + "log" + "syscall/js" + "github.com/HexmosTech/gabs/v2" "github.com/HexmosTech/lama2/lama2cmd" - "syscall/js" ) func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, string) { httpv, url, jsonObj, headers, multipartBool, formBool := ConstructCommandHelper(parsedInput) res, stdinBody := assembleCmdString(httpv, url, jsonObj, headers, multipartBool, formBool, nil) + LaBearerAuthToken := js.Global().Get("liveapiManifest").String() + + // Parse JSON + parsedJson, err := gabs.ParseJSON([]byte(LaBearerAuthToken)) + if err != nil { + log.Fatal("Error parsing JSON:", err) + } + + // Get project count + projects, ok := parsedJson.Path("projects").Data().([]interface{}) + if !ok { + log.Fatal("Failed to parse projects") + } + + fmt.Println("Project count:", len(projects)) + + projectRoot := getProjectRoot() + if projectRoot == "" { + fmt.Println("DBGYYY: No project_root meta tag found") + return res, stdinBody + } + + fmt.Println("DBGYYY: projectRoot:", projectRoot) - LaBearerAuthToken := js.Global().Get("LaBearerAuthToken").String() - authHeaderExists := false - for _, header := range res { - if len(header) >= len("Authorization:") && header[:len("Authorization:")] == "Authorization:" { - authHeaderExists = true + // Find the matching project + for _, project := range projects { + projectMap, ok := project.(map[string]interface{}) + if !ok { + continue + } + + if projectMap["project_root"] == projectRoot { + authData, exists := projectMap["result"].(map[string]interface{})["auth"] + if !exists { + fmt.Println("DBGYYY: No auth data found for this project") + } else { + authArray, ok := authData.([]interface{}) + if !ok { + fmt.Println("DBGYYY: Auth data is not in expected format") + return res, stdinBody + } + + // Filter the auth methods where "selected" is true + for _, authEntry := range authArray { + authMap, ok := authEntry.(map[string]interface{}) + if !ok { + continue + } + + selected, exists := authMap["selected"].(bool) + if exists && selected { + authJSON, _ := json.MarshalIndent(authMap, "", " ") + fmt.Println("DBGYYY: Selected Auth Data:", string(authJSON)) + } + } + } break } } - if !authHeaderExists { - res = append(res, "Authorization: Bearer "+LaBearerAuthToken) - } return res, stdinBody } + + + +func getProjectRoot() string { + // Fetch project_root from the tag in the document + meta := js.Global().Get("document").Call("querySelector", `meta[name="project_root"]`) + if meta.IsNull() { + return "" + } + return meta.Call("getAttribute", "content").String() +} From 770b9de6283d0833c6ec28c4946e22705eecf96f Mon Sep 17 00:00:00 2001 From: lovestaco Date: Tue, 25 Feb 2025 20:52:26 +0530 Subject: [PATCH 04/10] retrive manifest instead of auth --- l2.wasm.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/l2.wasm.go b/l2.wasm.go index 15689cfd..588d429a 100644 --- a/l2.wasm.go +++ b/l2.wasm.go @@ -30,8 +30,8 @@ func main() { func wasmLamaPromise() js.Func { return js.FuncOf(func(this js.Value, args []js.Value) interface{} { - laBearerAuthToken := args[2].String() - js.Global().Set("LaBearerAuthToken", laBearerAuthToken) + liveapiManifest := args[2].String() + js.Global().Set("liveapiManifest", liveapiManifest) inputdata := args[0].String() handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} { resolve := args[0] From 948b9f8d7e6ab777135f65f68b87616784d8ed17 Mon Sep 17 00:00:00 2001 From: lovestaco Date: Tue, 25 Feb 2025 21:17:14 +0530 Subject: [PATCH 05/10] jwt working --- cmdgen/cmdgen.wasm.go | 65 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 5 deletions(-) diff --git a/cmdgen/cmdgen.wasm.go b/cmdgen/cmdgen.wasm.go index ad038e3f..92d1a1c4 100644 --- a/cmdgen/cmdgen.wasm.go +++ b/cmdgen/cmdgen.wasm.go @@ -11,10 +11,12 @@ import ( "github.com/HexmosTech/gabs/v2" "github.com/HexmosTech/lama2/lama2cmd" ) + func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, string) { httpv, url, jsonObj, headers, multipartBool, formBool := ConstructCommandHelper(parsedInput) res, stdinBody := assembleCmdString(httpv, url, jsonObj, headers, multipartBool, formBool, nil) + LaBearerAuthToken := js.Global().Get("liveapiManifest").String() // Parse JSON @@ -33,11 +35,13 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, projectRoot := getProjectRoot() if projectRoot == "" { - fmt.Println("DBGYYY: No project_root meta tag found") + fmt.Println("DBGYYZ: No project_root meta tag found") return res, stdinBody } - fmt.Println("DBGYYY: projectRoot:", projectRoot) + fmt.Println("DBGYYZ: projectRoot:", projectRoot) + + var selectedAuthHeader string // Find the matching project for _, project := range projects { @@ -49,11 +53,11 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, if projectMap["project_root"] == projectRoot { authData, exists := projectMap["result"].(map[string]interface{})["auth"] if !exists { - fmt.Println("DBGYYY: No auth data found for this project") + fmt.Println("DBGYYZ: No auth data found for this project") } else { authArray, ok := authData.([]interface{}) if !ok { - fmt.Println("DBGYYY: Auth data is not in expected format") + fmt.Println("DBGYYZ: Auth data is not in expected format") return res, stdinBody } @@ -67,13 +71,64 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, selected, exists := authMap["selected"].(bool) if exists && selected { authJSON, _ := json.MarshalIndent(authMap, "", " ") - fmt.Println("DBGYYY: Selected Auth Data:", string(authJSON)) + fmt.Println("DBGYYZ: Selected Auth Data:", string(authJSON)) + + // Construct Authorization header based on type + authType := authMap["type"].(string) + authValue := authMap["value"].(map[string]interface{}) + fmt.Println("DBGYYZ: authType:", authType) + fmt.Println("DBGYYZ: authValue:", authValue) + switch authType { + case "bearer-token": + if token, ok := authValue["token"].(string); ok { + fmt.Println("DBGYYZ: token:", token) + selectedAuthHeader = "Authorization: " + token + } + case "jwt": + if jwt, ok := authValue["jwt"].(string); ok { + fmt.Println("DBGYYZ: jwt:", jwt) + selectedAuthHeader = "Authorization: Bearer " + jwt + } + case "api-key": + if key, ok := authValue["key"].(string); ok { + fmt.Println("DBGYYZ: key:", key) + if value, ok := authValue["value"].(string); ok { + fmt.Println("DBGYYZ: value:", value) + selectedAuthHeader = key + ": " + value + } + } + case "basic-auth": + if username, ok := authValue["username"].(string); ok { + fmt.Println("DBGYYZ: username:", username) + if password, ok := authValue["password"].(string); ok { + fmt.Println("DBGYYZ: password:", password) + selectedAuthHeader = "Authorization: Basic " + username + ":" + password + } + } + } + break // Stop after the first selected auth method } } } break } } + fmt.Println("DBGYYZ: selectedAuthHeader:", selectedAuthHeader) + // Add the selected authentication method to headers if not already present + if selectedAuthHeader != "" { + authHeaderExists := false + for _, header := range res { + if len(header) >= len("Authorization:") && header[:len("Authorization:")] == "Authorization:" { + authHeaderExists = true + break + } + } + + if !authHeaderExists { + res = append(res, selectedAuthHeader) + fmt.Println("DBGYYZ: Added Auth Header:", selectedAuthHeader) + } + } return res, stdinBody } From 28bb29d4ba134d9684db7a703e3978442bd9cafa Mon Sep 17 00:00:00 2001 From: lovestaco Date: Tue, 25 Feb 2025 21:19:11 +0530 Subject: [PATCH 06/10] add basic auth functionality --- cmdgen/cmdgen.wasm.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cmdgen/cmdgen.wasm.go b/cmdgen/cmdgen.wasm.go index 92d1a1c4..8479333c 100644 --- a/cmdgen/cmdgen.wasm.go +++ b/cmdgen/cmdgen.wasm.go @@ -99,10 +99,10 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, } case "basic-auth": if username, ok := authValue["username"].(string); ok { - fmt.Println("DBGYYZ: username:", username) if password, ok := authValue["password"].(string); ok { - fmt.Println("DBGYYZ: password:", password) - selectedAuthHeader = "Authorization: Basic " + username + ":" + password + credentials := username + ":" + password + encoded := js.Global().Get("btoa").Invoke(credentials).String() + selectedAuthHeader = "Authorization: Basic " + encoded } } } From 1ffcac06204c45cf50ae84df8f4f81b4acbecf6f Mon Sep 17 00:00:00 2001 From: lovestaco Date: Wed, 26 Feb 2025 19:06:13 +0530 Subject: [PATCH 07/10] fix bearer auth --- cmdgen/cmdgen.wasm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmdgen/cmdgen.wasm.go b/cmdgen/cmdgen.wasm.go index 8479333c..297fbc83 100644 --- a/cmdgen/cmdgen.wasm.go +++ b/cmdgen/cmdgen.wasm.go @@ -82,7 +82,7 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, case "bearer-token": if token, ok := authValue["token"].(string); ok { fmt.Println("DBGYYZ: token:", token) - selectedAuthHeader = "Authorization: " + token + selectedAuthHeader = "Authorization: Bearer " + token } case "jwt": if jwt, ok := authValue["jwt"].(string); ok { From 8b691f7cad340c469c6ff6e8beefda2eef9454ec Mon Sep 17 00:00:00 2001 From: lovestaco Date: Wed, 26 Feb 2025 19:15:35 +0530 Subject: [PATCH 08/10] remove unecessary logs --- cmdgen/cmdgen.wasm.go | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/cmdgen/cmdgen.wasm.go b/cmdgen/cmdgen.wasm.go index 297fbc83..2ac040fe 100644 --- a/cmdgen/cmdgen.wasm.go +++ b/cmdgen/cmdgen.wasm.go @@ -3,8 +3,6 @@ package cmdgen import ( - "encoding/json" - "fmt" "log" "syscall/js" @@ -31,15 +29,12 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, log.Fatal("Failed to parse projects") } - fmt.Println("Project count:", len(projects)) projectRoot := getProjectRoot() if projectRoot == "" { - fmt.Println("DBGYYZ: No project_root meta tag found") return res, stdinBody } - fmt.Println("DBGYYZ: projectRoot:", projectRoot) var selectedAuthHeader string @@ -53,11 +48,9 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, if projectMap["project_root"] == projectRoot { authData, exists := projectMap["result"].(map[string]interface{})["auth"] if !exists { - fmt.Println("DBGYYZ: No auth data found for this project") } else { authArray, ok := authData.([]interface{}) if !ok { - fmt.Println("DBGYYZ: Auth data is not in expected format") return res, stdinBody } @@ -70,30 +63,21 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, selected, exists := authMap["selected"].(bool) if exists && selected { - authJSON, _ := json.MarshalIndent(authMap, "", " ") - fmt.Println("DBGYYZ: Selected Auth Data:", string(authJSON)) - // Construct Authorization header based on type authType := authMap["type"].(string) authValue := authMap["value"].(map[string]interface{}) - fmt.Println("DBGYYZ: authType:", authType) - fmt.Println("DBGYYZ: authValue:", authValue) switch authType { case "bearer-token": if token, ok := authValue["token"].(string); ok { - fmt.Println("DBGYYZ: token:", token) selectedAuthHeader = "Authorization: Bearer " + token } case "jwt": if jwt, ok := authValue["jwt"].(string); ok { - fmt.Println("DBGYYZ: jwt:", jwt) selectedAuthHeader = "Authorization: Bearer " + jwt } case "api-key": if key, ok := authValue["key"].(string); ok { - fmt.Println("DBGYYZ: key:", key) if value, ok := authValue["value"].(string); ok { - fmt.Println("DBGYYZ: value:", value) selectedAuthHeader = key + ": " + value } } @@ -113,7 +97,6 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, break } } - fmt.Println("DBGYYZ: selectedAuthHeader:", selectedAuthHeader) // Add the selected authentication method to headers if not already present if selectedAuthHeader != "" { authHeaderExists := false @@ -126,7 +109,6 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, if !authHeaderExists { res = append(res, selectedAuthHeader) - fmt.Println("DBGYYZ: Added Auth Header:", selectedAuthHeader) } } From 58e35ef950c6f526c530ef523b8923eb5aa909df Mon Sep 17 00:00:00 2001 From: lovestaco Date: Wed, 26 Feb 2025 19:27:19 +0530 Subject: [PATCH 09/10] build script --- wasmbuild.sh | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/wasmbuild.sh b/wasmbuild.sh index 9273287d..21353bc3 100755 --- a/wasmbuild.sh +++ b/wasmbuild.sh @@ -1,5 +1,8 @@ #!/bin/bash +export AWS_ACCESS_KEY_ID="" +export AWS_SECRET_ACCESS_KEY="" GOOS=js GOARCH=wasm go build -a -gcflags=all="-l -B -wb=false" -ldflags="-w -s" -o static/main.wasm -cp static/main.wasm /home/i3nux-mint/repos/liveapi/src/views/dist +# cp static/main.wasm /home/i3nux-mint/repos/liveapi/src/views +aws s3 cp static/main.wasm s3://temp-2d/temp/main.wasm --region ap-south-1 From 7695c809acc3d5518a79f6676d5495798a56cbd7 Mon Sep 17 00:00:00 2001 From: lovestaco Date: Wed, 26 Feb 2025 20:11:16 +0530 Subject: [PATCH 10/10] variable name change --- cmdgen/cmdgen.wasm.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmdgen/cmdgen.wasm.go b/cmdgen/cmdgen.wasm.go index 2ac040fe..02b852f3 100644 --- a/cmdgen/cmdgen.wasm.go +++ b/cmdgen/cmdgen.wasm.go @@ -15,10 +15,10 @@ func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, httpv, url, jsonObj, headers, multipartBool, formBool := ConstructCommandHelper(parsedInput) res, stdinBody := assembleCmdString(httpv, url, jsonObj, headers, multipartBool, formBool, nil) - LaBearerAuthToken := js.Global().Get("liveapiManifest").String() + manisfestData := js.Global().Get("liveapiManifest").String() // Parse JSON - parsedJson, err := gabs.ParseJSON([]byte(LaBearerAuthToken)) + parsedJson, err := gabs.ParseJSON([]byte(manisfestData)) if err != nil { log.Fatal("Error parsing JSON:", err) }