registerWithIDP uploads the service provider's metadata to the example identity provider's /services API.
(idpMetadataURL url.URL, samlSP *samlsp.Middleware)
| 294 | // registerWithIDP uploads the service provider's metadata to the example |
| 295 | // identity provider's /services API. |
| 296 | func registerWithIDP(idpMetadataURL url.URL, samlSP *samlsp.Middleware) error { |
| 297 | spMetadataBuf, err := xml.MarshalIndent(samlSP.ServiceProvider.Metadata(), "", " ") |
| 298 | if err != nil { |
| 299 | return fmt.Errorf("cannot marshal SP metadata: %w", err) |
| 300 | } |
| 301 | spURL := idpMetadataURL |
| 302 | spURL.Path = "/services/sp" |
| 303 | spURL.RawQuery = "" |
| 304 | |
| 305 | ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) |
| 306 | defer cancel() |
| 307 | req, err := http.NewRequestWithContext(ctx, http.MethodPut, spURL.String(), bytes.NewReader(spMetadataBuf)) |
| 308 | if err != nil { |
| 309 | return err |
| 310 | } |
| 311 | req.Header.Set("Content-Type", "text/xml") |
| 312 | resp, err := http.DefaultClient.Do(req) |
| 313 | if err != nil { |
| 314 | return fmt.Errorf("cannot register with the IDP: %w", err) |
| 315 | } |
| 316 | defer resp.Body.Close() //nolint:errcheck // nothing useful to do with a close error here |
| 317 | if resp.StatusCode < 200 || resp.StatusCode > 299 { |
| 318 | body, _ := io.ReadAll(io.LimitReader(resp.Body, 1024)) |
| 319 | return fmt.Errorf("cannot register with the IDP: %s: %s", resp.Status, strings.TrimSpace(string(body))) |
| 320 | } |
| 321 | return nil |
| 322 | } |