-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata.go
90 lines (77 loc) · 2.1 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"bytes"
"encoding/xml"
"errors"
"github.com/crewjam/saml"
xrv "github.com/mattermost/xml-roundtrip-validator"
"io"
"log"
"net/http"
"os"
"path/filepath"
)
// https://github.com/crewjam/saml/blob/main/samlidp/util.go
func getSPMetadata(r io.Reader) (spMetadata *saml.EntityDescriptor, err error) {
var data []byte
if data, err = io.ReadAll(r); err != nil {
return nil, err
}
spMetadata = &saml.EntityDescriptor{}
if err := xrv.Validate(bytes.NewBuffer(data)); err != nil {
return nil, err
}
if err := xml.Unmarshal(data, &spMetadata); err != nil {
if err.Error() == "expected element type <EntityDescriptor> but have <EntitiesDescriptor>" {
entities := &saml.EntitiesDescriptor{}
if err := xml.Unmarshal(data, &entities); err != nil {
return nil, err
}
for _, e := range entities.EntityDescriptors {
if len(e.SPSSODescriptors) > 0 {
return &e, nil
}
}
// there were no SPSSODescriptors in the response
return nil, errors.New("metadata contained no service provider metadata")
}
return nil, err
}
return spMetadata, nil
}
type fileServiceProvider struct {
serviceProviders map[string]*saml.EntityDescriptor
}
func NewFileServiceProvider(glob string) *fileServiceProvider {
matches, err := filepath.Glob(glob)
if err != nil {
log.Fatalf("cannot glob service providers: %v", err)
return nil
}
p := &fileServiceProvider{
serviceProviders: map[string]*saml.EntityDescriptor{},
}
for _, m := range matches {
f, err := os.Open(m)
if err != nil {
log.Fatalf("cannot open service provider file: %v", err)
return nil
}
defer f.Close()
metadata, err := getSPMetadata(f)
if err != nil {
log.Fatalf("cannot parse service provider file: %v", err)
return nil
}
p.serviceProviders[metadata.EntityID] = metadata
log.Printf("loaded service provider: %s", metadata.EntityID)
}
return p
}
func (p *fileServiceProvider) GetServiceProvider(_ *http.Request, serviceProviderID string) (*saml.EntityDescriptor, error) {
rv, ok := p.serviceProviders[serviceProviderID]
if !ok {
return nil, os.ErrNotExist
}
return rv, nil
}