/
oauth_proxy_auth.go
368 lines (310 loc) · 9.91 KB
/
oauth_proxy_auth.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package resources
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"strings"
"github.com/PuerkitoBio/goquery"
"golang.org/x/net/html"
)
const (
testingIDP = "testing-idp"
)
func Auth3Scale(client *http.Client, redirectUrl, keycloakHost, clientId, secret string) (string, error) {
// Start the authentication
u := fmt.Sprintf("%v/auth/realms/openshift/protocol/openid-connect/auth?client_id=%v&redirect_uri=%v&response_type=code&scope=openid", keycloakHost, clientId, redirectUrl)
response, err := client.Get(u)
if err != nil {
return "", fmt.Errorf("failed to open %s: %s", u, err)
}
if response.StatusCode != 200 {
return "", errorWithResponseDump(response, fmt.Errorf("the request to %s failed with code %d", u, response.StatusCode))
}
// Select the testing IDP
document, err := ParseHtmlResponse(response)
if err != nil {
return "", errorWithResponseDump(response, err)
}
// find the link to the testing IDP
link, err := findElement(document, fmt.Sprintf("a:contains('%s')", testingIDP))
if err != nil {
return "", errorWithResponseDump(response, err)
}
// get the url from the link
href, err := getAttribute(link, "href")
if err != nil {
return "", errorWithResponseDump(response, err)
}
u, err = resolveRelativeURL(response, href)
if err != nil {
return "", err
}
response, err = client.Get(u)
if err != nil {
return "", fmt.Errorf("failed to request %s: %s", u, err)
}
if response.StatusCode != 200 {
return "", errorWithResponseDump(response, fmt.Errorf("the request to %s failed with code %d", u, response.StatusCode))
}
document, err = ParseHtmlResponse(response)
if err != nil {
return "", errorWithResponseDump(response, err)
}
// 3scale auth page, select the keycloak auth option
link, err = findElement(document, "a.authorizeLink")
if err != nil {
return "", errorWithResponseDump(response, err)
}
href, err = getAttribute(link, "href")
if err != nil {
return "", errorWithResponseDump(response, err)
}
response, err = client.Get(href)
if err != nil {
return "", fmt.Errorf("failed to open %s: %s", u, err)
}
if response.StatusCode != 200 {
return "", errorWithResponseDump(response, fmt.Errorf("the request to %s failed with code %d", u, response.StatusCode))
}
code := strings.Split(response.Request.URL.RawQuery, "&")[1]
tokenUrl := fmt.Sprintf("%v/auth/realms/openshift/protocol/openid-connect/token", keycloakHost)
formValues := url.Values{
"grant_type": []string{"authorization_code"},
"code": []string{strings.Split(code, "=")[1]},
"client_id": []string{"3scale"},
"redirect_uri": []string{redirectUrl},
"client_secret": []string{secret},
}
response, err = client.PostForm(tokenUrl, formValues)
if err != nil {
return "", fmt.Errorf("failed to request %s: %s", u, err)
}
// extract the token
postBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
tokenResponse := struct {
AccessToken string `json:"access_token"`
}{}
err = json.Unmarshal(postBody, &tokenResponse)
if err != nil {
return "", fmt.Errorf("failed to unmarshal json: %v", err)
}
return tokenResponse.AccessToken, nil
}
// Login a user through the oauth proxy
func ProxyOAuth(client *http.Client, host string, username string, password string) (*http.Client, error) {
// Start the authentication
u := fmt.Sprintf("%s/oauth/start", host)
response, err := client.Get(u)
if err != nil {
return nil, fmt.Errorf("failed to open %s: %s", u, err)
}
if response.StatusCode != 200 {
return nil, errorWithResponseDump(response, fmt.Errorf("the request to %s failed with code %d", u, response.StatusCode))
}
// Select the testing IDP
document, err := ParseHtmlResponse(response)
if err != nil {
return nil, errorWithResponseDump(response, err)
}
// find the link to the testing IDP
link, err := findElement(document, fmt.Sprintf("a:contains('%s')", testingIDP))
if err != nil {
return nil, errorWithResponseDump(response, err)
}
// get the url from the
href, err := getAttribute(link, "href")
if err != nil {
return nil, errorWithResponseDump(response, err)
}
u, err = resolveRelativeURL(response, href)
if err != nil {
return nil, err
}
response, err = client.Get(u)
if err != nil {
return nil, fmt.Errorf("failed to request %s: %s", u, err)
}
if response.StatusCode != 200 {
return nil, errorWithResponseDump(response, fmt.Errorf("the request to %s failed with code %d", u, response.StatusCode))
}
// Submit the username and password
document, err = ParseHtmlResponse(response)
if err != nil {
return nil, errorWithResponseDump(response, err)
}
// find the form for the login
form, err := findElement(document, "#kc-form-login")
if err != nil {
// maybe wer are already logged in (previous openshift login)
// find the form for the approval
form = document.Find("form")
if form.Length() == 0 {
// Nope
// return nil, errorWithResponseDump(response, err)
// this is a workaround for the oauth-proxy issue (https://issues.redhat.com/browse/INTLY-8473)
return client, nil
}
_, err = approvePermissions(form, client, response)
if err != nil {
return nil, errorWithResponseDump(response, err)
}
return client, nil
}
// retrieve the action of the form
action, err := getAttribute(form, "action")
if err != nil {
return nil, errorWithResponseDump(response, err)
}
u, err = resolveRelativeURL(response, action)
if err != nil {
return nil, err
}
// submit the form with the username and password
v := url.Values{"username": []string{username}, "password": []string{password}}
response, err = client.PostForm(u, v)
if err != nil {
return nil, fmt.Errorf("failed to request %s: %s", u, err)
}
if response.StatusCode != 200 {
return nil, errorWithResponseDump(response, fmt.Errorf("the request to %s failed with code %d", u, response.StatusCode))
}
document, err = ParseHtmlResponse(response)
if err != nil {
return nil, err
}
// find the form for the approval
form = document.Find("form")
if err != nil {
return nil, errorWithResponseDump(response, err)
}
// No form found: no further approval required, we are authenticated
// at this point
if form.Length() == 0 {
return client, nil
}
// On first login the user is presented with an approval form. We have to submit
// the form along with the scopes that we want to grant.
_, err = approvePermissions(form, client, response)
if err != nil {
return nil, err
}
return client, nil
}
// Submit permission approval form
func approvePermissions(form *goquery.Selection, client *http.Client, response *http.Response) (string, error) {
// retrieve the action of the form
action, err := getAttribute(form, "action")
if err != nil {
return "", err
}
// form submit url
formUrl, err := resolveRelativeURL(response, action)
if err != nil {
return "", err
}
then, _ := form.Find("input[name='then']").Attr("value")
csrf, _ := form.Find("input[name='csrf']").Attr("value")
clientId, _ := form.Find("input[name='client_id']").Attr("value")
userName, _ := form.Find("input[name='user_name']").Attr("value")
redirectUrl, _ := form.Find("input[name='redirect_uri']").Attr("value")
_, err = client.PostForm(formUrl, url.Values{
"then": []string{then},
"csrf": []string{csrf},
"client_id": []string{clientId},
"user_name": []string{userName},
"redirect_uri": []string{redirectUrl},
"scope": []string{"user:info", "user:check-access"},
"approve": []string{"Allow+selected+permissions"},
})
return redirectUrl, err
}
func dumpResponse(r *http.Response) string {
msg := "> Request\n"
reqOut, err := httputil.DumpRequestOut(r.Request, false)
if err != nil {
msg += fmt.Sprintf("failed to dump the request: %s", err)
} else {
msg += string(reqOut)
}
msg += "\n"
msg += "< Response\n"
for name, values := range r.Header {
// Loop over all values for the name.
for _, value := range values {
msg += name + " " + value + "\n"
}
}
document, err := ParseHtmlResponse(r)
if err != nil {
msg += "parse html response failed"
} else {
selection := document.Find("body")
if selection.Length() == 1 {
var b bytes.Buffer
err = html.Render(&b, selection.Nodes[0])
if err != nil {
msg += "html reader failed"
}
msg += b.String()
}
}
msg += "\n"
return msg
}
func errorWithResponseDump(r *http.Response, err error) error {
return fmt.Errorf("%s\n\n%s", err, dumpResponse(r))
}
func ParseHtmlResponse(r *http.Response) (*goquery.Document, error) {
// Clone the body while reading it so that in case of errors
// we can dump the response with the body
var clone bytes.Buffer
body := io.TeeReader(r.Body, &clone)
r.Body = ioutil.NopCloser(&clone)
d, err := goquery.NewDocumentFromReader(body)
if err != nil {
return nil, fmt.Errorf("failed to create the document: %s", err)
}
// <noscript> bug workaround
// https://github.com/PuerkitoBio/goquery/issues/139#issuecomment-517526070
d.Find("noscript").Each(func(i int, s *goquery.Selection) {
s.SetHtml(s.Text())
})
return d, nil
}
func resolveRelativeURL(r *http.Response, relativeURL string) (string, error) {
u, err := url.Parse(relativeURL)
if err != nil {
return "", fmt.Errorf("failed to parse the url %s: %s", relativeURL, err)
}
u = r.Request.URL.ResolveReference(u)
return u.String(), nil
}
func findElement(d *goquery.Document, selector string) (*goquery.Selection, error) {
e := d.Find(selector)
if e.Length() == 0 {
return nil, fmt.Errorf("failed to find an element matching the selector %s", selector)
}
if e.Length() > 1 {
return nil, fmt.Errorf("multiple element founded matching the selector %s", selector)
}
return e, nil
}
func getAttribute(element *goquery.Selection, name string) (string, error) {
v, ok := element.Attr(name)
if !ok {
e, err := element.Html()
if err != nil {
e = fmt.Sprintf("failed to get the html content: %s", err)
}
return "", fmt.Errorf("the element '%s' doesn't have the %s attribute", e, name)
}
return v, nil
}