diff --git a/cmdgen/cmdgen.wasm.go b/cmdgen/cmdgen.wasm.go index 6a1f1904..02b852f3 100644 --- a/cmdgen/cmdgen.wasm.go +++ b/cmdgen/cmdgen.wasm.go @@ -3,26 +3,125 @@ package cmdgen import ( + "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("LaBearerAuthToken").String() - authHeaderExists := false - for _, header := range res { - if len(header) >= len("Authorization:") && header[:len("Authorization:")] == "Authorization:" { - authHeaderExists = true + manisfestData := js.Global().Get("liveapiManifest").String() + + // Parse JSON + parsedJson, err := gabs.ParseJSON([]byte(manisfestData)) + 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") + } + + + projectRoot := getProjectRoot() + if projectRoot == "" { + return res, stdinBody + } + + + var selectedAuthHeader string + + // 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 { + } else { + authArray, ok := authData.([]interface{}) + if !ok { + 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 { + // Construct Authorization header based on type + authType := authMap["type"].(string) + authValue := authMap["value"].(map[string]interface{}) + switch authType { + case "bearer-token": + if token, ok := authValue["token"].(string); ok { + selectedAuthHeader = "Authorization: Bearer " + token + } + case "jwt": + if jwt, ok := authValue["jwt"].(string); ok { + selectedAuthHeader = "Authorization: Bearer " + jwt + } + case "api-key": + if key, ok := authValue["key"].(string); ok { + if value, ok := authValue["value"].(string); ok { + selectedAuthHeader = key + ": " + value + } + } + case "basic-auth": + if username, ok := authValue["username"].(string); ok { + if password, ok := authValue["password"].(string); ok { + credentials := username + ":" + password + encoded := js.Global().Get("btoa").Invoke(credentials).String() + selectedAuthHeader = "Authorization: Basic " + encoded + } + } + } + break // Stop after the first selected auth method + } + } + } break } } + // 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, "Authorization: Bearer "+LaBearerAuthToken) + if !authHeaderExists { + res = append(res, selectedAuthHeader) + } } + 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() +} 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] diff --git a/wasmbuild.sh b/wasmbuild.sh index e48a1fc5..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 /home/sreedeep/Downloads/Lama2/static/main.wasm /home/sreedeep/js-widget/dist/main.wasm +# 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