/
metadata.go
52 lines (47 loc) · 1.46 KB
/
metadata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// DO NOT EDIT LOCAL SDK - USE v3 okta-sdk-golang FOR API CALLS THAT DO NOT EXIST IN LOCAL SDK
package sdk
import (
"context"
"encoding/xml"
"fmt"
"io"
"net/http"
"github.com/crewjam/saml"
)
func (m *APISupplement) GetSAMLMetadata(ctx context.Context, id, keyID string) ([]byte, *saml.EntityDescriptor, error) {
var query string
if keyID != "" {
query = fmt.Sprintf("?kid=%s", keyID)
}
return m.getXML(ctx, fmt.Sprintf("/api/v1/apps/%s/sso/saml/metadata%s", id, query))
}
func (m *APISupplement) GetSAMLIdpMetadata(ctx context.Context, id string) ([]byte, *saml.EntityDescriptor, error) {
return m.getXML(ctx, fmt.Sprintf("/api/v1/idps/%s/metadata.xml", id))
}
func (m *APISupplement) getXML(ctx context.Context, url string) ([]byte, *saml.EntityDescriptor, error) {
re := m.cloneRequestExecutor()
req, err := re.WithAccept("application/xml").NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, nil, err
}
metadataRoot := saml.EntityDescriptor{}
resp, err := re.Do(ctx, req, &metadataRoot)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
raw, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
// this means, that RequestExecutor didn't decode the data, so doing it manually
if metadataRoot.EntityID == "" {
copyRawBytes := make([]byte, len(raw))
copy(copyRawBytes, raw)
err = xml.Unmarshal(copyRawBytes, &metadataRoot)
if err != nil {
return nil, nil, err
}
}
return raw, &metadataRoot, nil
}