Skip to content

Commit 796abaa

Browse files
authored
Merge pull request #53 from HexmosTech/wasm-multi-auth-support
Wasm multi auth support
2 parents fb8cf62 + 7695c80 commit 796abaa

File tree

3 files changed

+113
-11
lines changed

3 files changed

+113
-11
lines changed

cmdgen/cmdgen.wasm.go

Lines changed: 107 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,125 @@
33
package cmdgen
44

55
import (
6+
"log"
7+
"syscall/js"
8+
69
"github.com/HexmosTech/gabs/v2"
710
"github.com/HexmosTech/lama2/lama2cmd"
8-
"syscall/js"
911
)
12+
1013

1114
func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, string) {
1215
httpv, url, jsonObj, headers, multipartBool, formBool := ConstructCommandHelper(parsedInput)
1316
res, stdinBody := assembleCmdString(httpv, url, jsonObj, headers, multipartBool, formBool, nil)
1417

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+
}
2097
break
2198
}
2299
}
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+
}
23109

24-
if !authHeaderExists {
25-
res = append(res, "Authorization: Bearer "+LaBearerAuthToken)
110+
if !authHeaderExists {
111+
res = append(res, selectedAuthHeader)
112+
}
26113
}
114+
27115
return res, stdinBody
28116
}
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+
}

l2.wasm.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ func main() {
3030

3131
func wasmLamaPromise() js.Func {
3232
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
33-
laBearerAuthToken := args[2].String()
34-
js.Global().Set("LaBearerAuthToken", laBearerAuthToken)
33+
liveapiManifest := args[2].String()
34+
js.Global().Set("liveapiManifest", liveapiManifest)
3535
inputdata := args[0].String()
3636
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
3737
resolve := args[0]

wasmbuild.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
2+
export AWS_ACCESS_KEY_ID=""
3+
export AWS_SECRET_ACCESS_KEY=""
24
GOOS=js GOARCH=wasm go build -a -gcflags=all="-l -B -wb=false" -ldflags="-w -s" -o static/main.wasm
3-
cp /home/sreedeep/Downloads/Lama2/static/main.wasm /home/sreedeep/js-widget/dist/main.wasm
5+
# cp static/main.wasm /home/i3nux-mint/repos/liveapi/src/views
6+
aws s3 cp static/main.wasm s3://temp-2d/temp/main.wasm --region ap-south-1
47

58

0 commit comments

Comments
 (0)