package ibd import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" ) const ( checkUsernameUrl = "https://sso.accounts.dowjones.com/getuser" ) func (c *Client) CheckIBDUsername(ctx context.Context, username string) (bool, error) { cfg, err := c.getLoginPage(ctx) if err != nil { return false, err } return c.checkIBDUsername(ctx, cfg, username) } func (c *Client) checkIBDUsername(ctx context.Context, cfg *authConfig, username string) (bool, error) { body := map[string]string{ "username": username, "csrf": cfg.ExtraParams.Csrf, } bodyJson, err := json.Marshal(body) if err != nil { return false, err } req, err := http.NewRequestWithContext(ctx, http.MethodPost, checkUsernameUrl, bytes.NewReader(bodyJson)) if err != nil { return false, err } req.Header.Set("Content-Type", "application/json") req.Header.Set("X-REMOTE-USER", username) req.Header.Set("X-REQUEST-EDITIONID", "IBD-EN_US") req.Header.Set("X-REQUEST-SCHEME", "https") resp, err := c.Do(req, withExpectedStatuses(http.StatusOK, http.StatusUnauthorized)) if err != nil { return false, err } defer func(Body io.ReadCloser) { _ = Body.Close() }(resp.Body) if resp.StatusCode == http.StatusUnauthorized { return false, nil } else if resp.StatusCode != http.StatusOK { contentBytes, err := io.ReadAll(resp.Body) if err != nil { return false, fmt.Errorf("failed to read response body: %w", err) } content := string(contentBytes) return false, fmt.Errorf( "unexpected status code %d: %s", resp.StatusCode, content, ) } return true, nil }