-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.go
77 lines (68 loc) · 1.94 KB
/
parser.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
package samlutils
import (
"github.com/beevik/etree"
)
type SamlMetadata struct {
EntityId string
Certificate string
LoginUrl string
LogoutUrl string
RedirectUrl string
}
// ParseIdpMetadata This function tries to parse a string as SAML metadata file
// and returns the parsed content.
func ParseIdpMetadata(content string) SamlMetadata {
doc := etree.NewDocument()
if err := doc.ReadFromString(content); err != nil {
return SamlMetadata{}
}
return SamlMetadata{
EntityId: findEntityID(doc),
Certificate: findCertificate(doc),
LoginUrl: findLoginUrl(doc),
LogoutUrl: findLogoutUrl(doc),
RedirectUrl: findRedirectUrl(doc),
}
}
func (s SamlMetadata) IsValid() bool {
if s.LoginUrl != "" && s.EntityId != "" && s.Certificate != "" {
return true
}
return false
}
func findEntityID(d *etree.Document) string {
// This should also find namespaced entity descriptor i.e. md:EntityDescriptor
entityDesc := d.FindElement("//EntityDescriptor")
if entityDesc != nil {
return entityDesc.SelectAttrValue("entityID", "")
}
return ""
}
func findLoginUrl(d *etree.Document) string {
ssoService := d.FindElement("//SingleSignOnService[@Binding='urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST']")
if ssoService != nil {
return ssoService.SelectAttrValue("Location", "")
}
return ""
}
func findLogoutUrl(d *etree.Document) string {
ssoService := d.FindElement("//SingleLogoutService[@Binding='urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect']")
if ssoService != nil {
return ssoService.SelectAttrValue("Location", "")
}
return ""
}
func findRedirectUrl(d *etree.Document) string {
ssoService := d.FindElement("//SingleSignOnService[@Binding='urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect']")
if ssoService != nil {
return ssoService.SelectAttrValue("Location", "")
}
return ""
}
func findCertificate(d *etree.Document) string {
key := d.FindElement("//X509Certificate")
if key != nil {
return key.Text()
}
return ""
}