forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keycloak.go
152 lines (121 loc) · 3.49 KB
/
keycloak.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
package saml2aws
import (
"bytes"
"crypto/tls"
"io/ioutil"
"log"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/pkg/errors"
"fmt"
"golang.org/x/net/publicsuffix"
)
// KeyCloakClient wrapper around KeyCloak.
type KeyCloakClient struct {
client *http.Client
}
// NewKeyCloakClient create a new KeyCloakClient
func NewKeyCloakClient(skipVerify bool) (*KeyCloakClient, error) {
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: skipVerify},
Proxy: http.ProxyFromEnvironment,
}
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 &KeyCloakClient{
client: client,
}, nil
}
// Authenticate logs into KeyCloak and returns a SAML response
func (kc *KeyCloakClient) Authenticate(loginDetails *LoginDetails) (string, error) {
var authSubmitURL string
var samlAssertion string
authForm := url.Values{}
samlAssertion = ""
keyCloakURL := fmt.Sprintf("https://%s", loginDetails.Hostname)
// fmt.Printf("KeyCloak URL: %s\n\n", keyCloakURL)
res, err := kc.client.Get(keyCloakURL)
if err != nil {
return "", errors.Wrap(err, "error retieving form")
}
// fmt.Printf("Response: %d\n\n", res.StatusCode)
doc, err := goquery.NewDocumentFromResponse(res)
if err != nil {
return "", errors.Wrap(err, "failed to build document from response")
}
doc.Find("input").Each(func(i int, s *goquery.Selection) {
updateKeyCloakFormData(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 = kc.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 updateKeyCloakFormData(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, "username") {
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)
}
}