|
3 | 3 | package cmdgen |
4 | 4 |
|
5 | 5 | import ( |
| 6 | + "log" |
| 7 | + "syscall/js" |
| 8 | + |
6 | 9 | "github.com/HexmosTech/gabs/v2" |
7 | 10 | "github.com/HexmosTech/lama2/lama2cmd" |
8 | | - "syscall/js" |
9 | 11 | ) |
| 12 | + |
10 | 13 |
|
11 | 14 | func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, string) { |
12 | 15 | httpv, url, jsonObj, headers, multipartBool, formBool := ConstructCommandHelper(parsedInput) |
13 | 16 | res, stdinBody := assembleCmdString(httpv, url, jsonObj, headers, multipartBool, formBool, nil) |
14 | 17 |
|
15 | | - LaBearerAuthToken := js.Global().Get("LaBearerAuthToken").String() |
16 | | - authHeaderExists := false |
17 | | - for _, header := range res { |
18 | | - if len(header) >= len("Authorization:") && header[:len("Authorization:")] == "Authorization:" { |
19 | | - authHeaderExists = true |
| 18 | + manisfestData := js.Global().Get("liveapiManifest").String() |
| 19 | + |
| 20 | + // Parse JSON |
| 21 | + parsedJson, err := gabs.ParseJSON([]byte(manisfestData)) |
| 22 | + if err != nil { |
| 23 | + log.Fatal("Error parsing JSON:", err) |
| 24 | + } |
| 25 | + |
| 26 | + // Get project count |
| 27 | + projects, ok := parsedJson.Path("projects").Data().([]interface{}) |
| 28 | + if !ok { |
| 29 | + log.Fatal("Failed to parse projects") |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + projectRoot := getProjectRoot() |
| 34 | + if projectRoot == "" { |
| 35 | + return res, stdinBody |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + var selectedAuthHeader string |
| 40 | + |
| 41 | + // Find the matching project |
| 42 | + for _, project := range projects { |
| 43 | + projectMap, ok := project.(map[string]interface{}) |
| 44 | + if !ok { |
| 45 | + continue |
| 46 | + } |
| 47 | + |
| 48 | + if projectMap["project_root"] == projectRoot { |
| 49 | + authData, exists := projectMap["result"].(map[string]interface{})["auth"] |
| 50 | + if !exists { |
| 51 | + } else { |
| 52 | + authArray, ok := authData.([]interface{}) |
| 53 | + if !ok { |
| 54 | + return res, stdinBody |
| 55 | + } |
| 56 | + |
| 57 | + // Filter the auth methods where "selected" is true |
| 58 | + for _, authEntry := range authArray { |
| 59 | + authMap, ok := authEntry.(map[string]interface{}) |
| 60 | + if !ok { |
| 61 | + continue |
| 62 | + } |
| 63 | + |
| 64 | + selected, exists := authMap["selected"].(bool) |
| 65 | + if exists && selected { |
| 66 | + // Construct Authorization header based on type |
| 67 | + authType := authMap["type"].(string) |
| 68 | + authValue := authMap["value"].(map[string]interface{}) |
| 69 | + switch authType { |
| 70 | + case "bearer-token": |
| 71 | + if token, ok := authValue["token"].(string); ok { |
| 72 | + selectedAuthHeader = "Authorization: Bearer " + token |
| 73 | + } |
| 74 | + case "jwt": |
| 75 | + if jwt, ok := authValue["jwt"].(string); ok { |
| 76 | + selectedAuthHeader = "Authorization: Bearer " + jwt |
| 77 | + } |
| 78 | + case "api-key": |
| 79 | + if key, ok := authValue["key"].(string); ok { |
| 80 | + if value, ok := authValue["value"].(string); ok { |
| 81 | + selectedAuthHeader = key + ": " + value |
| 82 | + } |
| 83 | + } |
| 84 | + case "basic-auth": |
| 85 | + if username, ok := authValue["username"].(string); ok { |
| 86 | + if password, ok := authValue["password"].(string); ok { |
| 87 | + credentials := username + ":" + password |
| 88 | + encoded := js.Global().Get("btoa").Invoke(credentials).String() |
| 89 | + selectedAuthHeader = "Authorization: Basic " + encoded |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + break // Stop after the first selected auth method |
| 94 | + } |
| 95 | + } |
| 96 | + } |
20 | 97 | break |
21 | 98 | } |
22 | 99 | } |
| 100 | + // Add the selected authentication method to headers if not already present |
| 101 | + if selectedAuthHeader != "" { |
| 102 | + authHeaderExists := false |
| 103 | + for _, header := range res { |
| 104 | + if len(header) >= len("Authorization:") && header[:len("Authorization:")] == "Authorization:" { |
| 105 | + authHeaderExists = true |
| 106 | + break |
| 107 | + } |
| 108 | + } |
23 | 109 |
|
24 | | - if !authHeaderExists { |
25 | | - res = append(res, "Authorization: Bearer "+LaBearerAuthToken) |
| 110 | + if !authHeaderExists { |
| 111 | + res = append(res, selectedAuthHeader) |
| 112 | + } |
26 | 113 | } |
| 114 | + |
27 | 115 | return res, stdinBody |
28 | 116 | } |
| 117 | + |
| 118 | + |
| 119 | + |
| 120 | +func getProjectRoot() string { |
| 121 | + // Fetch project_root from the <meta> tag in the document |
| 122 | + meta := js.Global().Get("document").Call("querySelector", `meta[name="project_root"]`) |
| 123 | + if meta.IsNull() { |
| 124 | + return "" |
| 125 | + } |
| 126 | + return meta.Call("getAttribute", "content").String() |
| 127 | +} |
0 commit comments