@@ -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.
5392func 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