-
Notifications
You must be signed in to change notification settings - Fork 0
/
saml_metadata.go
110 lines (84 loc) · 3.3 KB
/
saml_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package certmonitor
import (
"context"
"crypto/x509"
"encoding/base64"
"net/url"
"strings"
"github.com/crewjam/saml/samlsp"
)
// GetSAMLMetadataCertificates returns the X509 certificates from the SAML metadata url
func (certMonitor *CertMonitor) GetSAMLMetadataCertificates(metadataURL string) ([]*x509.Certificate, error) {
return certMonitor.getSAMLMetadataCertificates(metadataURL)
}
func (certMonitor *CertMonitor) getSAMLMetadataCertificates(metadataURL string) ([]*x509.Certificate, error) {
idpMetadataURL, err := url.Parse(metadataURL)
if err != nil {
certMonitor.logger.Error("Pasring metadata Url", "metadataURL", metadataURL)
return nil, err
}
// get http client from CertMonitor Config
client := certMonitor.GetHttpClientWithConfiguration()
idpMetadata, err := samlsp.FetchMetadata(context.Background(), &client,
*idpMetadataURL)
if err != nil {
certMonitor.logger.Error("Error fetching metadata", "metadataURL", metadataURL, "err", err)
return nil, err
}
// list of X509 Certificate extracted from SAML Metadata
var samlCerts []*x509.Certificate
// Process IDP Descriptor
for _, idpSSODescriptors := range idpMetadata.IDPSSODescriptors {
for _, KeyDescriptors := range idpSSODescriptors.KeyDescriptors {
// go over each certs from KeyInfo and convert into X509Certificates
for _, c := range KeyDescriptors.KeyInfo.X509Data.X509Certificates {
cert, err := certMonitor.getCertificateFromSAMLKeyDescriptorData(c.Data)
if err != nil {
certMonitor.logger.Error("Could not parse X509 Certificate from keydescriptor", "metadataURL", metadataURL, "err", err)
// continue processing
continue
}
// add X509 Cert to list
samlCerts = append(samlCerts, cert)
}
}
}
// Process IDP Descriptor
for _, spSSODescriptors := range idpMetadata.SPSSODescriptors {
for _, KeyDescriptors := range spSSODescriptors.KeyDescriptors {
// go over each certs from KeyInfo and convert into X509Certificates
for _, c := range KeyDescriptors.KeyInfo.X509Data.X509Certificates {
cert, err := certMonitor.getCertificateFromSAMLKeyDescriptorData(c.Data)
if err != nil {
certMonitor.logger.Error("Could not parse X509 Certificate from keydescriptor", "metadataURL", metadataURL, "err", err)
// continue processing
continue
}
// add X509 Cert to list
samlCerts = append(samlCerts, cert)
}
}
}
return samlCerts, nil
}
func (certMonitor *CertMonitor) getCertificateFromSAMLKeyDescriptorData(x509String string) (*x509.Certificate, error) {
certMonitor.logger.Debug("KeyInfo raw x509 cert", "x509String", x509String)
// Format X509 Base64 PEM
x509String = strings.ReplaceAll(x509String, "\n", "")
x509String = strings.ReplaceAll(x509String, "\r", "")
x509String = strings.ReplaceAll(x509String, " ", "")
certMonitor.logger.Debug("KeyInfo formatted x509 cert", "x509String", x509String)
// base64 decode PEM formatted X509
x509DecodedByte, err := base64.StdEncoding.DecodeString(x509String)
if err != nil {
certMonitor.logger.Error("Could not parse PEM to X509", "x509String", x509String, "err", err)
return nil, err
}
// Parse X509
cert, err := x509.ParseCertificate(x509DecodedByte)
if err != nil {
certMonitor.logger.Error("Could not parse PEM to X509", "x509String", x509String, "err", err)
return nil, err
}
return cert, nil
}