Skip to content

Commit 1c222e2

Browse files
committed
feat: add FetchMetadataByEntityID
1 parent f777964 commit 1c222e2

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

metadata.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ type EntityDescriptor struct {
5858
AdditionalMetadataLocations []string `xml:"AdditionalMetadataLocation"`
5959
}
6060

61+
type EntityDescriptorArray struct {
62+
EntityDescriptorElements []EntityDescriptor `xml:"EntityDescriptor"`
63+
}
64+
6165
// MarshalXML implements xml.Marshaler
6266
func (m EntityDescriptor) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
6367
type Alias EntityDescriptor

samlsp/fetch_metadata.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,45 @@ func ParseMetadata(data []byte) (*saml.EntityDescriptor, error) {
4949
return entity, nil
5050
}
5151

52+
// Use this if metadataURL returns an array of EntityDescriptos
53+
func FetchMetadataByEntityID(ctx context.Context, httpClient *http.Client, metadataURL url.URL, entityID string) (*saml.EntityDescriptor, error) {
54+
req, err := http.NewRequest("GET", metadataURL.String(), nil)
55+
if err != nil {
56+
return nil, err
57+
}
58+
req = req.WithContext(ctx)
59+
60+
resp, err := httpClient.Do(req)
61+
if err != nil {
62+
return nil, err
63+
}
64+
defer resp.Body.Close()
65+
if resp.StatusCode >= 400 {
66+
return nil, httperr.Response(*resp)
67+
}
68+
69+
data, err := ioutil.ReadAll(resp.Body)
70+
if err != nil {
71+
return nil, err
72+
}
73+
74+
entities := &saml.EntityDescriptorArray{}
75+
76+
err = xml.Unmarshal(data, entities)
77+
78+
if err != nil {
79+
return nil, err
80+
}
81+
82+
for i := range entities.EntityDescriptorElements {
83+
if entities.EntityDescriptorElements[i].EntityID == entityID {
84+
return &entities.EntityDescriptorElements[i], nil
85+
}
86+
}
87+
88+
return nil, errors.New("entityid not found")
89+
}
90+
5291
// FetchMetadata returns metadata from an IDP metadata URL.
5392
func FetchMetadata(ctx context.Context, httpClient *http.Client, metadataURL url.URL) (*saml.EntityDescriptor, error) {
5493
req, err := http.NewRequest("GET", metadataURL.String(), nil)

0 commit comments

Comments
 (0)