Skip to content

Commit

Permalink
[management] extend readZitadelError to be used for requestJWTToken
Browse files Browse the repository at this point in the history
  • Loading branch information
adasauce committed Sep 27, 2024
1 parent 1176f2a commit a54e173
Showing 1 changed file with 24 additions and 17 deletions.
41 changes: 24 additions & 17 deletions management/server/idp/zitadel.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type ZitadelCredentials struct {
appMetrics telemetry.AppMetrics
}

// zitadelErrorResponse represents an API error containing a more detailed internal code and message.
type zitadelErrorResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Expand Down Expand Up @@ -102,6 +103,23 @@ type zitadelUserResponse struct {
PasswordlessRegistration zitadelPasswordlessRegistration `json:"passwordlessRegistration"`
}

// readZitadelError parses errors returned by the zitadel APIs from a response.
func readZitadelError(body io.ReadCloser) error {
bodyBytes, err := io.ReadAll(body)
if err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}

helper := JsonParser{}
var zitadelErr zitadelErrorResponse
err = helper.Unmarshal(bodyBytes, &zitadelErr)
if err != nil {
return fmt.Errorf("error unparsable body: %s", string(bodyBytes))
}

return fmt.Errorf("error code: %d message: %s", zitadelErr.Code, zitadelErr.Message)
}

// NewZitadelManager creates a new instance of the ZitadelManager.
func NewZitadelManager(config ZitadelClientConfig, appMetrics telemetry.AppMetrics) (*ZitadelManager, error) {
httpTransport := http.DefaultTransport.(*http.Transport).Clone()
Expand Down Expand Up @@ -181,7 +199,8 @@ func (zc *ZitadelCredentials) requestJWTToken(ctx context.Context) (*http.Respon
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unable to get zitadel token, statusCode %d", resp.StatusCode)
zErr := readZitadelError(resp.Body)
return nil, fmt.Errorf("unable to get zitadel token, statusCode %d, zitadel: %w", resp.StatusCode, zErr)
}

return resp, nil
Expand Down Expand Up @@ -494,10 +513,9 @@ func (zm *ZitadelManager) post(ctx context.Context, resource string, body string
zm.appMetrics.IDPMetrics().CountRequestStatusError()
}

bodyBytes, _ := io.ReadAll(resp.Body)
zErr := zm.readZitadelError(bodyBytes)
zErr := readZitadelError(resp.Body)

return bodyBytes, fmt.Errorf("unable to post %s, statusCode %d, zitadel: %w", reqURL, resp.StatusCode, zErr)
return nil, fmt.Errorf("unable to post %s, statusCode %d, zitadel: %w", reqURL, resp.StatusCode, zErr)
}

return io.ReadAll(resp.Body)
Expand Down Expand Up @@ -569,25 +587,14 @@ func (zm *ZitadelManager) get(ctx context.Context, resource string, q url.Values
zm.appMetrics.IDPMetrics().CountRequestStatusError()
}

bodyBytes, _ := io.ReadAll(resp.Body)
zErr := zm.readZitadelError(bodyBytes)
zErr := readZitadelError(resp.Body)

return bodyBytes, fmt.Errorf("unable to get %s, statusCode %d, zitadel: %w", reqURL, resp.StatusCode, zErr)
return nil, fmt.Errorf("unable to get %s, statusCode %d, zitadel: %w", reqURL, resp.StatusCode, zErr)
}

return io.ReadAll(resp.Body)
}

func (zm *ZitadelManager) readZitadelError(errorBody []byte) error {
var zitadelErr zitadelErrorResponse
err := zm.helper.Unmarshal(errorBody, &zitadelErr)
if err != nil {
return fmt.Errorf("error unparsable body: %s", errorBody)
}

return fmt.Errorf("error code: %d message: %s", zitadelErr.Code, zitadelErr.Message)
}

// userData construct user data from zitadel profile.
func (zp zitadelProfile) userData() *UserData {
var (
Expand Down

0 comments on commit a54e173

Please sign in to comment.