diff options
Diffstat (limited to 'backend/internal/ibd/auth.go')
-rw-r--r-- | backend/internal/ibd/auth.go | 54 |
1 files changed, 38 insertions, 16 deletions
diff --git a/backend/internal/ibd/auth.go b/backend/internal/ibd/auth.go index 7dff3a7..f09f3f7 100644 --- a/backend/internal/ibd/auth.go +++ b/backend/internal/ibd/auth.go @@ -7,6 +7,7 @@ import ( "encoding/json" "errors" "fmt" + "io" "net/http" "strings" @@ -51,16 +52,23 @@ func (c *Client) getLoginPage(ctx context.Context) (*authConfig, error) { if err != nil { return nil, err } - - if resp.Result.StatusCode != http.StatusOK { + defer func(Body io.ReadCloser) { + _ = Body.Close() + }(resp.Body) + + if resp.StatusCode != http.StatusOK { + content, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } return nil, fmt.Errorf( "unexpected status code %d: %s", - resp.Result.StatusCode, - resp.Result.Content, + resp.StatusCode, + string(content), ) } - node, err := html.Parse(strings.NewReader(resp.Result.Content)) + node, err := html.Parse(resp.Body) if err != nil { return nil, err } @@ -113,18 +121,25 @@ func (c *Client) sendAuthRequest(ctx context.Context, cfg *authConfig, username, if err != nil { return "", "", err } + defer func(Body io.ReadCloser) { + _ = Body.Close() + }(resp.Body) - if resp.Result.StatusCode == http.StatusUnauthorized { + if resp.StatusCode == http.StatusUnauthorized { return "", "", ErrBadCredentials - } else if resp.Result.StatusCode != http.StatusOK { + } else if resp.StatusCode != http.StatusOK { + content, err := io.ReadAll(resp.Body) + if err != nil { + return "", "", fmt.Errorf("failed to read response body: %w", err) + } return "", "", fmt.Errorf( "unexpected status code %d: %s", - resp.Result.StatusCode, - resp.Result.Content, + resp.StatusCode, + string(content), ) } - node, err := html.Parse(strings.NewReader(resp.Result.Content)) + node, err := html.Parse(resp.Body) if err != nil { return "", "", err } @@ -145,19 +160,26 @@ func (c *Client) sendPostAuth(ctx context.Context, token, params string) (*http. if err != nil { return nil, err } - - if resp.Result.StatusCode != http.StatusOK { + defer func(Body io.ReadCloser) { + _ = Body.Close() + }(resp.Body) + + if resp.StatusCode != http.StatusOK { + content, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response body: %w", err) + } return nil, fmt.Errorf( "unexpected status code %d: %s", - resp.Result.StatusCode, - resp.Result.Content, + resp.StatusCode, + string(content), ) } // Extract cookie - for _, cookie := range resp.Result.Cookies { + for _, cookie := range resp.Cookies() { if cookie.Name == cookieName { - return cookie.ToHTTPCookie() + return cookie, nil } } |