forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adfs.go
146 lines (119 loc) · 3.5 KB
/
adfs.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
package saml2aws
import (
"bytes"
"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
"golang.org/x/net/publicsuffix"
)
// ADFSClient wrapper around ADFS enabling authentication and retrieval of assertions
type ADFSClient struct {
client *http.Client
}
// NewADFSClient create a new ADFS client
func NewADFSClient(skipVerify bool) (*ADFSClient, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify},
}
options := &cookiejar.Options{
PublicSuffixList: publicsuffix.List,
}
jar, err := cookiejar.New(options)
if err != nil {
return nil, err
}
client := &http.Client{Transport: tr, Jar: jar}
return &ADFSClient{
client: client,
}, nil
}
// Authenticate authenticate to ADFS and return the data from the body of the SAML assertion.
func (ac *ADFSClient) Authenticate(loginDetails *LoginDetails) (string, error) {
var authSubmitURL string
var samlAssertion string
authForm := url.Values{}
adfsURL := fmt.Sprintf("https://%s/adfs/ls/IdpInitiatedSignOn.aspx?loginToRp=urn:amazon:webservices", loginDetails.Hostname)
res, err := ac.client.Get(adfsURL)
if err != nil {
return samlAssertion, errors.Wrap(err, "error retieving form")
}
doc, err := goquery.NewDocumentFromResponse(res)
if err != nil {
return samlAssertion, errors.Wrap(err, "failed to build document from response")
}
doc.Find("input").Each(func(i int, s *goquery.Selection) {
updateFormData(authForm, s, loginDetails)
})
doc.Find("form").Each(func(i int, s *goquery.Selection) {
action, ok := s.Attr("action")
if !ok {
return
}
authSubmitURL = action
})
if authSubmitURL == "" {
return samlAssertion, fmt.Errorf("unable to locate IDP authentication form submit URL")
}
//log.Printf("id authentication url: %s", authSubmitURL)
req, err := http.NewRequest("POST", authSubmitURL, strings.NewReader(authForm.Encode()))
if err != nil {
return samlAssertion, errors.Wrap(err, "error building authentication request")
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, err = ac.client.Do(req)
if err != nil {
return samlAssertion, errors.Wrap(err, "error retieving login form")
}
//log.Printf("res code = %v status = %s", res.StatusCode, res.Status)
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return samlAssertion, errors.Wrap(err, "error retieving body")
}
doc, err = goquery.NewDocumentFromReader(bytes.NewBuffer(data))
if err != nil {
return samlAssertion, errors.Wrap(err, "error parsing document")
}
doc.Find("input").Each(func(i int, s *goquery.Selection) {
name, ok := s.Attr("name")
if !ok {
log.Fatalf("unable to locate IDP authentication form submit URL")
}
if name == "SAMLResponse" {
val, ok := s.Attr("value")
if !ok {
log.Fatalf("unable to locate saml assertion value")
}
samlAssertion = val
}
})
return samlAssertion, nil
}
func updateFormData(authForm url.Values, s *goquery.Selection, user *LoginDetails) {
name, ok := s.Attr("name")
// log.Printf("name = %s ok = %v", name, ok)
if !ok {
return
}
lname := strings.ToLower(name)
if strings.Contains(lname, "user") {
authForm.Add(name, user.Username)
} else if strings.Contains(lname, "email") {
authForm.Add(name, user.Username)
} else if strings.Contains(lname, "pass") {
authForm.Add(name, user.Password)
} else {
// pass through any hidden fields
val, ok := s.Attr("value")
if !ok {
return
}
authForm.Add(name, val)
}
}