forked from aliyun/saml2alibabacloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaml.go
157 lines (125 loc) · 4.61 KB
/
saml.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package saml2alibabacloud
import (
"fmt"
"strconv"
"github.com/beevik/etree"
)
const (
assertionTag = "Assertion"
attributeStatementTag = "AttributeStatement"
attributeTag = "Attribute"
attributeValueTag = "AttributeValue"
responseTag = "Response"
)
//ErrMissingElement is the error type that indicates an element and/or attribute is
//missing. It provides a structured error that can be more appropriately acted
//upon.
type ErrMissingElement struct {
Tag, Attribute string
}
//ErrMissingAssertion indicates that an appropriate assertion element could not
//be found in the SAML Response
var (
ErrMissingAssertion = ErrMissingElement{Tag: assertionTag}
)
func (e ErrMissingElement) Error() string {
if e.Attribute != "" {
return fmt.Sprintf("missing %s attribute on %s element", e.Attribute, e.Tag)
}
return fmt.Sprintf("missing %s element", e.Tag)
}
// ExtractSessionDuration this will attempt to extract a session duration from the assertion
// see https://www.alibabacloud.com/help/doc-detail/110614.htm
func ExtractSessionDuration(data []byte) (int64, error) {
doc := etree.NewDocument()
if err := doc.ReadFromBytes(data); err != nil {
return 0, err
}
assertionElement := doc.FindElement(".//Assertion")
if assertionElement == nil {
return 0, ErrMissingAssertion
}
// log.Printf("tag: %s", assertionElement.Tag)
//Get the actual assertion attributes
attributeStatement := assertionElement.FindElement(childPath(assertionElement.Space, attributeStatementTag))
if attributeStatement == nil {
return 0, ErrMissingElement{Tag: attributeStatementTag}
}
attributes := attributeStatement.FindElements(childPath(assertionElement.Space, attributeTag))
for _, attribute := range attributes {
if attribute.SelectAttrValue("Name", "") != "https://www.aliyun.com/SAML-Role/Attributes/SessionDuration" {
continue
}
atributeValues := attribute.FindElements(childPath(assertionElement.Space, attributeValueTag))
for _, attrValue := range atributeValues {
return strconv.ParseInt(attrValue.Text(), 10, 64)
}
}
// TODO need to check `SessionNotOnOrAfter` attribute
return 0, nil
}
// ExtractDestinationURL will find the Destination URL to POST the SAML assertion to.
// This is necessary to support custom endpoints such as AlibabaCloud International without requiring
// hardcoded endpoints on the saml2alibabacloud side.
func ExtractDestinationURL(data []byte) (string, error) {
doc := etree.NewDocument()
if err := doc.ReadFromBytes(data); err != nil {
return "", err
}
rootElement := doc.Root()
if rootElement == nil {
return "", ErrMissingElement{Tag: responseTag}
}
destination := rootElement.SelectAttrValue("Destination", "none")
if destination == "none" {
// If Destination attribute is not found in Response (root) element,
// get the Recipient attribute from the SubjectConfirmationData element.
subjectConfirmationDataElement := doc.FindElement(".//SubjectConfirmationData")
if subjectConfirmationDataElement == nil {
return "", ErrMissingElement{Tag: responseTag}
}
destination = subjectConfirmationDataElement.SelectAttrValue("Recipient", "none")
if destination == "none" {
return "", ErrMissingElement{Tag: responseTag}
}
}
return destination, nil
}
// ExtractRamRoles given an assertion document extract the AlibabaCloud RAM roles
func ExtractRamRoles(data []byte) ([]string, error) {
ramRoles := []string{}
doc := etree.NewDocument()
if err := doc.ReadFromBytes(data); err != nil {
return ramRoles, err
}
// log.Printf("root tag: %s", doc.Root().Tag)
assertionElement := doc.FindElement(".//Assertion")
if assertionElement == nil {
return nil, ErrMissingAssertion
}
// log.Printf("tag: %s", assertionElement.Tag)
//Get the actual assertion attributes
attributeStatement := assertionElement.FindElement(childPath(assertionElement.Space, attributeStatementTag))
if attributeStatement == nil {
return nil, ErrMissingElement{Tag: attributeStatementTag}
}
// log.Printf("tag: %s", attributeStatement.Tag)
attributes := attributeStatement.FindElements(childPath(assertionElement.Space, attributeTag))
for _, attribute := range attributes {
if attribute.SelectAttrValue("Name", "") != "https://www.aliyun.com/SAML-Role/Attributes/Role" {
continue
}
atributeValues := attribute.FindElements(childPath(assertionElement.Space, attributeValueTag))
for _, attrValue := range atributeValues {
ramRoles = append(ramRoles, attrValue.Text())
}
}
return ramRoles, nil
}
func childPath(space, tag string) string {
if space == "" {
return "./" + tag
}
//log.Printf("query = %s", "./"+space+":"+tag)
return "./" + space + ":" + tag
}