From f977da910ce9c7d83b1478d5d88cdee8f129a2e3 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Fri, 6 Oct 2023 14:49:29 +0200 Subject: [PATCH 1/2] Serve prompt data on demand --- internal/cli/universal_login_customize.go | 43 ++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/internal/cli/universal_login_customize.go b/internal/cli/universal_login_customize.go index 4291e6832..73111f7c5 100644 --- a/internal/cli/universal_login_customize.go +++ b/internal/cli/universal_login_customize.go @@ -2,6 +2,7 @@ package cli import ( "context" + "encoding/json" "fmt" "net" "net/http" @@ -40,7 +41,7 @@ type ( promptData struct { Language string `json:"language"` Prompt string `json:"prompt"` - CustomText map[string]map[string]interface{} `json:"custom_text"` + CustomText map[string]map[string]interface{} `json:"custom_tex,omitempty"` } webSocketHandler struct { @@ -49,6 +50,11 @@ type ( api *auth0.API brandingData *universalLoginBrandingData } + + webSocketMessage struct { + Type string `json:"type"` + Payload json.RawMessage `json:"payload"` + } ) func customizeUniversalLoginCmd(cli *cli) *cobra.Command { @@ -326,6 +332,41 @@ func (h *webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { h.shutdown() return } + + for { + var message webSocketMessage + if err := connection.ReadJSON(&message); err != nil { + h.display.Errorf("failed to read WebSocket message: %v", err) + continue + } + + switch message.Type { + case "fetch_prompt": + var promptToFetch promptData + if err := json.Unmarshal(message.Payload, &promptToFetch); err != nil { + h.display.Errorf("failed to unmarshal %q payload: %v", message.Type, err) + continue + } + + promptToSend, err := fetchPromptCustomTextWithDefaults( + r.Context(), + h.api, + promptToFetch.Prompt, + promptToFetch.Language, + ) + if err != nil { + h.display.Errorf("failed to fetch custom text for prompt: %v", err) + continue + } + + if err = connection.WriteJSON(promptToSend); err != nil { + h.display.Errorf("failed to send prompt data message: %v", err) + continue + } + case "save_branding": + h.display.Warnf("not yet implemented") + } + } } func checkOriginFunc(r *http.Request) bool { From 071b5c22b735817a104827f6ce3e13ec9ed54aca Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Fri, 6 Oct 2023 17:17:24 +0200 Subject: [PATCH 2/2] Send consistent message format to web app --- internal/cli/universal_login_customize.go | 47 ++++++++++++++++++----- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/internal/cli/universal_login_customize.go b/internal/cli/universal_login_customize.go index 73111f7c5..522b5170c 100644 --- a/internal/cli/universal_login_customize.go +++ b/internal/cli/universal_login_customize.go @@ -20,7 +20,12 @@ import ( "github.com/auth0/auth0-cli/internal/display" ) -const webAppURL = "http://localhost:5173" +const ( + webAppURL = "http://localhost:5173" + loadBrandingMessageType = "LOAD_BRANDING" + fetchPromptMessageType = "FETCH_PROMPT" + saveBrandingMessageType = "SAVE_BRANDING" +) type ( universalLoginBrandingData struct { @@ -41,7 +46,7 @@ type ( promptData struct { Language string `json:"language"` Prompt string `json:"prompt"` - CustomText map[string]map[string]interface{} `json:"custom_tex,omitempty"` + CustomText map[string]map[string]interface{} `json:"custom_text,omitempty"` } webSocketHandler struct { @@ -320,15 +325,27 @@ func (h *webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { connection, err := upgrader.Upgrade(w, r, nil) if err != nil { - h.display.Errorf("error accepting WebSocket connection: %v", err) + h.display.Errorf("failed to upgrade the connection to the WebSocket protocol: %v", err) h.shutdown() return } connection.SetReadLimit(1e+6) // 1 MB. - if err = connection.WriteJSON(h.brandingData); err != nil { - h.display.Errorf("failed to write json message: %v", err) + payload, err := json.Marshal(&h.brandingData) + if err != nil { + h.display.Errorf("failed to encode the branding data to json: %v", err) + h.shutdown() + return + } + + loadBrandingMsg := webSocketMessage{ + Type: loadBrandingMessageType, + Payload: payload, + } + + if err = connection.WriteJSON(loadBrandingMsg); err != nil { + h.display.Errorf("failed to send branding data message: %v", err) h.shutdown() return } @@ -341,10 +358,10 @@ func (h *webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } switch message.Type { - case "fetch_prompt": + case fetchPromptMessageType: var promptToFetch promptData if err := json.Unmarshal(message.Payload, &promptToFetch); err != nil { - h.display.Errorf("failed to unmarshal %q payload: %v", message.Type, err) + h.display.Errorf("failed to unmarshal fetch prompt payload: %v", err) continue } @@ -359,11 +376,23 @@ func (h *webSocketHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { continue } - if err = connection.WriteJSON(promptToSend); err != nil { + payload, err := json.Marshal(promptToSend) + if err != nil { + h.display.Errorf("failed to encode the branding data to json: %v", err) + h.shutdown() + return + } + + fetchPromptMsg := webSocketMessage{ + Type: fetchPromptMessageType, + Payload: payload, + } + + if err = connection.WriteJSON(fetchPromptMsg); err != nil { h.display.Errorf("failed to send prompt data message: %v", err) continue } - case "save_branding": + case saveBrandingMessageType: h.display.Warnf("not yet implemented") } }