ParseMetadata parses arbitrary SAML IDP metadata. Note: this is needed because IDP metadata is sometimes wrapped in an , and sometimes the top level element is an .
(data []byte)
| 21 | // an <EntitiesDescriptor>, and sometimes the top level element is an |
| 22 | // <EntityDescriptor>. |
| 23 | func ParseMetadata(data []byte) (*saml.EntityDescriptor, error) { |
| 24 | entity := &saml.EntityDescriptor{} |
| 25 | |
| 26 | if err := xrv.Validate(bytes.NewBuffer(data)); err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | |
| 30 | err := xml.Unmarshal(data, entity) |
| 31 | |
| 32 | // this comparison is ugly, but it is how the error is generated in encoding/xml |
| 33 | if err != nil && err.Error() == "expected element type <EntityDescriptor> but have <EntitiesDescriptor>" { |
| 34 | entities := &saml.EntitiesDescriptor{} |
| 35 | if err := xml.Unmarshal(data, entities); err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | |
| 39 | for i, e := range entities.EntityDescriptors { |
| 40 | if len(e.IDPSSODescriptors) > 0 { |
| 41 | return &entities.EntityDescriptors[i], nil |
| 42 | } |
| 43 | } |
| 44 | return nil, errors.New("no entity found with IDPSSODescriptor") |
| 45 | } |
| 46 | if err != nil { |
| 47 | return nil, err |
| 48 | } |
| 49 | return entity, nil |
| 50 | } |
| 51 | |
| 52 | // FetchMetadata returns metadata from an IDP metadata URL. |
| 53 | func FetchMetadata(ctx context.Context, httpClient *http.Client, metadataURL url.URL) (*saml.EntityDescriptor, error) { |
no test coverage detected