forked from segmentio/aws-okta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
83 lines (70 loc) · 1.75 KB
/
utils.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
package lib
import (
"encoding/base64"
"encoding/xml"
"fmt"
"strings"
"github.com/segmentio/aws-okta/lib/saml"
"golang.org/x/net/html"
)
//TODO: Move those functions into saml package
func GetRoleFromSAML(resp *saml.Response, profileARN string) (string, string, error) {
for _, a := range resp.Assertion.AttributeStatement.Attributes {
if strings.HasSuffix(a.Name, "SAML/Attributes/Role") {
for _, v := range a.AttributeValues {
tokens := strings.Split(v.Value, ",")
if len(tokens) != 2 {
continue
}
principalARN, profile := tokens[0], tokens[1]
if profile == profileARN {
return principalARN, profile, nil
}
}
}
}
return "", "", fmt.Errorf("Role '%s' not authorized by Okta. Contact Okta admin to make sure that the AWS app is configured properly.", profileARN)
}
func ParseSAML(body []byte, resp *SAMLAssertion) (err error) {
var val string
var data []byte
var doc *html.Node
doc, err = html.Parse(strings.NewReader(string(body)))
if err != nil {
return
}
val, _ = GetNode(doc)
if val != "" {
resp.RawData = []byte(val)
val = strings.Replace(val, "+", "+", -1)
val = strings.Replace(val, "=", "=", -1)
data, err = base64.StdEncoding.DecodeString(val)
if err != nil {
return
}
}
err = xml.Unmarshal(data, &resp.Resp)
return
}
func GetNode(n *html.Node) (val string, node *html.Node) {
var isSAML bool
if n.Type == html.ElementNode && n.Data == "input" {
for _, a := range n.Attr {
if a.Key == "name" && a.Val == "SAMLResponse" {
isSAML = true
}
if a.Key == "value" && isSAML {
val = a.Val
}
}
}
if node == nil || val == "" {
for c := n.FirstChild; c != nil; c = c.NextSibling {
val, node = GetNode(c)
if val != "" {
return
}
}
}
return
}