postSOAPMessage sends envelope to location with the SAML SOAP binding, using httpClient and ctx, and returns the body of the response, which must have status 200 and be at most limit bytes.
(ctx context.Context, httpClient *http.Client, location string, envelope *etree.Element, limit int64)
| 127 | // using httpClient and ctx, and returns the body of the response, which must |
| 128 | // have status 200 and be at most limit bytes. |
| 129 | func postSOAPMessage(ctx context.Context, httpClient *http.Client, location string, envelope *etree.Element, limit int64) ([]byte, error) { |
| 130 | requestBody, err := elementToBytes(envelope) |
| 131 | if err != nil { |
| 132 | return nil, err |
| 133 | } |
| 134 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, location, bytes.NewReader(requestBody)) //nolint:gosec // G704: location is an endpoint from the peer's metadata, never from the request being served |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | req.Header.Set("Content-Type", soapContentType) |
| 139 | req.Header.Set("SOAPAction", soapActionSAML) |
| 140 | setNoCacheHeaders(req.Header) |
| 141 | |
| 142 | resp, err := httpClient.Do(req) //nolint:gosec // G704: location is an endpoint from the peer's metadata, never from the request being served |
| 143 | if err != nil { |
| 144 | return nil, err |
| 145 | } |
| 146 | defer func() { |
| 147 | if err := resp.Body.Close(); err != nil { |
| 148 | logger.DefaultLogger.Printf("Error while closing SOAP response body: %v", err) |
| 149 | } |
| 150 | }() |
| 151 | // A SOAP fault is sent with a 500 status (SOAP 1.1 §6.2); it is not |
| 152 | // parsed, since it carries no SAML response. |
| 153 | if resp.StatusCode != http.StatusOK { |
| 154 | return nil, fmt.Errorf("HTTP status %d (%s)", resp.StatusCode, resp.Status) |
| 155 | } |
| 156 | buf, err := readLimited(resp.Body, limit) |
| 157 | if err != nil { |
| 158 | return nil, fmt.Errorf("response %w", err) |
| 159 | } |
| 160 | return buf, nil |
| 161 | } |
| 162 | |
| 163 | // readLimited reads r to the end, failing if it holds more than limit bytes. |
| 164 | func readLimited(r io.Reader, limit int64) ([]byte, error) { |
no test coverage detected