forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jumpcloud.go
191 lines (156 loc) · 4.62 KB
/
jumpcloud.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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"
"github.com/segmentio/go-prompt"
"golang.org/x/net/publicsuffix"
)
// JumpCloudClient is a wrapper representing a JumpCloud SAML client
type JumpCloudClient struct {
client *http.Client
}
// NewJumpCloudClient creates a new JumpCloud client
func NewJumpCloudClient(skipVerify bool) (*JumpCloudClient, 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 &JumpCloudClient{
client: client,
}, nil
}
// Authenticate logs into JumpCloud and returns a SAML response
func (jc *JumpCloudClient) Authenticate(loginDetails *LoginDetails) (string, error) {
var authSubmitURL string
var samlAssertion string
mfaRequired := false
authForm := url.Values{}
jumpCloudURL := fmt.Sprintf("https://%s", loginDetails.Hostname)
res, err := jc.client.Get(jumpCloudURL)
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) {
updateJumpCloudForm(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")
}
authSubmitURL = fmt.Sprintf("https://sso.jumpcloud.com/%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")
// Temporarily disable following redirects so we can detect MFA.
disableFollowRedirect(jc.client)
res, err = jc.client.Do(req)
if err != nil {
return samlAssertion, errors.Wrap(err, "error retrieving login form")
}
if res.StatusCode == 302 {
location, err := res.Location()
if err != nil {
return samlAssertion, errors.Wrap(err, "error retrieving redirect location")
}
if location.EscapedPath() == "/login/user/mfa" {
mfaRequired = true
} else {
// Just follow the redirect.
res, err = jc.client.Get(location.String())
if err != nil {
return samlAssertion, errors.Wrap(err, "error retrieving SAML response")
}
}
}
enableFollowRedirect(jc.client)
if mfaRequired {
token := prompt.StringRequired("MFA Token")
authForm.Add("otp", token)
req, err := http.NewRequest("POST", authSubmitURL, strings.NewReader(authForm.Encode()))
if err != nil {
return samlAssertion, errors.Wrap(err, "error building MFA authentication request")
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, err = jc.client.Do(req)
if err != nil {
return samlAssertion, errors.Wrap(err, "error submitting MFA login form")
}
}
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 updateJumpCloudForm(authForm url.Values, s *goquery.Selection, user *LoginDetails) {
name, ok := s.Attr("name")
if !ok {
return
}
lname := strings.ToLower(name)
if strings.Contains(lname, "email") {
authForm.Add(name, user.Username)
} else if strings.Contains(lname, "password") {
authForm.Add(name, user.Password)
} else {
// pass through any hidden fields
val, ok := s.Attr("value")
if !ok {
return
}
authForm.Add(name, val)
}
}
func disableFollowRedirect(client *http.Client) {
client.CheckRedirect = func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}
}
func enableFollowRedirect(client *http.Client) {
client.CheckRedirect = nil
}