forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uaa_connection.go
112 lines (96 loc) · 2.88 KB
/
uaa_connection.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
package uaa
import (
"bytes"
"crypto/x509"
"encoding/json"
"io/ioutil"
"net"
"net/http"
"net/url"
"time"
"code.cloudfoundry.org/cli/util"
)
// UAAConnection represents the connection to UAA
type UAAConnection struct {
HTTPClient *http.Client
}
// NewConnection returns a pointer to a new UAA Connection
func NewConnection(skipSSLValidation bool, disableKeepAlives bool, dialTimeout time.Duration) *UAAConnection {
tr := &http.Transport{
DialContext: (&net.Dialer{
KeepAlive: 30 * time.Second,
Timeout: dialTimeout,
}).DialContext,
DisableKeepAlives: disableKeepAlives,
Proxy: http.ProxyFromEnvironment,
TLSClientConfig: util.NewTLSConfig(nil, skipSSLValidation),
}
return &UAAConnection{
HTTPClient: &http.Client{
Transport: tr,
CheckRedirect: func(_ *http.Request, _ []*http.Request) error {
// This prevents redirects. When making a request to /oauth/authorize,
// the client should not follow redirects in order to obtain the ssh
// passcode.
return http.ErrUseLastResponse
},
},
}
}
// Make takes a passedRequest, converts it into an HTTP request and then
// executes it. The response is then injected into passedResponse.
func (connection *UAAConnection) Make(request *http.Request, passedResponse *Response) error {
// In case this function is called from a retry, passedResponse may already
// be populated with a previous response. We reset in case there's an HTTP
// error and we don't repopulate it in populateResponse.
passedResponse.reset()
response, err := connection.HTTPClient.Do(request)
if err != nil {
return connection.processRequestErrors(request, err)
}
return connection.populateResponse(response, passedResponse)
}
func (*UAAConnection) handleStatusCodes(response *http.Response, passedResponse *Response) error {
if response.StatusCode >= 400 {
return RawHTTPStatusError{
StatusCode: response.StatusCode,
RawResponse: passedResponse.RawResponse,
}
}
return nil
}
func (connection *UAAConnection) populateResponse(response *http.Response, passedResponse *Response) error {
passedResponse.HTTPResponse = response
rawBytes, err := ioutil.ReadAll(response.Body)
defer response.Body.Close()
if err != nil {
return err
}
passedResponse.RawResponse = rawBytes
err = connection.handleStatusCodes(response, passedResponse)
if err != nil {
return err
}
if passedResponse.Result != nil {
decoder := json.NewDecoder(bytes.NewBuffer(passedResponse.RawResponse))
decoder.UseNumber()
err = decoder.Decode(passedResponse.Result)
if err != nil {
return err
}
}
return nil
}
func (connection *UAAConnection) processRequestErrors(request *http.Request, err error) error {
switch e := err.(type) {
case *url.Error:
if _, ok := e.Err.(x509.UnknownAuthorityError); ok {
return UnverifiedServerError{
URL: request.URL.String(),
}
}
return RequestError{Err: e}
default:
return err
}
}