diff options
Diffstat (limited to 'backend/internal/ibd')
-rw-r--r-- | backend/internal/ibd/check_ibd_username.go | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/backend/internal/ibd/check_ibd_username.go b/backend/internal/ibd/check_ibd_username.go new file mode 100644 index 0000000..03d2640 --- /dev/null +++ b/backend/internal/ibd/check_ibd_username.go @@ -0,0 +1,59 @@ +package ibd + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "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) + if err != nil { + return false, err + } + + if resp.Result.StatusCode == http.StatusUnauthorized { + return false, nil + } else if resp.Result.StatusCode != http.StatusOK { + return false, fmt.Errorf( + "unexpected status code %d: %s", + resp.Result.StatusCode, + resp.Result.Content, + ) + } + return true, nil +} |