Browse Source

NM-24: IDP UX improvements (#3583)

* fix(go): parse error message when url.Error.

* fix(go): suppress intermediate errors and return the root cause.
Vishal Dalwadi 1 month ago
parent
commit
062552170d
2 changed files with 28 additions and 2 deletions
  1. 2 2
      pro/idp/azure/azure.go
  2. 26 0
      pro/idp/google/google.go

+ 2 - 2
pro/idp/azure/azure.go

@@ -199,7 +199,7 @@ func (a *Client) getAccessToken() (string, error) {
 
 	resp, err := http.PostForm(tokenURL, data)
 	if err != nil {
-		return "", err
+		return "", errors.New("invalid credentials")
 	}
 	defer func() {
 		_ = resp.Body.Close()
@@ -208,7 +208,7 @@ func (a *Client) getAccessToken() (string, error) {
 	var tokenResp map[string]interface{}
 	err = json.NewDecoder(resp.Body).Decode(&tokenResp)
 	if err != nil {
-		return "", err
+		return "", errors.New("invalid credentials")
 	}
 
 	if token, ok := tokenResp["access_token"].(string); ok {

+ 26 - 0
pro/idp/google/google.go

@@ -11,6 +11,8 @@ import (
 	"google.golang.org/api/googleapi"
 	"google.golang.org/api/impersonate"
 	"google.golang.org/api/option"
+	"net/url"
+	"strings"
 )
 
 type Client struct {
@@ -84,6 +86,22 @@ func (g *Client) Verify() error {
 			return errors.New(gerr.Message)
 		}
 
+		var uerr *url.Error
+		if errors.As(err, &uerr) {
+			errMsg := strings.TrimSpace(uerr.Err.Error())
+			if strings.Contains(errMsg, "{") && strings.HasSuffix(errMsg, "}") {
+				// probably contains response json.
+				_, jsonBody, _ := strings.Cut(errMsg, "{")
+				jsonBody = "{" + jsonBody
+
+				var errResp errorResponse
+				err := json.Unmarshal([]byte(jsonBody), &errResp)
+				if err == nil && errResp.Error.Message != "" {
+					return errors.New(errResp.Error.Message)
+				}
+			}
+		}
+
 		return err
 	}
 
@@ -158,3 +176,11 @@ func (g *Client) GetGroups() ([]idp.Group, error) {
 
 	return retval, err
 }
+
+type errorResponse struct {
+	Error struct {
+		Code    int    `json:"code"`
+		Message string `json:"message"`
+		Status  string `json:"status"`
+	} `json:"error"`
+}