rsapi/auth.go
func (s *cookieSigner) Sign(req *http.Request) error {
	if time.Now().After(s.refreshAt) {
		authReq, authErr := s.builder.BuildLoginRequest(s.host)
		if authErr != nil {
			return authErr
		}
		resp, err := s.client.DoHidden(authReq)
		if err != nil {
			return err
		}
		url, err := extractRedirectURL(resp)
		if err != nil {
			return err
		}
		if url != nil {
			authReq, authErr = s.builder.BuildLoginRequest(url.Host)
			if authErr != nil {
				return authErr
			}
			s.host = url.Host
			req.Host = url.Host
			req.URL.Host = url.Host
			resp, err = s.client.DoHidden(authReq)
		}
		if err != nil {
			return fmt.Errorf("Authentication failed: %s", err)
		}
		if err := s.refresh(resp); err != nil {
			return err
		}
	}
	for _, c := range s.cookies {
		req.AddCookie(c)
	}
	req.Header.Set("X-Account", strconv.Itoa(s.accountID))
	return nil
}
func (s *cookieSigner) Sign(req *http.Request) error {
	if time.Now().After(s.refreshAt) {
		authReq, authErr := s.builder.BuildLoginRequest(s.host)
		if authErr != nil {
			return authErr
		}
		resp, err := s.client.DoHidden(authReq)
		if err != nil {
			return err
		}
		url, err := extractRedirectURL(resp)
		if err != nil {
			return err
		}
		if url != nil {
			authReq, authErr = s.builder.BuildLoginRequest(url.Host)
			if authErr != nil {
				return authErr
			}
			s.host = url.Host
			req.Host = url.Host
			req.URL.Host = url.Host
			resp, err = s.client.DoHidden(authReq)
		}
		if err != nil {
			return fmt.Errorf("Authentication failed: %s", err)
		}
		if err := s.refresh(resp); err != nil {
			return err
		}
	}
	for _, c := range s.cookies {
		req.AddCookie(c)
	}
	req.Header.Set("X-Account", strconv.Itoa(s.accountID))
	return nil
}
// Sign adds the username and password authorization cookies to the request.
// Checks the freshness of the session and creates a new one if needed.